Answer Posted / jyoti bajaj
virtual keyword can be used with base classes as well as
with functions.Here we are talking about virtual base
classes.
class A
{
public:
A(){cout<<"it is class A"<<endl;}
};
class B:public A
{
public:
B(){cout<<"it is class B"<<endl;}
};
class C:public A
{
public:
C(){cout<<"it is class C"<<endl;}
};
class D:public B,public C
{
public:
D(){}
};
void main()
{
D obj;
}
output:
it is class A
it is class B
it is class A
it is class C
since class A is constructed twice.but if we want that only
one copy of class A should be shared by both classes A &
B.so we inherit the base class by using virtual keyword.
class A
{
public:
A(){cout<<"it is class A"<<endl;}
};
class B:virtual public A
{
public:
B(){cout<<"it is class B"<<endl;}
};
class C:virtual public A
{
public:
C(){cout<<"it is class C"<<endl;}
};
class D:public B,public C
{
public:
D(){}
};
void main()
{
D obj;
}
output:
it is class A
it is class B
it is class C
Is This Answer Correct ? | 5 Yes | 0 No |
Post New Answer View All Answers
What are the three parts of a simple empty class?
Why do we need oop?
What is the fundamental idea of oop?
What is constructor overloading in oop?
Can we have inheritance without polymorphism?
What is a null tree?
What is an advantage of polymorphism?
What is difference between pop and oop?
What is the use of oops?
What are the 3 principles of oop?
What is object and class in oops?
What is the difference between abstraction and polymorphism?
i got a backdoor offer in process global,Bangalore..Can i work with it?
how to get the oracle certification? send me the answer
What is oops and why we use oops?