C++ Multilevel Inheritance

Last Updated : 25 Jun, 2026

Multilevel inheritance is a type of inheritance in C++ where a class is derived from another derived class, forming an inheritance chain. The most derived class inherits the accessible members of all classes present in the hierarchy.

  • Allows a class to inherit features indirectly through intermediate classes.
  • Promotes code reuse by extending functionality across multiple inheritance levels.
  • Helps model hierarchical real-world relationships.

Example: if we take Grandfather as a base class then Father is the derived class that has features of Grandfather and then Child is the also derived class that is derived from the sub-class Father which inherits all the features of Father.

inheritence2
Multilevel Inheritance

Syntax

Multilevel inheritance is achieved by deriving a class from another derived class.

class A {
// Base class
};

class B : public A {
// Derived from A
};

class C : public B {
// Derived from B
};

Where:

  • A is the base class.
  • B inherits from A.
  • C inherits from B.
  • C can access all accessible members inherited through the hierarchy.

Working of Multilevel Inheritance

In multilevel inheritance, each derived class acts as a base class for the next level. As the inheritance chain grows, the most derived class gains access to the accessible members inherited through all preceding levels.

  • Features are inherited level by level.
  • The most derived class can use members inherited from all ancestor classes.
  • Accessibility of inherited members depends on the inheritance access specifier.

Example: Accessing Members Across Multiple Levels

C++
#include <bits/stdc++.h>
using namespace std;

class Grandfather {
public:
    void g() {
        cout << "This is a Grandfather class" << endl;
    }
    
};

class Father : public Grandfather {
public:
    void f() {
        cout << "This is Father class" << endl;
    }
};

class Child : public Father {
public:
    void c() {
        cout << "This is Son class" << endl;
    }
};

int main() {
    
    // Creating object of Child class
    Child obj;
    
    // Calling member methods of all classes
    // using Child object
    obj.c();
    obj.Father::f();
    obj.Father::Grandfather::g();
    return 0;
}

Output
This is Son class
This is Father class
This is a Grandfather class

Explanation: The Child class inherits from Father, which itself inherits from Grandfather. Therefore, a Child object can access members belonging to all three classes.

Constructor and Destructor Order

When an object of the most derived class is created, constructors are called from the base class to the derived class. Destructors are called in the reverse order.

  • Constructors execute from top to bottom in the hierarchy.
  • Destructors execute from bottom to top.
  • Ensures proper initialization and cleanup of inherited members.
C++
#include <bits/stdc++.h>
using namespace std;

class Grandfather {
public:
    Grandfather() {
        cout << "This is a Grandfather class"<< endl;
    }
    ~Grandfather() {
        cout << "Grandfather class destroyed" << endl;
    }
};

class Father : public Grandfather {
public:
    Father() {
        cout << "This is Father class"<< endl;
    }
    ~Father() {
        cout << "Father class destroyed" << endl;
    }
};

class Child : public Father {
public:
    Child() {
        cout << "This is Child class" << endl;
    }
    ~Child() {
        cout << "Child class destroyed" << endl;
    }
};

int main() {
    
    // Creating object of sub class will
    // invoke the constructor of base classes.
    Child obj;
    return 0;
}

Output
This is a Grandfather class
This is Father class
This is Child class
Child class destroyed
Father class destroyed
Grandfather class destroyed

Explanation: Constructors execute in the order Grandfather -> Father -> Child, while destructors execute in the reverse order Child -> Father -> Grandfather.

Advantages of Multilevel Inheritance

Multilevel inheritance helps organize classes into hierarchical relationships.

  • Encourages code reuse across multiple levels.
  • Reduces duplication of common functionality.
  • Models real-world relationships naturally.
  • Simplifies maintenance by centralizing common behavior.
  • Improves extensibility by allowing new levels to be added easily.

Limitations of Multilevel Inheritance

Although useful, deep inheritance hierarchies can increase complexity.

  • Makes the class hierarchy harder to understand.
  • Changes in ancestor classes may affect many derived classes.
  • Increases coupling between classes.
  • Deep inheritance chains can become difficult to maintain and debug.
Comment