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 use inheritance?
What does oop mean in snapchat?
Can a destructor be called directly?
Is this job good for future? can do this job post grduate student?
What is destructor example?
Can enum be null?
What is encapsulation c#?
Can we have inheritance without polymorphism?
What do you mean by variable?
Why is oop better than procedural?
I have One image (means a group photo ) how to split the faces only from the image?............ please send the answer nagadurgaraju@gmail.com thanks in advace...
Can static class have constructor?
What type of loop is a for loop?
Why is abstraction used?