class TimeType
1: // Filenme: TIMECLAS.CPP
2: // Purpose: Illustrates a simple time class, with the entire
3: // class definition and implementation contained in
4: // the file where it is being used. You should note
5: // for future reference that this is not usual practice.
7: #include <iostream>
9: #include "PAUSE_CPP"
12: class TimeType
13: {
14: public:
15: void Set(/* in */ int newHours,
16: /* in */ int newMinutes,
17: /* in */ int newSeconds);
18: // Pre: 0 <= newHours <= 23
19: // 0 <= newMinutes <= 59
20: // 0 <= newSeconds <= 59
21: // Post: Time is set according to the incoming parameters.
22: // Note that this function MUST have been called prior to
23: // any call to *any* of the other member functions.
25: void Increment();
26: // Pre: The Set function has been invoked at least once.
27: // Post: Time has been advanced by one second, with
28: // 23:59:59 wrapping around to 0:0:0.
30: bool Equal(/* in */ TimeType otherTime) const;
31: // Pre: The Set function has been invoked at least once
32: // for both this time and otherTime.
33: // Post: Function value returned is
34: // - true, if this time equals otherTime
35: // - false, otherwise
37: bool EarlierThan(/* in */ TimeType otherTime) const;
38: // Pre: The Set function has been invoked at least once
39: // for both this time and otherTime.
40: // This time and otherTime represent times in the same day.
41: // Post: Function value returned is
42: // - true, if this time is earlier in the day than otherTime
43: // - false, otherwise
45: void Display() const;
46: // Pre: The Set function has been invoked at least once.
47: // Post: Time has been output in the form HH:MM:SS.
49: private:
50: int hours;
51: int minutes;
52: int seconds;
53: };
54:
55: int main()
56: {
57: cout << endl
58: << "This program illustrates the use "
59: << "of a simple time class. " << endl
60: << "You should study the source code "
61: << "at the same time as the output. " << endl
62: << endl;
64: TimeType t1;
65: TimeType t2;
67: t1.Set(11, 59, 50);
68: t2.Set(12, 0, 0);
70: t1.Display(); cout << endl;
71: t2.Display(); cout << endl;
72: Pause();
74: cout << endl;
75: while (t1.EarlierThan(t2))
76: {
77: t1.Display(); cout << endl;
78: t1.Increment();
79: }
80: Pause();
82: cout << endl;
84: if (t1.Equal(t2))
85: {
86: t1.Display(); cout << endl;
87: }
89: cout << endl;
91: return 0;
92: }
94:
95: //******************************************************************
96: void TimeType::Set(/* in */ int newHours,
97: /* in */ int newMinutes,
98: /* in */ int newSeconds)
99: // Pre: 0 <= newHours <= 23
100: // 0 <= newMinutes <= 59
101: // 0 <= newSeconds <= 59
102: // Post: Time is set according to the incoming parameters.
103: // Note that this function MUST have been called prior to
104: // any call to *any* of the other member functions.
105: {
106: hours = newHours;
107: minutes = newMinutes;
108: seconds = newSeconds;
109: }
112: //******************************************************************
113: void TimeType::Increment()
114: // Pre: The Set function has been invoked at least once.
115: // Post: Time has been advanced by one second, with
116: // 23:59:59 wrapping around to 0:0:0.
117: {
118: seconds++;
119: if (seconds > 59)
120: {
121: seconds = 0;
122: minutes++;
123: if (minutes > 59)
124: {
125: minutes = 0;
126: hours++;
127: if (hours > 23)
128: hours = 0;
129: }
130: }
131: }
134: //******************************************************************
135: bool TimeType::Equal(/* in */ TimeType otherTime) const
136: // Pre: The Set function has been invoked at least once
137: // for both this time and otherTime.
138: // Post: Function value returned is
139: // - true, if this time equals otherTime
140: // - false, otherwise
141: {
142: return (hours == otherTime.hours &&
143: minutes == otherTime.minutes &&
144: seconds == otherTime.seconds);
145: }
147:
148: //******************************************************************
149: bool TimeType::EarlierThan(/* in */ TimeType otherTime) const
150: // Pre: The Set function has been invoked at least once
151: // for both this time and otherTime.
152: // This time and otherTime represent times in the same day.
153: // Post: Function value returned is
154: // - true, if this time is earlier in the day than otherTime
155: // - false, otherwise
156: {
157: return ( (hours < otherTime.hours)
158: ||
159: (hours == otherTime.hours &&
160: minutes < otherTime.minutes)
161: ||
162: (hours == otherTime.hours &&
163: minutes == otherTime.minutes &&
164: seconds < otherTime.seconds) );
165: }
168: //******************************************************************
169: void TimeType::Display() const
170: // Pre: The Set function has been invoked at least once.
171: // Post: Time has been output in the form HH:MM:SS.
172: {
173: if (hours < 10)
174: cout << '0';
175: cout << hours << ':';
176: if (minutes < 10)
177: cout << '0';
178: cout << minutes << ':';
179: if (seconds < 10)
180: cout << '0';
181: cout << seconds;
182: }