Hey i have one questio .....plzz do answer........
i have 3 classes A,B and D

A is base class........

if B is derived from A..

can we derive C from both A and B......??

6 Years
Discussion Span
Last Post by suhasgh

Yes, in a few different ways.

First, if C derives from B, then it is also derived from A, through the fact that B is derived from A. In other words, derived classes inherit from their base class(es) and all their base class(es).

Second, C++ also permits multiple inheritance, meaning that a class can derive from multiple base classes. They are declared as class C : public A, public B { /* .. */ };. However, this can creates some awkward situations in the case for example that B derives from A, then it means that C inherits twice from the base class A. In fact, C will indeed inherit twice from A (in formal jargon, we say that C has two inherited sub-objects of class A).

Finally, to use multiple inheritance while not getting duplicated base classes, you have to use virtual inheritance. That is, if you declare class B : public virtual A { }; and then do class C : public virtual A, public B { };, then the result is that C will inherit only one base-class A (i.e. as opposed to two in the previous method).

Read more about it here.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.