Virtual class in C++ by R4R Team

Virtual Base class:

Virtual base class is used in situation where a derived have multiple copies of base class.

program:

#include< iostream.h> #include< conio.h>
class ClassA
{
public:
int a;
};
class ClassB : virtual public ClassA
{
public:
int b;
};
class ClassC : virtual public ClassA
{
public:
int c;
};
class ClassD : public ClassB, public ClassC
{
public:
int d;
};
void main()
{
ClassD obj;
obj.a = 10;//Statement 1
obj.a = 100; //Statement 2
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout<< "n A : "<< obj.a;
cout<< "n B : "<< obj.b;
cout<< "n C : "<< obj.c;
cout<< "n D : "<< obj.d;
}


output-

A : 100
B : 20
C : 30
D : 40




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!