MULTIPATH INHERITANCE:
If
a class is derived from one or more derived classes That are derivd from the
same base class, such type of inheritance is called as multi-path inheritance.
NOTE:multi-path inheritance is also called as diamond shaped inheritance.
Example 8:
class A
{
public:
int a;
};
class B: public A
{
public:
int b;
};
class C: public A
{
public:
int c;
};
class D: public B, public C
{
public:
int d;
};
From
the above example we can conclude that class B, class C are derived from class
A and class D is derived from class B and class D
1. During the compilation time the compiler comes to a state of
ambiguity because when class D access variable ‘a’ of class A ,the compiler
gives an error message because the compiler is in an ambiguous state in
selecting a variable, whether to select it from class B or class C since both
of them have same copy of A.
2. The ambiguity in multi-path inheritance can be resolved by
using the keyword “virtual”
No comments:
Post a Comment