Inheritance in C++ - Order of Invocation of Constructors and Destructors

Knoji reviews products and up-and-coming brands we think you'll love. In certain cases, we may receive a commission from brands mentioned in our guides. Learn more.
The article discusses the order of construction and destruction of objects in C++ for automatic object allocation on stack. In other words, here we discuss the order in which the special member functions - constructors and destructors would be automatical

The constructors and destructors in C++ are special member functions of classes which are automatically invoked when objects are created and destroyed respectively. However, when a object being constructed is part of an inheritance hierarchy, which means it inherits from one or more parent classes (which could also be subclasses of other super classes), then not just a single constructor/destructor but all the constructors and destructors of the classes involved in the inheritance hierarchy are invoked.

The constructors in this case are invoked in the order of derivation, which means that constructor of the super class residing at the top of the hierarchy is invoked first. After that, the constructor of the class immediately derived from the root class is invoked, and so on till the most derived class’s constructor - the one whose object is being constructed is invoked.

Consider the code below, and it’s output. It’s clear that the constructor of class A is invoked first, followed by constructors of classes B, C and D.    

It’s important that the construction of a class hierarchy is done is order of derivation, since the derived class is supposed to inherit all the properties (member functions and instance variables) of base class. Thus, when it’s own object gets constructed, the sub class should have access to the completely constructed super class.

The destructors however, are invoked in the reverse order of derivation. Consider the output of the above program again, it’s clear that the destructor of class D, which is the most derived class, is invoked first and then the destructors of classes C, B and A are invoked respectively.   

Important: Notice that the object allocated is automatic - which means it’s allocated on stack (auto storage class). When objects are allocated on heap using pointers, the order of construction is the same, but the order of destruction is not. The details of this concept is out of scope of this article. 

#include <iostream>

using namespace std;

class A {    

public:

    A()

    {

        cout << "Class A's Constructorn";

    }

 

    ~A()

    {

        cout << "Class A's Destructorn";

    }

};

 

class B : public A

{

public:

    B()

    {

        cout << "Class B's Constructorn";

    }

    ~B()

    {

        cout << "Class B's Destructorn";

    }

   

   

};

 

class C : public B

{

public:

    C()

    {

        cout << "Class C's Constructorn";

    }

    ~C()

    {

        cout << "Class C's Destructorn";

    }

    

    

};

 

class D : public C

{

public:

    D()

    {

        cout << "Class D's Constructorn";

    }

    

    ~D()

    {

        cout << "Class D's Destructorn";

    }

  

};

 

int main ()

{

    D object;

    return 0;

}

 

Output:

Class A's Constructor

Class B's Constructor

Class C's Constructor

Class D's Constructor

Class D's Destructor

Class C's Destructor

Class B's Destructor

Class A's Destructor

3 comments

john doe
0
Posted on Feb 25, 2012
Donata L.
0
Posted on Feb 25, 2012
Roberta Baxter
0
Posted on Feb 24, 2012