|
|
|
|
|
|
|
public:
void Punch( /* in */ int hours,
/* in */ int minutes,
/* in */ int seconds );
void Print() const;
.
.
.
TimeCard( /* in */ long idNum,
/* in */ int initHrs,
/* in */ int initMins,
/* in */ int initSecs );
TimeCard() ;
private:
long id;
Time timeStamp;
}; |
|
|
|
|
|
|
|
|
In designing the TimeCard class, you have used composition; a TimeCard object is composed of a Time object (and a long variable). Composition creates a has-a relationshipa TimeCard object has a Time object as a subobject (see Figure 16-7). |
|
|
|
|
|
|
|
|
Implementation of the TimeCard Class |
|
|
|
|
|
|
|
|
The private data of TimeCard consists of a long variable named id and a Time object named timeStamp. The TimeCard member functions can manipulate id by using ordinary built-in operations, but they must manipulate timeStamp through the member functions defined for the Time class. For example, you could implement the Print and Punch functions as follows: |
|
|
|
|
|
|
|
|
void TimeCard::Print() const
{
cout << ID: < id < Time: ;
timeStamp.Write();
}
void TimeCard::Punch( /* in */ int hours,
/* in */ int minutes,
/* in */ int seconds )
{
timeStamp.Set(hours, minutes, seconds);
} |
|
|
|
|
|
|
|
|
Implementing the class constructors is a bit more complicated to describe. Let's start with an implementation of the first constructor shown in the TimeCard class declaration: |
|
|
|
|
|