In what order are the class constructor called when a derived class object is created?

When we inherit class into another class then object of base class is initialized first. If a class do not have any constructor then default constructor will be called. But if we have created any parameterized constructor then we have to initialize base class constructor from derived class.

We have to call constructor from another constructor. It is also known as constructor chaining.

When we have to call same class constructor from another constructor then we use this keyword. In addition, when we have to call base class constructor from derived class then we use base keyword.

In the following example we have created Abc is as a base class and inherit it into Pqr. We have created parameterized constructor in class Abc so we have to initialize this constructor from derived class constructor in the following manner:-



		
public class Abc
{
    public int p, q;
    public Abc(int p1, int p2)
    {
        p = p1;
        q = p2;
    }
    public int sum(int x, int y)
    {
        return (x + y);
    }
    
}

//derived class/ child class
public class Pqr : Abc
{
    public int a;
    public Pqr(int a1,int p1, int p2):base(p1,p2)
    {
        a = a1;
    }
    public int sub(int x, int y)
    {
        return (x - y);
    }
}


When we create the object of Pqr class then first it will call Pqr class constructor but Pqr class constructor first initialize the base class constructor then Pqr constructor will be initialized. It is very important point to note down the base class constructor initialized first.

In this tutorial, we will learn about the Order of Constructor Call with Inheritance in C++. If you are not familiar with the Constructor in C++, you can learn about it from C++ Constructors tutorial.

In what order are the class constructor called when a derived class object is created?

Base class Default Constructor in Derived class Constructors:

When we derive a class from the base class then all the data members of the base class will become a member of the derived class. We use the constructor to initialize the data members and here the obvious case is when the data is inherited into the derived class who will be responsible to initialize them? To initialize the inherited data membres constructor is necessary and that's why the constructor of the base class is called first. In the program given below, we can see the sequence of execution of constructors in inheritance is given below:

#include 
using namespace std;
class Base
{
   int x;

public:
   // default constructor
   Base()
   {
      cout << "Base default constructor\n";
   }
};

class Derived : public Base
{
   int y;

public:
   // default constructor
   Derived()
   {
      cout << "Derived default constructor\n";
   }
   // parameterized constructor
   Derived(int i)
   {
      cout << "Derived parameterized constructor\n";
   }
};

int main()
{
   Base b;
   Derived d1;
   Derived d2(10);
}


Base default constructor
Base default constructor
Derived default constructor
Base default constructor
Derived parameterized constructor

Base class Parameterized Constructor in Derived class Constructor:

Let's see how we can call the parameterized constructor in the Derived class, We need to explicitly define the calling for the parameterized constructor of the derived class using : operator while defining the parameterized constructor in a derived class.

#include 
using namespace std;
class Base
{ 
    int x;
    public:
    // parameterized constructor
    Base(int i)
    { 
        x = i;
        cout << "Base Parameterized Constructor\n";
    }
};

class Derived: public Base
{ 
    int y;
    public:
    // parameterized constructor
    Derived(int j):Base(j)
    { 
        y = j;
        cout << "Derived Parameterized Constructor\n";
    }
};

int main()
{
    Derived d(10) ;
}


Base Parameterized Constructor
Derived Parameterized Constructor

Here are some basic rules to figure out the Order of Constructor Call with Inheritance in C++.

  • Construction always starts with the base class. If there are multiple base classes then, construction starts with the leftmost base. If there is a virtual inheritance then it's given higher preference).

  • Then the member fields are constructed. They are initialized in the order they are declared

  • Finally, the class itself is constructed

  • The order of the destructor is exactly the reverse

Related Tutorials:

  • OOPS Basic Concepts

  • Introduction to C++

  • Functions in C++

  • References in C++



In what order are constructors called?

When a class object is created using constructors, the execution order of constructors is: Constructors of Virtual base classes are executed, in the order that they appear in the base list. Constructors of nonvirtual base classes are executed, in the declaration order.

In what order are constructor called in a class hierarchy?

The answer is that in a class hierarchy, constructors are called in order of derivation, from superclass to subclass.

What is the order of constructor call when the object of derived class B is declared?

What is the order of Constructors call when the object of derived class B is declared, provided class B is derived from class A? Explanation: Firstly the Constructor of class A is called then class B because the Constructor of the base class is called before derived class.

In what order are class constructors and class destructors called when a derived class object is created and deleted?

Destructors are called in reversed order of called constructors.