Constructors and Destructors in Inheritance in C++

Constructors and Destructors in Inheritance

On this page we will discuss about constructors and destructors in inheritance in C++ .When we are using the constructors and destructors in the inheritance, parent class  constructors and destructors are accessible to the child class hence when we create an object for the child class, constructors and destructors of both parent and child class get executed.
Constructors and Destructors in Inheritance

Constructors and Destructors in Inheritance

Let us look at below example to understand what happens –

Constructors and Destructors in Inheritance in C++

C++ program to the sequence of execution of constructor and destructor inheritance

Run
#include <iostream>
using namespace std;

class parent //parent class
{
    public:
    parent() //constructor
    {
        cout << "Parent class Constructor\n";
    }

    ~parent()//destructor
    {
        cout << "Parent class Destructor\n";
    }
};

class child : public parent//child class
{

    public:
    child() //constructor
    {
    cout << "Child class Constructor\n";
    }

    ~ child() //destructor
    {
    cout << "Child class Destructor\n";
    }
};

int main()
{
    //automatically executes both child and parent class
    //constructors and destructors because of inheritance
    child c;

    return 0;
}

Output

Parent class Constructor 
Child class Constructor 
Child class Destructor 
Parent class Destructor

Inheritance in Parametrized Constructor/ Destructor

In the case of the default constructor, it is implicitly accessible from parent to the child class but parameterized constructors are not accessible to the derived class automatically, for this reason, an explicit call has to be made in the child class constructor to access the parameterized constructor of the parent class to the child class using the following syntax

<class_name>:: constructor(arguments)

Example program

Run
#include <iostream>
using namespace std;

class parent
{

    int x;
    public:

    // parameterized constructor
    parent(int i)
    {
        x = i;
        cout << "Parent class Parameterized Constructor\n";
    }
};

class child: public parent
{

    int y;
    public:

    // parameterized constructor
    child(int j) : parent(j)  //Explicitly calling
    {
        y = j;
        cout << "Child class Parameterized Constructor\n";
    }
};

int main()
{
    child c(10);

    return 0;
}

Output

Parent class Parameterized Constructor
Child class Parameterized Constructor

Constructor call in multiple inheritance constructors

class C: public A, public B;

Constructors are called upon the order in which they are inherited
First class A constructors are executed followed by class B constructors, then class C constructors

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription