class Component
{
public:
Component(void);
};
class Whole
{
Component c1, c2;
public:
Whole(void);
};
Component::Component(void)
{
cout << "Component constructor called" << endl;
}
Whole::Whole(void)
{
cout << "Whole constructor called" << endl;
}
What messages do you think is generated when the following code is encountered?
Whole wholeGrain;
When wholeGrain is created, it first invokes the constructors
of its components, c1 and c2. This generates two
messages saying ``Component constructor called''. Next, the
constructor of Whole is called, generating a message
saying ``Whole constructor called''.
In other words, when a constructor of a class is invoked, it can assume that all its components (data members) are initialized by their own constructors.