fork download
  1. #include "iostream"
  2. using namespace std;
  3.  
  4. class A {
  5. int i;
  6. public:
  7. A(int ii) : i(ii) {
  8. cout << "\n Constructor of A is called \n";
  9.  
  10. }
  11. ~A() {
  12. cout << "\n destructor of A is called \n";
  13. }
  14. void f() const {}
  15. };
  16.  
  17. class B {
  18. int i;
  19. public:
  20. B(int ii) : i(ii) {
  21. cout << "\n Constructor of B is called \n";
  22. }
  23. ~B() {
  24. cout << "\n destructor of B is called \n";
  25. }
  26. void f() const {}
  27. };
  28.  
  29. class C : public B {
  30. A a;
  31. public:
  32. C(int ii) : a(ii), B(ii) {
  33. cout << "\n Constructor of C is called \n";
  34. }
  35. ~C() {
  36. cout << "\n destructor of C is called \n";
  37. } // Calls ~A() and ~B()
  38. void f() const { // Redefinition
  39. a.f();
  40. B::f();
  41. }
  42. };
  43.  
  44. int main() {
  45. C c(47);
  46. } ///:~
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
 Constructor of B is called 

 Constructor of A is called 

 Constructor of C is called 

 destructor  of C is called 

 destructor  of A is called 

 destructor  of B is called