Constructor and Destructor in Derived Class in C++

Constructor and Destructor in Derived Class in C++

We know that constructor is invoked implicitly when an object is created. But do you think what will happen when we create object of derived class? 

1. In inheritance, When an object of derived class is created then constructor of derived class get executed and then it calls the constructor of base class.


2. If there is no default constructor present in parent class then not only we have to create constructor in child class but also we will have to call the constructor of parent class.


3. In order to call parameterized constructor of parent class, we need to create a constructor in child class and also we will have to call parameterized constructor with the help of child class constructor.


Constructors in Derived Class in C++

In C++, constructors in derived classes are special member functions used to initialize objects of derived classes. When you create an object of a derived class, its constructor is called to initialize both the base class part and the derived class part of the object. Constructors in derived classes can call constructors of their base classes explicitly to ensure proper initialization of the inherited members.


Example:

#include <iostream>

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

// Derived class
class Derived : public Base {
public:
    Derived() {
        std::cout << "Derived constructor\n";
    }
};

int main() {
    Derived d; // Creating an object of the derived class
    return 0;
}

Let's break it down:

In this example, when you create an object of the Derived class (Derived d;), its constructor is called. The constructor of the Derived class implicitly calls the constructor of the Base class (Base()), ensuring that both the base class and the derived class are properly initialized.


Destructors in Derived Class in C++

In C++, destructors in derived classes play a crucial role in ensuring proper cleanup of resources allocated by both the base class and the derived class. A destructor in C++ is a special member function of a class that is automatically called when an object of that class goes out of scope or is explicitly deleted. In the context of derived classes, a derived class can have its own destructor in addition to the destructor of its base class.


Example:

#include <iostream>
class Base {
public:
    Base() {
        std::cout << "Base constructor called" << std::endl;
    }

    ~Base() {
        std::cout << "Base destructor called" << std::endl;
    }
};

class Derived : public Base {
public:
    Derived() {
        std::cout << "Derived constructor called" << std::endl;
    }

    ~Derived() {
        std::cout << "Derived destructor called" << std::endl;
    }
};

int main() {
    Derived derivedObj;
    return 0;
}

Let's break it down:

In this example, when the derivedObj goes out of scope at the end of the main() function, the destructors are called in the following order: 

1. Derived destructor

2. Base destructor

This order ensures that resources allocated by Derived are properly released before those allocated by Base.


Syntax of calling parent class constructor using child class constructor 

class A
{
 public:
  A() {
  
   //constructor body
   
  }
 };
 
 class B: public A {
  public:
   B(): A() {
   
   //constructor body
  
  }
 };

For Example:
class A
{
  int a;

public:
  A (int k)   //parameterized constructor of parent class.
  {
    a = k;
  }
};

class B: public A
{
  int b;
  
public:
  B(int x, int y):A(x)  //constructor of child class calling constructor of base class.
  {
    b = y;
  }
};

int main()
{
  B obj(2,3);
}

Points to Remember:

1. In inheritance, the order of constructors calling is: from child class to parent class (child -> parent).


2. In inheritance, the order of constructors execution is: from parent class to child class (parent -> class).

3. In inheritance, the order of destructors calling is: from child class to parent class (child -> parent).


4. In inheritance, the order of destructors execution is: from child class to parent class (child -> parent).


Destructor Example:
#include<iostream>
using namespace std;

class baseClass
{
public:
  baseClass()
  {
    cout << "I am baseClass constructor" << endl;
  }
  
  ~baseClass()
  {
    cout << "I am baseClass destructor" << endl;
  }
};

class derivedClass: public baseClass
{
public:
  derivedClass()
  {
    cout << "I am derivedClass constructor" << endl;
  }
  
  ~derivedClass()
  {
    cout <<" I am derivedClass destructor" << endl;
  }
};

int main()
{
  derivedClass D;
  return 0;
}

Points to Remember:

 
class C: public A, public B
{
//...
};

1. Here, A class in inherited first, so constructor of class A is called first then the constructor of class B will be called next.

2. The destructor of derived class will be called first then destructor of base class which is mentioned in the derived class declaration is called from last towards first sequence wise.

Previous
Next Post »