Multi-Level Inheritance in C++

The mechanism of deriving a class from another derived class is known as multi-level inheritance in C++

It is not uncommon that a class is derived from another derived class as shown below:Multilevel Inheritance in C++

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 since it provides a link for the inheritance between A and C. The chain A -> B -> C is known as inheritance path.

A derived class with multi-level inheritance is declared as follows:

class A      //Base Class
   {
      ...
   };

class B : public A      //B derived from A
   {
      ...
   };

class C : public B      //C derived from B
   {
      ...
   };

This process can be extended to any number of levels.

Let us consider the below example:
Assume that the test results of a batch of students are stored in three different classes. class student stores the roll-number, class test stores the marks obtained in two subjects and class result contains the total marks obtained in the test. The class result can inherit the details of the marks obtained in the test and the roll-number of the students through multilevel inheritance.

class student
   {
      protected:
         int roll_number;
      public:
         void get_number(int);
         void put_number(void);
   };

void student :: get_number(int a)
   {
      roll_number = a;
   }

void student :: put_number()
   {
      cout << "Roll Number: " << roll_number << endl;
   }

class test : public student      //First level derivation
   {
      protected:
         float sub1;
         float sub2;
      public:
         void get_marks(float, float);
         void put_marks(void);
   };

void test :: get_marks(float x, float y)
   {
      sub1 = x;
      sub2 = y;
   }

void test :: put_marks()
   {
      cout << "Marks in Sub1 = " << sub1 << endl;
      cout << "Marks in Sub2 = " << sub2 << endl;
   }

class result : public test   //second Level Derivation
   {
         float total;      //private by default
      public:
         void display();
   };

The last class result in the above example, after inheritance from B to C, would contain the following members:

private:
   float total;      //own member
protected:
   int roll_number;   //inherited from student via test
   float sub1;        //inherited from test
   float sub;        //inherited from test
public:
   void get_number(int);         //from student via test
   void put_number(void);        //from student via test
   void get_marks(float, float); //from test
   void put_marks(void);         //from test
   void display(void);           //own member

The inherited functions put_numbers() and put_marks() can be used in the definition of display() function as follows:

void result :: display(void)
   {
      total = sub1 + sub2;
      put_number();
      put_marks();
      cout << "Total = " << total << endl;
   }

Here is a simple main() program based on the above defined class result:

void main()
   {
      result student;      //student1 object created
      
      student1.get_number(786);
      student1.get_marks(71.5, 99.5);

      student1.display();   //display the result of student1
   }

You may also like...

3 Responses

  1. Charle Madinga says:

    I am new to C++so bear with me: does anybody how to output the (shortest) inheritance path between classes for example in the following multi-level inheritance:
    John
    |
    —————————–
    | |
    James Jill
    |
    |——————-|
    Susan Edward

    And you want to output the inheritance path: Susan->James->John <-Jill

    An example code would be be very helpful !!!

  2. Charle Madinga says:

    My apologies the hierarchy did not come out right. John is the base class; James and Jill are the derived classes from John and Susan & Edward in turn are derived from James. How do you output for example:
    Susan->James->John <-Jill

  1. July 10, 2015

    […] Multi-Level Inheritance […]

Leave a Reply

Your email address will not be published.

%d bloggers like this: