Order of Constructor And Destructor Calls in C++

In this post, I am going to discuss the order of constructor and destructor calls In C++. When we create a class object, the default constructor of that class is immediately called to initialize the class’s members.

If we inherit a class from another class and create an object of the derived class, it is obvious that the derived class’s default constructor will be invoked first, followed by the default constructor of all base classes, i.e. the order of invocation is that the base class’s default constructor is invoked first, followed by the derived class’s default constructor.

Why is the constructor of the base class called when a derived class object is created?

To grasp this, you’ll need to recall your inheritance knowledge. When a class is inherited from another, what happens? Based on the access specifier, the data members and member functions of the base class are automatically included in derived classes, but the description of these members is only available in the base class.

As a result, when we create a derived class object, we must initialize all of the derived class’s members, but the inherited members in derived classes can only be initialized by the base class’s constructor because their definition is only in the base class. This is why the base class’s constructor is called first in order to initialize all of the inherited members.

// C++ program to show the order of constructor call
// in single inheritance

#include <iostream>
using namespace std;

// base class
class Parent
{
	public:
	
	// base class constructor
	Parent()
	{
		cout << "Inside base class" << endl;
	}
};

// sub class
class Child : public Parent
{
	public:
	
	//sub class constructor
	Child()
	{
		cout << "Inside sub class" << endl;
	}
};

// main function
int main() {
	
	// creating object of sub class
	Child obj;
	
	return 0;
}

Output:

Inside base class
Inside sub class

Join Our Community

Join our WhatsApp Group To know more about Programming Language tips, tricks and knowledge about and how to start learning any programming language.

Order of constructor call for Multiple Inheritance:

When multiple inheritances is used, the base class’s constructors are called first, followed by the derived class’s constructor.

// C++ program to show the order of constructor calls
// in Multiple Inheritance

#include <iostream>
using namespace std;

// first base class
class Parent1
{
	
	public:
	
	// first base class's Constructor
	Parent1()
	{
		cout << "Inside first base class" << endl;
	}
};

// second base class
class Parent2
{
	public:
	
	// second base class's Constructor
	Parent2()
	{
		cout << "Inside second base class" << endl;
	}
};

// child class inherits Parent1 and Parent2
class Child : public Parent1, public Parent2
{
	public:
	
	// child class's Constructor
	Child()
	{
		cout << "Inside child class" << endl;
	}
};

// main function
int main() {
	
	// creating object of class Child
	Child obj1;
	return 0;
}

Output:

Inside first base class
Inside second base class
Inside child class

The order of constructors and destructors requires a specific inheritance order

order of constructor destructor calling
Order Of Constructor Calling

How to call the parameterized constructor of base class in derived class constructor?

To call the parameterized constructor of the base class when the parameterized constructor of the derived class is called, you must explicitly specify the parameterized constructor of the base class in the derived class, as illustrated in the following programme:

// C++ program to show how to call parameterised Constructor
// of base class when derived class's Constructor is called

#include <iostream>
using namespace std;

// base class
class Parent {
	int x;

public:
	// base class's parameterised constructor
	Parent(int i)
	{
		x = i;
		cout << "Inside base class's parameterised "
				"constructor"
			<< endl;
	}
};

// sub class
class Child : public Parent {
	int j;

public:
	// sub class's parameterised constructor
	Child(int x) : Parent(j), j(x)
	{
		cout << "Inside sub class's parameterised "
				"constructor"
			<< endl;
	}
};

// main function
int main()
{

	// creating object of class Child
	Child obj1(10);
	return 0;
}

Output:

Inside base class's parameterised constructor
Inside sub class's parameterised constructor

Important Points

The base class’s default constructors are called automatically whenever the derived class’s default constructors are called.

We must explicitly invoke the parameterized constructors of the base class inside the parameterized constructors of the derived class.

The parameterized constructors of the base class should not be called in the default constructors of the subclass; instead, they should be called inside the parameterized constructors of the subclass.

Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 217

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply