What is a containership
1 minute read
What is a containership? How does it differ from inheritance?
v Inheritance is the mechanism of deriving certain properties of one class into another.
v C++ supports the new way of inheriting properties of one class into another. This approach takes view that an object can be a collection of many other objects. In this a class can contain the object of the other class as its members.
v This kind of relationship is called the containership or nesting.
v Example: consider the following statements,
Class alpha { . . . .};
Class beta { . . . . };
Class gamma
{
alpha a; //where a is an object of alpha class
beta b; //where b is an object of beta class
. . . . . .
};
v The member objects are created using their respective constructor and then the other ‘ordinary’ members are created.
v Constructors of all the member objects should be called before its own constructor body is executed.