What is Multilevel Inheritance in C++

What is Multilevel Inheritance in C++ image1

Multilevel Inheritance in C++

On this page we will discuss about 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 multilevel inheritance.

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
MultiLevel Inheritance

Example : Program to demonstrate  Multilevel Inheritance

Run

#include <iostream>
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 
};

int 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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription