|
|
|
|
|
|
|
Figure 15-2
Data and Operations Bound
into a Single Unit |
|
|
|
|
|
|
|
|
class TimeType
{
public:
void Set( int, int, int );
void Increment() ;
void Write() const;
Boolean Equal( TimeType ) const;
Boolean LessThan( TimeType ) const;
private:
int hrs;
int mins;
int secs;
}; |
|
|
|
|
|
|
|
|
(For now, you should ignore the word const appearing in some of the function prototypes. We explain this use of const later in the chapter.) |
|
|
|
|
|
|
|
|
The TimeType class has eight membersfive member functions (Set, Increment, Write, Equal, LessThan) and three member variables (hrs, mins, secs). As you might guess, the three member variables form the concrete data representation for the TimeType ADT. The five member functions correspond to the operations we listed for the TimeType ADT: set the time (to the hours, minutes, and seconds passed as parameters to the Set function), increment the time by one second, print the time, compare two times for equality, and determine if one time is less than another. Although the Equal function compares two TimeType variables for equality, its parameter list has only one parametera TimeType variable. Similarly, the LessThan function has only one parameter, even though it compares two times. We'll see the reason later. |
|
|
|
|
|
|
|
|
Like a struct declaration, the declaration of TimeType creates a data type but does not create variables of the type. Class variables (more often referred to as class objects or class instances) are created by using ordinary variable declarations: |
|
|
|
|
|