Virtual Base Class in C++

Virtual Base Class:

It is defined by writing a keyword “virtual” in the derived classes, which is used to prevent multiple instances of a given class from appearing in an inheritance hierarchy when using multiple inheritances. Consider a situation where all three kinds of inheritance namely Multilevel, Multiple, and Hierarchical inheritance are involved.

Multipath Inheritance

In Fig, the child has two direct base classes ‘parent1‘ and ‘parent2‘ which themselves have a common base class ‘grandparent‘. The ‘child‘ inherits the traits of ‘grandparent‘ via two separate paths. It can also inherit directly from the broken line. The ‘grandparent’ is sometimes referred to as an indirect base class. The duplication of inherited members due to these multiple paths can be avoided by making the common base class that is called Virtual Base Class.