constructor-inh/multiple-4.cc - Constructors and multiple inheritance - order of construction and destruction. | Lecture 5 - slide 4 : 40 Program 4 |
// Demonstration of construction and destruction order. #include <iostream> #include <string> using namespace std; class Base1{ public: int i; string s; Base1(int i, string s): i(i), s(s){ cout << "Base1 constructor" << endl; } ~Base1(){ cout << "Base1 destructor" << endl; } }; class Base2{ public: int j; bool b; Base2(int j, bool b): j(j), b(b){ cout << "Base2 constructor" << endl; } ~Base2(){ cout << "Base2 destructor" << endl; } }; class Derived: public Base1, public Base2{ // Declaration order: Base1 before Base2 public: double d; Derived(double d, int i, string s): Base1(i+1, s+s), Base2(i+2, false), d(d){ cout << "Derived constructor" << endl; } ~Derived(){ cout << "Derived destructor" << endl; } }; int main(){ Derived d1(5.0, 1, "AP"); // Base1 constructor // Base2 constructor // Derived constructor // Derived destructor // Base2 destructor // Base1 destructor }