< previous page page_864 next page >

Page 864
ond, the data type of the function is omitted. The reason is that a constructor cannot return a function value. Its purpose is only to initialize a class object's private data.
In the implementation file, the function definitions for the two TimeType constructors might look like the following:
//******************************************************************

TimeType::TimeType( /* in */ int initHrs,
                    /* in */ int initMins,
                    /* in */ int initSecs )
// Constructor

// Precondition:
//     0 <= initHrs <= 23 && 0 <= initMins <= 59
//  && 0 <= initSecs <= 59
// Postcondition:
//     hrs == initHrs && mins == initMins && secs == initSecs

{
    hrs = initHrs;
    mins = initMins;
    secs = initSecs;
}

//******************************************************************

TimeType::TimeType()

// Default constructor

// Postcondition:
//     hrs == 0 && mins == 0 && secs == 0

{
    hrs = 0;
    mins = 0;
    secs = 0;
}
Invoking a Constructor
Although a constructor is a member of a class, it is never invoked using dot notation. A constructor is automatically invoked whenever a class object is created. The declaration
TimeType lectureTime(10, 30, 0);

 
< previous page page_864 next page >