< previous page page_867 next page >

Page 867
private:
    int hrs;
    int mins;
    int secs;
};
To save space, we do not include the revised implementation file here. The only changes are
1. the inclusion of the function definitions for the two class constructors, which we presented earlier.
2. the deletion of all function preconditions stating that the Set function must be invoked previously.
At this point, you may wonder why we need the Set function at all. After all, both the Set function and the parameterized constructor seem to do the same thingset the time according to values passed as parametersand the implementations of the two functions are essentially identical. The difference is that Set can be invoked for a class object whenever and as often as we wish, whereas the parameterized constructor is invoked once onlyat the moment a class object is created. Therefore, we retain the Set function to provide maximum flexibility to clients of the class.
Guidelines for Using Class Constructors
The class is an essential language feature for creating abstract data types in C++. The class mechanism is a powerful design tool, but along with this power come rules for using classes correctly.
C++ has some very intricate rules about using constructors, many of which relate to language features we have not yet discussed. Below are some guidelines that are pertinent at this point.
1. A constructor cannot return a function value, so the function is declared without a return value type.
2. A class may provide several constructors. When a class object is declared, the compiler chooses the appropriate constructor according to the number and data types of the parameters to the constructor.
3. Parameters to a constructor are passed by placing the actual parameter list immediately after the name of the class object being declared:
SomeClass anObject(param1, param2);
4. If a class object is declared without a parameter list, as in the statement
SomeClass anObject;

 
< previous page page_867 next page >