What is a virtual base class?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

why reinterpret cast is considered dangerous?

1898


In which header file does one find isalpha() a) conio.h b) stdio.h c) ctype.h

675


Why null pointer is used?

581


What is an operator function? Describe the function of an operator function?

612


What is polymorphism oop?

619






Explain data encapsulation?

601


What is a storage class used in c++?

610


What is private inheritance?

596


To what does “event-driven” refer?

608


What is a volatile variable in c++?

556


What is a dynamic binding in c++?

528


Difference between overloading vs. Overriding

588


What is meaning of in c++?

670


In a function declaration, what does extern mean?

636


What is overloading unary operator?

595