Virtual Base Classes
A situation may arise where all the three kinds of inheritance, namely, multilevel, multiple and hierarchical inheritance are involved. This is illustrated in the figure below; 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 class via two separate paths. It can also inherit directly, as shown by the dotted lines. The grandparent class is sometimes referred to as indirect base class.

Such inheritance by the child class may create some problems. All the public and protected members of the grandparent class are inherited into the child class twice; first via parent1 class and then again via parent2 class. This means that the child class would have duplicate set of members inherited from grandparent, which introduces ambiguity and should be avoided.
The duplication of inherited members due to these multiple paths can be avoided by making the common base class as virtual base class, while declaring the direct or intermediate base classes.
When a class is made virtual base class, C++ takes necessary care to see that only one copy of that class is inherited, regardless of how many inheritance paths exist between the virtual base class and a derived class.
Example:
Consider a student result processing system.



- Inheritance
Inheritance is what happens when a subclass receives variables or methods from a superclass. It is also when one class passes down its properties to another class. The main reason for inheritance is reusability. Distinguishing a new class from an existing class is called derivation. The old class is referred to as the base class [...]...
- Virtual Functions
Dynamic polymorphism allows programmers to write code using functions applied to objects of the base class without worrying about how those functions will actually be defined by derived classes. For instance, programmers might call functions on a Shape object that computes its area or perimeter, without concerning themselves with the actual (derived) type of the [...]...
- Constructors in Derived Class
A constructor plays a vital role in initializing an object. An important note, while using constructors during inheritance, is that, as long as a base class constructor does not take any arguments, the derived class need not have a constructor function. However, if a base class contains a constructor with one or more arguments, then [...]...
- Classes
A class is an approach to bind the data, describing the entity or an object, and its associated functions together. In C++, a class creates a data-type that is used to create objects of that particular type. Class represents a group of similar objects. Example: class account { private: int accoutno; char type; float balance; [...]...
- VPN (Virtual Private Network)
A traditional private network consists of leased lines connecting multiple sites together. An example would be two offices connected by a point-to-point T-1 line. Connecting multiple sites together over the Internet is usually less expensive than leasing dedicated circuits. One risk in using the Internet as a transport medium is the risk of data interception [...]...




