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.


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).


For Example:- 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.


For Example:- Constructor & Destructor in Multiple inheritance
#include<iostream>
using namespace std;

class baseClass {
 public:
  baseClass1() {
   cout<<"I am baseClass1 constructor"<<endl;
  }
  
  ~baseClass1() {
   cout<<"I am baseClass1 destructor"<<endl;
  }
 };
 
 class baseClass2 {
  public:
   baseClass2() {
    cout<<"I am baseClass2 constructor"<<endl;
   }
   
   ~baseClass2() {
    cout<<"I am baseClass2 destructor"<<endl;
   }
  };
  
 class derivedClass: public baseClass1, public baseClass2 {
  public:
   derivedClass() {
    cout<<"I am derivedClass constructor"<<endl;
   }
   
   ~derivedClass() {'
    cout<<"I am derivedClass destructor"<<endl;
   }
 };
 
 
int main() {
 derivedClass D;
 return 0;
}



<< Previous                                                                                      Next >>
Previous
Next Post »