C++ Multilevel Inheritance (With Example Programs)

- March 05, 2018
A class can be derived from another derived class. It is not uncommon that a class is derived from another derived class shown in the figure "Multilevel inheritance".
Working of multilevel inheritance

It’s clear with the above diagram that in
multilevel inheritance there is a concept of relation between grandfather, father and child.
The class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. The class B is known as intermediate base class because it provides a link for the inheritance between A and C. The chain ABC is known as inheritance path.

Declaration

In C++, a derived class with multilevel inheritance can be declared as follows:
      class A  //Base class
          { ---------
             --------- //body of class A
             ---------
          };
      class B : public A //B derived from class A
          {  ----------
              --------- //body of class B
              ----------
          };
      class C : public B //C derived from class B
          {  ----------
              --------- //body of class C
              ----------
          };

//This process can be extended to any number of levels.
The level of inheritance can be extended to any number of level depending upon the program requirements.

Multilevel Inheritance Example Programs

The following simple program written in C++ explains multilevel inheritance.
#include<iostream>
using namespace std;
   class B1
   {
        protected:
                   float m, n;
        public:
                   assign (float x, float y)
        {
               m = x;
               n = y;
        }
    };    //end base class B1
   class B2 : public B1
    {
        public:
        sum()
         {
            cout<<"Sum of "<<m<<"and"<<n<<"="<<m+n<<endl;
         }
    };    //end derived class B2
   class D1 : public B2
    {
        public:
        product()
         {
              cout<<"Product of "<<m<<"and"<<n<<"="<<m*n<<endl;
         }
   };   //end derived class D1

main()
 {
   D1 x;
   x .assign (10, 2);
   x .sum ();
   x .product();
    return 0;
 }
Output of Program:
Sum of 10 and 2 = 12
Product of 10 and 2 = 20

Example Program 2:  The following program prepares clint profile using multilevel inheritance.
#include <iostream>
using namespace std;
class clint
{
    char name[50],gender[15];
    int age;
    public:
        void getdata()
        {
            cout<<"Enter Clint Name: ";
            fflush(stdin);  // to clear input stream
            cin>>name;
            cout<<"Enter Age: ";
            cin>>age;
            cout<<"Enter Gender: ";
            cin>>gender;
        }
        void display()
        {
            cout<<"Name: "<<name<<endl;
            cout<<"Age: "<<age<<endl;
            cout<<"Gender: "<<gender<<endl;
        }
};
class employee: public clint
{
    char company[100];
    float salary;
    public:
        void getdata()
        {
            clint::getdata();
            cout<<"Name of Company: ";
            fflush(stdin);
            cin>>company;
            cout<<"Salary: Rs.";
            cin>>salary;
        }
        void display()
        {
            clint::display();
            cout<<"Name of Company:"<<company<<endl;
            cout<<"Salary:Rs."<<salary<<endl;
        }
};
class programmer: public employee
{
    int number;
    public:
        void getdata()
        {
            employee::getdata();
            cout<<"Number of programming language known: ";
            cin>>number;
        }
        void display()
        {
            employee::display();
            cout<<"Number of programming language known: "<<number;
        }
};

main()
{
    programmer p;
    std:: cout<<"Enter data"<<endl;
    p.getdata();
    cout<<endl<<"Displaying data"<<endl;
    p.display();
    return 0;
}
Output of Program:
Enter Clint Name: Zeshan Ali
Enter Age: 20
Enter Gender: Male
Name of Company: CodeHim
Salary: $1000
Number of programming language known: 6
Displaying data
Name: Zeshan Ali
Age: 20
Gender: Male
Name of Company: CodeHim
Salary: $1000
Number of programming language known: 6
Information about the clint (person) shown in the above output is imaginary and for educational purposes only.