What is Multilevel Inheritance in C++

Multi level inheritance

Multilevel Inheritance in C++

As we know that Inheritance is the process where a class is Extending the properties  from another class  but it is interesting to know that we can inherit a class that is already inherited which is known as

It is a process where an inherited class is further  made as a parent class to another class 

Syntax to Implement Multilevel Inheritance:

class A

{
//contains Only A class  properties
...;
};
class B: public A//child of A//intermediate base class
{
....;
//contains both  A, B  class properties
};
class C: public B//child of B
{
//contains both A,B ,C class properties

-----;
};
  • The class that is acquiring the behaviors is called child class or derived class or subclass
  • The class from which behaviors are taken is called parent class or superclass or base class.

Here B class contains both the properties of A and B whereas C class contains the properties of B and also inherited properties of B i.e A class and also properties of C class

Example Program to demonstrate  Multilevel Inheritance

#include
using namespace std;
class grandparent//level 1 parent
{
public:
void gp_property()
{
cout<<"\nproperty earned by grand father-Kandhi Buchayya";
}
};

class parent:public grandparent//inheriting grandparent to parent:level 2
{
public:
void p_property()
{
cout<<"\nproperty earned by parent-Kandhi Vijay";
}
//now it conatins 2 methods
};

class child:public parent//level 3 child(powerful fellow)
{
public:
void c_property()
{
cout<<"\n property earned by child itself-Kandhi Trishaank";
}
//It contains 3 methods now 
//enjoys properties of parent+grandparent+its own 
};

main()
{

child c;//containss the properties of 2 classes
c.gp_property();//derived from grand parent
c.p_property();//derived from parent
c.c_property();//its own method

parent p;
p.gp_property();//derived from grand the parent
p.p_property();//its own method

}
Output
property earned by grand father-Kandhi Buchayya
property earned by parent-Kandhi Vijay
 property earned by child itself-Kandhi Trishaank
property earned by grand father-Kandhi Buchayya
property earned by parent-Kandhi Vijay

In this example, class parent contains the capabilities of both parent and grandparent whereas a child has capabilities of both parent and grandparent.

  • Hence child class enjoys 3 methods gp_property(),p_property() and c_property()whereas parent class enjoys 2 methods gp_property() and p_property().
  • Hence class at the lowest level has more capabilities than remaining parent classes