What is the difference between a sub-object and a contained object? For constructing an instance of a class which is derived from another class, first the sub-object has to be created and then the derived object has to be created.
Questions:
-
Is it true that derived object contains the sub-object?
-
How can one visualize the difference in the structure of an object
a) which contains another object
b) which is derived from another class
Do the objects in a) and b) have the same structure ?
Yes, derived object contains instances of its base class subobjects.
Physically there’s really no difference between containing an object (as a member) and deriving from an object. Moreover, the language refers to both as subobjects: base class subobjects and member subobjects.
Virtual inheritance typically introduces some additional household data, which makes it more complicated than just “containing” a base subobject, but ordinary inheritance usually just boils down to plain “containment”.
The difference between deriving and containing as member really exists only on conceptual level. Different access syntax, different features. For example, derivation in involved into such language features as polymorphism.