Multilevel inheritance in C++ is a scenario whereby a class is derived from a particular class, which is already derived from another class.
Note: To understand inheritance in C++ click on this shot.
The figure below shows the visual representation of multilevel inheritance in C++:
In the code below, we derive a class son
from another class father
which is derived from a parent class grandfather
:
#include <iostream>using namespace std;// creating a parent classclass Grandfather{ // this is the parent classpublic: // this is the access specifiervoid myfunction() {cout<<"Gramdfather is the parent class. \nFather is derived from the Grandfather. \nAnd Son is derived from Father.";}};// deriving a class from the parent classclass Father: public Grandfather {};// deriving anogther class from the derived class aboveclass Son: public Father {};int main(){Son myobject; // this is an object of the classmyobject.myfunction(); // calling the functionreturn 0;}
Grandfather
.public
keyword.myfunction
class.myfunction
function.Father
from the parent class Grandfather
.Son
from the derived class Father
.myobject)
of the class (Son
).(myfunction)
.