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
Why use of template is better than a base class?
What is a linked list in c++?
What do you mean by global variables?
Is it possible to get the source code back from binary file?
Can you please explain the difference between using macro and inline functions?
What is setw manipulator in c++?
Does c++ have finally?
What is use of overloading?
Is facebook written in c++?
Name four predefined macros.
What is data abstraction? How is it different from data encapsulation?
Write my own zero-argument manipulator that should work same as hex?
How much maximum can you allocate in a single call to malloc()?
Why are pointers not used in c++?
What operator is used to access a struct through a pointer a) >> b) -> c) *