Wednesday, April 6, 2016

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 classsince 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 classresult 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:

0 comments:

Post a Comment