fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A
  5. {
  6. int x;
  7. public:
  8. A() : x(0) {cout<<"Конструктор А 1"<<endl;}
  9. A(int a) : x(a) {cout<<"Конструктор А 2"<<endl;}
  10. int func() const {return x;}
  11. };
  12. class B : public virtual A
  13. {
  14. int x;
  15. public:
  16. B(int a) : A(a*a), x(a) {cout<<"Конструктор B"<<endl;}
  17. int func() const {return x;}
  18. };
  19. class C : public virtual A
  20. {
  21. int x;
  22. public:
  23. C(int a) : A(a*a), x(a) {cout<<"Конструктор C"<<endl;}
  24. int func() const {return x;}
  25. };
  26. class D : public B, public C
  27. {
  28. int x;
  29. public:
  30. D(int a, int b) : B(a), C(b), x(a+b) {cout<<"Конструктор D"<<endl;}
  31. int func() const {return x;}
  32. };
  33.  
  34.  
  35. int main()
  36. {
  37. D a(12,78);
  38. cout<<"D::x = "<<a.func()<<endl;
  39. cout<<"B::x = "<<a.B::func()<<endl;
  40. cout<<"C::x = "<<a.C::func()<<endl;
  41. cout<<"А::x = "<<a.A::func()<<endl;
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Конструктор А 1
Конструктор B
Конструктор C
Конструктор D
D::x = 90
B::x = 12
C::x = 78
А::x = 0