|
|
|
|
|
|
|
includes a parameter list to the right of the name of the class object being declared. When this declaration is encountered at execution time, the first (parameterized) constructor is automatically invoked, initializing the private data of lectureTime to the time 10:30:0. The declaration |
|
|
|
|
|
|
|
|
has no parameter list after the identifier startTime. The default (parameterless) constructor is implicitly invoked, initializing startTime's private data to the time 0:0:0. |
|
|
|
|
|
|
|
|
Remember that a declaration in C++ is a genuine statement and can appear anywhere among executable statements. Placing declarations among executable statements is extremely useful when creating class objects whose initial values are not known until execution time. Here's an example: |
|
|
|
|
|
|
|
|
cout << Enter appointment time in hours, minutes, and seconds: ;
cin >> hours >> minutes >> seconds;
TimeType appointmentTime(hours, minutes, seconds);
cout << The appointment time is ;
appointmentTime.Write();
.
.
. |
|
|
|
|
|
|
|
|
Revised Specification and Implementation Files for TimeType |
|
|
|
|
|
|
|
|
By including constructors for the TimeType class, we are sure that each class object is initialized before any subsequent calls to the class member functions. One of the constructors allows the client code to specify an initial time; the other creates an initial time of 0:0:0 if the client does not specify a time. Because of these constructors, it is impossible for a TimeType object to be in an uninitialized state after it is created. As a result, we can delete from the TimeType specification file the warning to call Set before calling any other member functions. Also, we can remove all of the function preconditions that require Set to be called previously. Here is the revised TimeType specification file: |
|
|
|
|
|
|
|
|
//******************************************************************
// SPECIFICATION FILE (timetype.h)
// This file gives the specification
// of a TimeType abstract data type
//******************************************************************
|
|
|
|
|
|