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 class class Grandfather{ // this is the parent class public: // this is the access specifier void 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 class class Father: public Grandfather { }; // deriving anogther class from the derived class above class Son: public Father { }; int main(){ Son myobject; // this is an object of the class myobject.myfunction(); // calling the function return 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)
.RELATED TAGS
CONTRIBUTOR
View all Courses