Inheritance in C++ - Order of Invocation of Constructors and Destructors
Browse articles:
Auto Beauty Business Culture Dieting DIY Events Fashion Finance Food Freelancing Gardening Health Hobbies Home Internet Jobs Law Local Media Men's Health Mobile Nutrition Parenting Pets Pregnancy Products Psychology Real Estate Relationships Science Seniors Sports Technology Travel Wellness Women's Health
Browse companies:
Automotive Crafts, Hobbies & Gifts Department Stores Electronics & Wearables Fashion Food & Drink Health & Beauty Home & Garden Online Services & Software Sports & Outdoors Subscription Boxes Toys, Kids & Baby Travel & Events

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

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 automatically invoked when objects are created. The article also contains sample code with four classes to illustrate the discussed concept.

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

Need an answer?
Get insightful answers from community-recommended
experts
in Computer Programming & Languages on Knoji.
Would you recommend this author as an expert in Computer Programming & Languages?
You have 0 recommendations remaining to grant today.
Comments (3)

This information is way over my head,but I thank you for your willingness to supply this to those who understand and can learn from your article. Out of votes so will tweet.

Wow...great educational piece! Don't know how much of this I will retain..but you did a great job in putting this piece together. I'm sure this post will benefit someone seeking this information. Thanks for sharing your knowledge in this area...voted up!

This is outstanding work.

ARTICLE DETAILS
RELATED ARTICLES
RELATED CATEGORIES
ARTICLE KEYWORDS
RECENT SEARCHES ON KNOJI SHOPPING