Program to check why we need the concept of virtual base class when a single variable passed through different paths creating ambiguity.

#include
#include
class first
{
public:
int x;
};
class second:virtual public first
{
public:
void getdata()
{
cout<>x;
}
void display()
{
cout<<"n Entered Value of X by means of second class is: "<<x;
}

};
class third:virtual public first
{
public:
void getdata()
{
cout<>x;
}
void display()
{
cout<<"n Entered Value of X by means of third class is: "<<x;
}

};

class fourth:public second, public third
{
public:
void getdata()
{
second::getdata();
third::getdata();
}
void display()
{
second::display();
third::display();
}
};
void main()
{
class second objs;
class third objt;
class fourth objf;
clrscr();
objs.getdata();
objt.getdata();
objs.display();
objt.display();
objf.getdata();
objf.display();
getch();
}

Leave a reply