1 Answers
Best Answer
Syntax
class A // grandparent
{
……
……
};
class B1 : public A //parent 1
{
…….
……
};
class B2 : public A //parent 2
{
……
……
};
class C : public B1,public B2 //child
{
…… //inherits public functions from class A,B1 and B2
};
- The child has two direct base classes parent 1 and parent 2 which themselves have a common base class grandparent.
- The child inherits the traits of grandparent through two separate paths.
- Here the grandparent class is called the indirect base class.
- All the public and protected members are inherited twice each via parent1 and parents.
- This leads to ambiguity and should be avoided.
- Thus the common base class is made as a virtual base class.
syntax
class B1 : public virtual A //parent 1
{
…….
…….
};
class B2 : public virtual A //parent 2
{
……
…….
};