|
|
|
|
|
|
|
Deriving One Class from Another |
|
|
|
|
|
|
|
|
Suppose that someone has already written a Time class with the following specification, abbreviated by omitting the preconditions and postconditions: |
|
|
|
|
|
|
|
|
class Time
{
public:
void Set( /* in */ int hours,
/* in */ int minutes,
/* in */ int seconds );
void Increment();
void Write() const;
Time( /* in */ int initHrs, // Constructor
/* in */ int initMins,
/* in */ int initSecs );
Time(); // Default constructor,
private: // setting time to 0:0:0
int hrs;
int mins;
int secs;
}; |
|
|
|
|
|
|
|
|
This class is the same as our TimeType class of Chapter 15, simplified by omitting the Equal and LessThan member functions. Figure 16-4 displays a class interface diagram for the Time class. The public interface, shown as ovals in the side of the large circle, consists of the operations available to client code. The private data items shown in the interior are inaccessible to clients. |
|
|
|
|
|
|
|
|
Suppose we want to modify the Time class by adding, as private data, a variable of an enumeration type indicating the (American) time zoneEST for Eastern Standard Time, CST for Central Standard Time, MST for Mountain Standard Time, PST for Pacific Standard Time, EDT for Eastern Daylight Time, CDT for Central Daylight Time, MDT for Mountain Daylight Time, or PDT for Pacific Daylight Time. We'll need to modify the Set function and the class constructors to accommodate a time zone value. And the Write function should print the time in the form |
|
|
|
|
|
|
|
|
The Increment function, which advances the time by one second, does not need to be changed. |
|
|
|
|
|
|
|
|
To add these time-zone features to the Time class, the conventional approach would be to obtain the source code found in the time.cpp implementation file, analyze in detail how the class is implemented, then modify and recompile the source code. This process has several drawbacks. If Time is an |
|
|
|
|
|