VOOZH about

URL: https://www.geeksforgeeks.org/cpp/cpp-multilevel-inheritance/

⇱ C++ Multilevel Inheritance - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C++ Multilevel Inheritance

Last Updated : 23 Jul, 2025

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:

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.


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

Now let's change the access specifier.


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.


Output
This is a Grandfather class
This is Father class
This is Child class
Child class destroyed
Father class destroyed
Grandfather class destroyed
Comment
Article Tags:
Article Tags: