Multilevel Inheritance in C++

Inheritance in C++ is one of the key features of Object-oriented programming. It allows user to create a new class (derived class) from an existing class(base class). This makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. An existing class that is “parent” of a new class is called a base class. New class that inherits properties of the base class is called a derived class. The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

Multilevel Inheritance in C++

In this type of inheritance the derived class inherits from a class, which in turn inherits from some other class. The Super class for one, is sub class for the other. When a class is derived from a class which is also derived from another class, i.e. a class having more than one parent classes, such inheritance is called Multilevel Inheritance. The level of inheritance can be extended to any number of level depending upon the relation. Multilevel inheritance is similar to relation between grandfather, father and child.

multilevel inheritance in c++

Syntax of Multilevel Inheritance in C++
class base_classname
{
    properties;
    methods;
};

class intermediate_classname:visibility_mode base_classname
{
    properties;
    methods;
};

class child_classname:visibility_mode intermediate_classname
{
    properties;
    methods;
};
Example of Multilevel Inheritance in C++

Run Online

#include <iostream>
using namespace std;
 
//Base Class : class A
class A
{
    private:
        int a;
    public:
        void get_a(int val_a)
        {
            a=val_a;
        }
         
        void disp_a(void)
        {
            cout << "Value of a: " << a << endl;
        }
};
 
//Here Class B is base class for class C
//and Derived class for class A
class B: public A
{
    private:
        int b;
    public:
        //assign value of a from here
        void get_b(int val_a, int val_b)
        {
            //assign value of a by calling function of class A
            get_a(val_a);   
            b=val_b;
        }
         
        void disp_b(void)
        {
            //display value of a
            disp_a();
            cout << "Value of b: " << b << endl;
        }
};
 
//Here class C is derived class and B is Base class
class C: public B
{
    private:
        int c;
    public:
        //assign value of a from here
        void get_c(int val_a, int val_b,int val_c)
        {
            /*** Multilevel Inheritance ***/
            //assign value of a, bby calling function of class B and Class A
            //here Class A is inherited on Class B, and Class B in inherited on Class B
            get_b(val_a,val_b); 
            c=val_c;
        }
         
        void disp_c(void)
        {
            //display value of a and b using disp_b()
            disp_b();
            cout << "Value of c: " << c << endl;
        }
};
 
int main()
{
    //create object of final class, which is Class C
    C objC;
     
    objC.get_c(10,20,30);
    objC.disp_c();
     
    return 0;
}
Output
Total Objects: 1
Value of a: 10
Value of b: 20
Value of c: 30
Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *