Open In App

C++ Multilevel Inheritance

Last Updated : 26 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Multilevel Inheritance is a type of inheritance in C++ where one class inherits another class, which in turn is derived from another class. It is known as multi-level inheritance as there are more than one level of inheritance.

For 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

The syntax of multilevel inheritance is shown using the simplest example:

C++
// Base class
class A {
    // Class A body
};

// Derived class
class B : access_specifier A {
    // Class B body
} ;

// Derived from derived class B
class C : access_specifier B {
    // Class C body
} ;

Let's understand the characteristics of multilevel inheritance using examples:

Examples

Below example demonstrates the multilevel inheritance in C++:

Example 1: Transitive Nature

The most derived class inherits all the features of all the classes in the hierarchy, but whether they are accessible depends on the access specifier.

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

Now let's change the access specifier.

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

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

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

class Child : private 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

.main.cpp: In function ‘int main()’:
main.cpp:34:9: error: ‘class Father Father::Father’ is inaccessible within this context
34 | obj.Father::f();
| ^~~~~~
main.cpp:12:36: note: declared here
12 | class Father : private Grandfather {
| ^
main.cpp:34:17: error: ‘Father’ is an inaccessible base of ‘Child’
34 | obj.Father::f();
| ^
main.cpp:35:9: error: ‘class Father Father::Father’ is inaccessible within this context
35 | obj.Father::Grandfather::g();
| ^~~~~~
main.cpp:12:36: note: declared here
12 | class Father : private Grandfather {
| ^
main.cpp:35:17: error: ‘class Grandfather Grandfather::Grandfather’ is private within this context
35 | obj.Father::Grandfather::g();
| ^~~~~~~~~~~
main.cpp:12:7: note: declared private here
12 | class Father : private Grandfather {
| ^~~~~~
main.cpp:35:30: error: ‘Grandfather’ is an inaccessible base of ‘Child’
35 | obj.Father::Grandfather::g();
| ^

Example 2: Constructor and Destructor Order

As all the classes are inherited, constructor for these classes is called when the object of the most derived class is created. The order remains from the top to bottom, means from least derived to most derived. The order of destructor is reverse of it.

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

Next Article
Article Tags :
Practice Tags :

Similar Reads