Categories: C++

Order of Constructor Call in C++

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, but first, the default constructors of all base classes will be invoked.

The data members and member functions of the base class are automatically included in the derived class based on the access specifier, but the definition of these members is only available in the base class. When we create an object of a derived class, all of its members must be initialized, but the inherited members in the derived class can only be initialized by the base class’s constructor because their definition exists only in the base class. This is why the base class’s constructor is called first to initialize all inherited members.

Example:
#include<iostream>
using namespace std;

//base class
class Device{

public:
 Device(){
     cout<<"Constructor: Device\n";
     
 }
 ~Device(){
     cout<<"Destructor : Device\n";
     
 }
};

//derived class
class Mobile:public Device{

public:

 Mobile(){
     cout<<"Constructor: Mobile\n";
     
 }
 ~Mobile(){
     cout<<"Destructor : Mobile\n";
     
 }
};

//derived class
class Android:public Mobile{

public:

 Android(){
     cout<<"Constructor: Android\n";
     
 }
 ~Android(){
     cout<<"Destructor : Android\n";
     
 }
};



int main()
{
 Android _android; // create the object that will call required constructors 

 return 0;
}
Output:
Constructor: Device
Constructor: Mobile
Constructor: Android
Destructor : Android
Destructor : Mobile
Destructor : Device

Important points:

  • Whenever the derived class’s default constructor is called, the base class’s default constructor is called automatically.
  • To call the parameterized constructor of the base class inside the parameterized constructor of subclass, we have to mention it explicitly.
  • The parameterized constructor of a base class cannot be called in the default constructor of the subclass, it should be called in the parameterized constructor of the subclass.

Note: also read about Types of Inheritance in C++

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 day ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago