AIM: Write a C++ program to show virtual base class.
An ambiguity can arise when several paths exist to a class from the same base class. This means that a child class could have duplicate sets of members inherited from a single base class.
C++ solves this issue by introducing a virtual base class. When a class is made virtual, necessary care is taken so that the duplication is avoided regardless of the number of paths that exist to the child class.
When two or more objects are derived from a common base class, we can prevent multiple copies of the base class being present in an object derived from those objects by declaring the base class as virtual when it is being inherited. Such a base class is known as virtual base class. This can be achieved by preceding the base class’ name with the word virtual.
using namespace std;
class A2: public virtual A1
class A3:public virtual A1
class A4:public A2,A3
cout<<endl<<“Enter values for a1, a2, a3, a4:”<<endl;
cout<<“a1= “<<a1<<endl<<“a2= “<<a2<<endl<<“a3= “<<a3<<endl<<“a4= “<<a4<<endl;
Enter values for a1, a2, a3, a4:
85 963 942 1234