What is a virtual base class?
Answers were Sorted based on User's Feedback
Virtual base class is a base class acts as an indirect base
for more than one without duplication of its data members.
A single copy of its data members is shared by all the base
classes that use it as a virtual base.
For example:
A
/ \
B C
\ /
D
class A { /* ... */ }; // indirect base class
class B : virtual public A { /* ... */ };
class C : virtual public A { /* ... */ };
class D : public B, public C { /* ... */ }; // valid
Using the keyword virtual in this example ensures that an
object of class D inherits only one subobject of class A.
| Is This Answer Correct ? | 75 Yes | 7 No |
Answer / mukesh kumar
To Remove the ambguity problem in multiple inheritance we
make the base class as vitual that means it will make only
one copy its common data member.
EX: suppose we have a base class A , which have a data
member x as integer.
class b: virtual public A
{
};
class C : virtual public A
{
};
class d: public B,public C
{
}
without making classes A and B as virtual class d had two
copies of x.This will arised ambguity problem.
| Is This Answer Correct ? | 38 Yes | 4 No |
Answer / ragib nasir ahmed
A virtual base class is one in which only one copy of the
base class is inherited to the derived class.
Let us consider the classes A,B,C,D
class A
{
int a;
public:
void geta(void)
{
a=10;
}
};
class B: public virtual A
{
int b;
public:
void getb(void)
{
b=20;
}
};
class C:virtual public A
{
int c;
public:
void getc(void)
{
c=30;
}
};
class D:public B,public C
{
public:
void display(void)
{
cout<<a<<b<<c<<d;
}
};
| Is This Answer Correct ? | 25 Yes | 2 No |
Answer / raj randhawa
Suppose you have two derived classes B and C that have a
common base class A, and you also have another class D that
inherits from B and C. You can declare the base class A as
virtual to ensure that B and C share the same subobject of
A.
In the following example, an object of class D has two
distinct subobjects of class L, one through class B1 and
another through class B2. You can use the keyword virtual
in front of the base class specifiers in the base lists of
classes B1 and B2 to indicate that only one subobject of
type L, shared by class B1 and class B2, exists.
For example:
class L { /* ... */ }; // indirect base class
class B1 : virtual public L { /* ... */ };
class B2 : virtual public L { /* ... */ };
class D : public B1, public B2 { /* ... */ }; // valid
Using the keyword virtual in this example ensures that an
object of class D inherits only one subobject of class L.
| Is This Answer Correct ? | 5 Yes | 0 No |
Answer / guessme
Suppose u 've 2 derived class b & c that is a common base
class of a. & u also 've another class d.that is inherit
from b and c.
| Is This Answer Correct ? | 7 Yes | 15 No |
Answer / btech
virtual base class contains one or more virtual functions
| Is This Answer Correct ? | 22 Yes | 46 No |
Explain function prototypes in C++.
What is an abstract class in C++
0 Answers SwanSoft Technologies,
What are the advantages/disadvantages of using inline and const?
Difference between function overloading and function overriding.
What is Boyce Codd Normal form?
When must you use a constructor initializer list?
What is function overloading and operator overloading in C++?
How do you work around them?
How to delete array of objects in C++? Proof by C++ code for proper deletion
In C++ what do you mean by Inheritance?
write a program To generate the Fibonacci Series.
C++ Public access specifier instead of Private – What is bad ?