Site icon T4Tutorials.com

Order of Invocation/call of constructors and Destructors in C++

Most of the time you are confused when you read in a program comment that constructor invoked or destructor invoked.

The sequence of invocation of constructors and Destructors in C++ is different in different situations. Let’s see them one by one.

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.

//The sequence of constructor invoked and destructor invokes in C++
#include<iostream>
using namespace std;
class T4Tutorials
{
public:
    T4Tutorials()
    {
        cout<<"First: T4Tutorials class constructor."<<endl;
    } 
    ~T4Tutorials()
    {
        cout<<"Second: T4Tutorials class destructor."<<endl;
    }
};

int main()
{
    T4Tutorials object;
    return 0;
}

Output

First: T4Tutorials class constructor.

Second: T4Tutorials class destructor.

If we want to know the sequence of Invocation of constructors and destructors, then it is important to know that it depends on the type of inheritance being used.
Here, I am demonstrating the sequence in which constructors and destructors are called in single and multiple inheritence.

Constructor and destructor invocation (invoked)in single inheritance
In single inheritance, firstly, parent class constructors are called and then the chile class constructors are called.
Destructor and Destructor invocation (invoked)in single inheritance

In single inheritance, firstly, Child class destructors are called and then the parent class destructors are called.

Single inheritance: Invocation of constructors and Destructors – [C++]

Let’s see the invocation of constructors and Destructors in Single inheritance in C++.

//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;
}

Output

The output of the program will be;

The base class constructor called first

The derived class constructor called second.

The derived class destructor called first.

The base class destructor called second.

Multiple inheritance: Invocation of constructors and Destructors – [C++]

Let’s see the invocation of constructors and Destructors in Multiple inheritance in C++.

//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;
}

Output:

First: T4Tutorials_Parent1 class constructor.

Second: T4Tutorials_Parent2 class constructor.

Third: derived class constructor.

First: derived class destructor.

Second: T4Tutorials_Parent2 class destructor.

Third: T4Tutorials_Parent1 class destructor.

Multilevel inheritance: Invocation of constructors and Destructors – [C++]

Let’s see the invocation of constructors and Destructors in Multi Level inheritance in C++.

//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;
}

Output:

First: T4Tutorials_GrandFather class constructor.

Second: T4Tutorials_Father class constructor.

Third: T4Tutorials_Child class constructor.

First: T4Tutorials_Child class destructor.

Second: T4Tutorials_Father class destructor.

Third: T4Tutorials_GrandFather class destructor.

Video Lecture

Exit mobile version