Constructors & Destructors in Multiple Inheritance in C++

Constructors & Destructors in Multiple Inheritance in C++

In multiple inheritance, where a class inherits from more than one base class, constructors and destructors behave in a specific way to ensure proper initialization and cleanup of the inherited classes.


Constructors in Multiple Inheritance in C++

When a derived class inherits from multiple base classes, the constructors of each base class are called in the order they are specified in the derived class's inheritance list. This order is determined by the sequence in which the base classes are listed after the colon : in the derived class declaration.

Example:

#include <iostream>

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

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

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

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

Output:

Base1 constructor called
Base2 constructor called
Derived constructor called

Let's break it down: 

In this example, the constructors of Base1 and Base2 are called first (in the order of their appearance in the inheritance list), followed by the constructor of Derived.


Destructors in Multiple Inheritance in C++

Destructors in multiple inheritance follow the reverse order of constructors. That is, when an object of a derived class is destroyed, the destructors of the base classes are called in the reverse order of their construction.

Example:

#include <iostream>

class Base1 {
public:
    ~Base1() {
        std::cout << "Base1 destructor called" << std::endl;
    }
};

class Base2 {
public:
    ~Base2() {
        std::cout << "Base2 destructor called" << std::endl;
    }
};

class Derived : public Base1, public Base2 {
public:
    ~Derived() {
        std::cout << "Derived destructor called" << std::endl;
    }
};

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

Output:

Derived destructor called
Base2 destructor called
Base1 destructor called

Let's break it down: Let's break it down:

In this example, the destructor of Derived is called first, followed by the destructors of Base2 and Base1 in the reverse order of their construction. This ensures that resources are released in the correct order, maintaining proper cleanup semantics.

Example:
#include<iostream>
using namespace std;

class baseClass1 {
 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;
}

Output:

I am baseClass1 constructor
I am baseClass2 constructor
I am derivedClass constructor
I am derivedClass destructor
I am baseClass2 destructor
I am baseClass1 destructor
Previous
Next Post »