fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A
  5. {
  6. protected:
  7. int x,y,z;
  8. public:
  9. A(int a) : x(a),y(a*a),z(a*a*a) {cout<<"Конструктор А "<<this<<endl;}
  10. virtual ~A() {cout<<"Деструктор А "<<this<<endl;}
  11. virtual void func_1(A *) const =0 ;
  12. virtual void func_2() const =0 ;
  13. int get_x() const {return x;}
  14. int get_y() const {return y;}
  15. int get_z() const {return z;}
  16. };
  17.  
  18. class B : public A
  19. {
  20. public:
  21. B(int a) : A(a) {cout<<"Конструктор B "<<this<<endl;}
  22. ~B() {cout<<"Деструктор B "<<this<<endl;}
  23. void func_1(A *b) const
  24. {
  25. cout<<"Данные класса C: ";
  26. cout<<"x = "<<b->get_x();
  27. cout<<" y = "<<b->get_y();
  28. cout<<" z = "<<b->get_z()<<endl;
  29. b->func_2();
  30. }
  31. void func_2() const{cout<<"Класс B x = "<<x
  32. <<" y = "<<y<<" z = "<<z<<endl;}
  33. };
  34.  
  35. class C : public A
  36. {
  37. public:
  38. C(int a) : A(a) {cout<<"Конструктор C "<<this<<endl;}
  39. ~C() {cout<<"Деструктор C "<<this<<endl;}
  40. void func_1(A *c) const
  41. {
  42. cout<<"Данные класса B: ";
  43. cout<<"x = "<<c->get_x();
  44. cout<<" y = "<<c->get_y();
  45. cout<<" z = "<<c->get_z()<<endl;
  46. c->func_2();
  47. }
  48. void func_2() const{cout<<"Класс C x = "<<x
  49. <<" y = "<<y<<" z = "<<z<<endl;}
  50. };
  51.  
  52. int main()
  53. {
  54. A *c = new C(56);
  55. A *b = new B(45);
  56. c->func_1(b);
  57. b->func_1(c);
  58. cout<<"***************************"<<endl;
  59.  
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
Конструктор А 0x9b2fa10
Конструктор C 0x9b2fa10
Конструктор А 0x9b2fa28
Конструктор B 0x9b2fa28
Данные класса B: x = 45 y = 2025 z = 91125
Класс B x = 45 y = 2025 z = 91125
Данные класса C: x = 56 y = 3136 z = 175616
Класс C x = 56 y = 3136 z = 175616
***************************