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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago