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:

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.

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

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

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

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. Required fields are marked *