< previous page page_924 next page >

Page 924
This is the second time we've seen the unusual notationthe constructor initializerinserted between the formal parameter list and the body of a constructor. The first time was when we implemented the parameterized ExtTime class constructor (Figure 16-6). There, we used the constructor initializer to pass some of the incoming parameters to the base class constructor. Here, we use a constructor initializer to pass some of the parameters to a member object's (timeStamp's) constructor. Whether you are using inheritance or composition, the purpose of a constructor initializer is the same: to pass parameters to another constructor. The only difference is the following: With inheritance, you specify the name of the base class prior to the actual parameter list:
ExtTime::ExtTime( /* in */ int      initHrs,
                  /* in */ int      initMins,
                  /* in */ int      initSecs,
                  /* in */ ZoneType initZone )

    : Time(initHrs, initMins, initSecs)
With composition, you specify the name of the member object prior to the actual parameter list:
TimeCard::TimeCard( /* in */ long idNum,
                    /* in */ int initHrs,
                    /* in */ int initMins,
                    /* in */ int initSecs )

    : timeStamp(initHrs, initMins, initSecs)
Furthermore, if a class has several members that are objects of classes with parameterized constructors, you form a list of constructor initializers separated by commas:
SomeClass:: SomeClass (  )

    : memberObject1(param1, param2), memberObject2(param3)
Having discussed both inheritance and composition, we can give a complete description of the order in which constructors are executed:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Given a class X, if X is a derived class, its base class constructor is executed first. Next, constructors for member objects (if any) are executed. Finally, the body of X's constructor is executed.

 
< previous page page_924 next page >