< previous page page_919 next page >

Page 919
Figure 16-6 (continued)
void ExtTime::Write() const

// Postcondition:
//     Time has been output in the form HH:MM:SS ZZZ
//     where ZZZ is the time zone

{
    static char zoneString[8][4] =
    {
        EST, CST, MST, PST, EDT, CDT, MDT, PDT
    };

    Time::Write();
    cout <<   < zoneString[zone];
}
In the first constructor in Figure 16-6, notice the syntax by which a constructor passes parameters to its base class constructor:
ExtTime::ExtTime( /* in */ int      initHrs,
                  /* in */ int      initMins,
                  /* in */ int      initSecs,
                  /* in */ ZoneType initZone )

    : Time (initHrs, initMins, initSecs)  
¼  Constructor initializer

{
    zone = initZone;
}
After the parameter list to the ExtTime constructor (but before its body), you insert what is called a constructor initializera colon and then the name of the base class along with the actual parameters to its constructor. When an ExtTime object is created with a declaration such as
ExtTime time1(8, 35, 0, PST);
the ExtTime constructor receives four parameters. The first three are simply passed along to the Time class constructor by means of the constructor initializer. After the Time class constructor has executed (creating the base class subobject as shown in Figure 16-5), the body of the ExtTime constructor executes, setting zone equal to the fourth parameter.
The second constructor in Figure 16-6 (the default constructor) does not need a constructor initializer; there are no parameters to pass to the base

 
< previous page page_919 next page >