
Simple Class (no inheritance): Invocation of constructors and Destructors – [C++]
Let’s see the order of invocation of constructors and Destructors in C++ having a simple Class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//The sequence of constructor invoked and destructor invokes in C++ #include<iostream> using namespace std; class T4Tutorials { public: T4Tutorials() { cout<<"<a href="https://t4tutorials.com/constructor-of-a-class-purpose-of-constructor-in-c-oop/">First: T4Tutorials class constructor</a>."<<endl; } ~T4Tutorials() { cout<<"Second: T4Tutorials class destructor."<<endl; } }; int main() { T4Tutorials object; return 0; } |
Single inheritance: Invocation of constructors and Destructors – [C++]
Let’s see the invocation of constructors and Destructors in Single inheritance in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//eg of constructor and destructor in single inheritance #include<iostream> using namespace std; class base { public: base() { cout<<"The base class constructor called first."<<endl; } ~base() { cout<<"The base class destructor called second."<<endl; } }; class derived:public base { public: derived() { cout<<"The derived class constructor called second."<<endl; } ~derived() { cout<<"The derived class destructor called first."<<endl; } }; int main() { derived d; return 0; } |
Multiple inheritance: Invocation of constructors and Destructors – [C++]
Let’s see the invocation of constructors and Destructors in Multiple inheritance in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
//The sequence of constructor invoked and destructor invokes in multiple inheritance in C++ #include<iostream> using namespace std; class T4Tutorials_Parent1 { public: T4Tutorials_Parent1() { cout<<"First: T4Tutorials_Parent1 class constructor."<<endl; } ~T4Tutorials_Parent1() { cout<<"Third: T4Tutorials_Parent1 class destructor."<<endl; } }; class T4Tutorials_Parent2 { public: T4Tutorials_Parent2() { cout<<"Second: T4Tutorials_Parent2 class constructor."<<endl; } ~T4Tutorials_Parent2() { cout<<"Second: T4Tutorials_Parent2 class destructor."<<endl; } }; class derived:public T4Tutorials_Parent1, public T4Tutorials_Parent2 { public: derived() { cout<<"Third: derived class constructor."<<endl; } ~derived() { cout<<"First: derived class destructor."<<endl; } }; int main() { derived d; return 0; } |
Multilevel inheritance: Invocation of constructors and Destructors – [C++]
Let’s see the invocation of constructors and Destructors in Multi Level inheritance in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
//The sequence of constructor invoked and destructor invokes in multiple inheritance in C++ #include<iostream> using namespace std; class T4Tutorials_GrandFather { public: T4Tutorials_GrandFather() { cout<<"First: T4Tutorials_GrandFather class constructor"<<endl; } ~T4Tutorials_GrandFather() { cout<<"Third: T4Tutorials_GrandFather class destructor"<<endl; } }; class T4Tutorials_Father:public T4Tutorials_GrandFather { public: T4Tutorials_Father() { cout<<"Second: T4Tutorials_Father class constructor"<<endl; } ~T4Tutorials_Father() { cout<<"Second: T4Tutorials_Father class destructor"<<endl; } }; class T4Tutorials_Child:public T4Tutorials_Father { public: T4Tutorials_Child() { cout<<"Third: T4Tutorials_Child class constructor"<<endl; } ~T4Tutorials_Child() { cout<<"First: T4Tutorials_Child class destructor"<<endl; } }; int main() { T4Tutorials_Child d; return 0; } |