Loading....
Coupon Accepted Successfully!

 

Multipath Inheritance

When a class is derived from two or more classes, those are derived from the same base class. Such a type of inheritance is known as multipath inheritance. The multipath inheritance also consists of many types of inheritance, such as multiple, multilevel, and hierarchical, as shown in Figure.
 
​​
Fig: Ambiguity in classes

 

Consider the following example:

class A1

{

protected:

int a1;

};

class A2 : public A1

{

protected:

int a2;

};

class A3: public A1

{

protected: int a3;

};

class A4: public A2,A3

{

int a4;

};


In the above example, classes
A2 and A3 are derived from class A1; that is, their base class is similar to class A1 (hierarchical inheritance). Both classes A2 and A3 can access the variable a1 of class A1. The class A4 is derived from classes A2 and A3 by multiple inheritance. If we try to access the variable a1 of class A1, the compiler shows error messages as given below.
 

(a) Error PRG58.CPP 30: Member is ambiguous: ‘A1::a1’ and ‘A1::a1’

(b) Error PRG58.CPP 37: Member is ambiguous: ‘A1::a1’ and ‘A1::a1’


In the above example, we can observe all types of inheritance, that is,
multiple, multilevel, and hierarchical. The derived class A4 has two sets of data members of class A1 through the middle base classes A2 and A3. The class A1 is inherited twice.





Test Your Skills Now!
Take a Quiz now
Reviewer Name