1: // Filename: TIME4.CPP
2: // Purpose: Implementation file corresponding to TIME4.H.
3: // This version adds three overloaded operators and default
4: // parameters in order to combine constructor definitions.
6: #include <iostream>
7: using namespace std;
9: #include "TIME4.H"
12: // Private data members of the Time class:
13: // int hours;
14: // int minutes;
15: // int seconds;
18: // This is a "four-in-one" constructor, which may take zero, one,
19: // two, or three parameters.
20: //******************************************************************
21: Time::Time(/* in */ int hoursInitial,
22: /* in */ int minutesInitial,
23: /* in */ int secondsInitial)
24: // Constructor
25: // Pre: 0 <= hoursInitial <= 23 and
26: /// 0 <= minutesInitial <= 59 and
27: // 0 <= secondsInitial <= 59
28: // Post: Class object is constructed
29: // and the time is set according to the incoming parameters.
30: {
31: hours = hoursInitial;
32: minutes = minutesInitial;
33: seconds = secondsInitial;
34: }
37: // The following five "interface functions" are exactly the same as
38: // the corresponding ones in TIME3.CPP. For brevity we have omitted
39: // comments and pre/post-conditions, which are also the same, as are
40: // the function implementations.
43: //******************************************************************
44: void Time::Set(int hoursNew, int minutesNew, int secondsNew)
45: {
46: hours = hoursNew;
47: minutes = minutesNew;
48: seconds = secondsNew;
49: }
52: //******************************************************************
53: void Time::Increment()
54: {
55: seconds++;
56: if (seconds > 59)
57: {
58: seconds = 0;
59: minutes++;
60: if (minutes > 59)
61: {
62: minutes = 0;
63: hours++;
64: if (hours > 23)
65: hours = 0;
66: }
67: }
68: }
70: //******************************************************************
71: int Time::Hours() const { return hours; }
72: int Time::Minutes() const { return minutes; }
73: int Time::Seconds() const { return seconds; }
77: // The following functions are the definitions of the four overloaded
78: // operators supplied by this implementation of the Time class.
80: //******************************************************************
81: Time Time::operator+(/* in */ const Time& otherTime) const
82: // Pre: self and otherTime have been initialized.
83: // Post: Value returned is equivalent to self advanced by otherTime.
84: {
85: int hrs;
86: int mins;
87: int secs;
89: Time t;
91: secs = seconds + otherTime.seconds;
92: t.seconds = secs % 60;
93: mins = minutes + otherTime.minutes + secs / 60;
94: t.minutes = mins % 60;
95: hrs = hours + otherTime.hours + mins / 60;
96: t.hours = hrs % 24;
98: return t;
99: }
103: //******************************************************************
104: bool Time::operator==(/* in */ const Time& otherTime) const
105: // Pre: none
106: // Post: Returns true if self equals otherTime and false otherwise.
107: {
108: return (hours == otherTime.hours &&
109: minutes == otherTime.minutes &&
110: seconds == otherTime.seconds);
111: }
114: //******************************************************************
115: istream& operator>>(/* inout */ istream& inputStream,
116: /* out */ Time& t)
117: // Pre: The input stream inputStream is open.
118: // Time values in the input stream have the form HH:MM:SS.
119: // Post: Time t has been read in.
120: // inputStream is still open.
121: {
122: char ch;
124: inputStream >> t.hours >> ch
125: >> t.minutes >> ch
126: >> t.seconds;
128: return inputStream;
129: }
132: //******************************************************************
133: ostream& operator<<(/* inout */ ostream& outputStream,
134: /* in */ const Time& t)
135: // Pre: The output stream outputStream is open.
136: // Post: Time t has been output in the form HH:MM:SS.
137: // outputStream is still open.
138: {
139: if (t.hours < 10)
140: outputStream << '0';
141: outputStream << t.hours << ':';
142: if (t.minutes < 10)
143: outputStream << '0';
144: outputStream << t.minutes << ':';
145: if (t.seconds < 10)
146: outputStream << '0';
147: outputStream << t.seconds;
149: return outputStream;
150: }