1: // Filename: T4.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. 7: #include <iostream> 8: using namespace std; 11: #include "T4.H" 14: // Private data members of the Time class: 15: // int hours; 16: // int minutes; 17: // int seconds; 20: // This is a "four-in-one" constructor, which may take zero, one, 21: // two, or three parameters. 22: //****************************************************************** 23: Time::Time(/* in */ int hoursInitial, 24: /* in */ int minutesInitial, 25: /* in */ int secondsInitial) 26: // Constructor 27: // Pre: 0 <= hoursInitial <= 23 and 28: /// 0 <= minutesInitial <= 59 and 29: // 0 <= secondsInitial <= 59 30: // Post: Class object is constructed 31: // and the time is set according to the incoming parameters. 32: { 33: hours = hoursInitial; 34: minutes = minutesInitial; 35: seconds = secondsInitial; 36: } 39: // The following five "interface functions" are exactly the same as 40: // the corresponding ones in TIME3.CPP. For brevity we have omitted 41: // comments and pre/post-conditions, which are also the same, as are 42: // the function implementations. 45: //****************************************************************** 46: void Time::Set(int hoursNew, int minutesNew, int secondsNew) 47: { 48: hours = hoursNew; 49: minutes = minutesNew; 50: seconds = secondsNew; 51: } 54: //****************************************************************** 55: void Time::Increment() 56: { 57: seconds++; 58: if (seconds > 59) 59: { 60: seconds = 0; 61: minutes++; 62: if (minutes > 59) 63: { 64: minutes = 0; 65: hours++; 66: if (hours > 23) 67: hours = 0; 68: } 69: } 70: } 73: //****************************************************************** 74: int Time::Hours() const { return hours; } 75: int Time::Minutes() const { return minutes; } 76: int Time::Seconds() const { return seconds; } 80: // The following functions are the definitions of the three overloaded 81: // operators supplied by this implementation of the Time class. 84: //****************************************************************** 85: bool Time::operator==(/* in */ const Time& otherTime) const 86: // Pre: none 87: // Post: Returns true if self equals otherTime and false otherwise. 88: { 89: return (hours == otherTime.hours && 90: minutes == otherTime.minutes && 91: seconds == otherTime.seconds); 92: } 96: //****************************************************************** 97: istream& operator>>(/* inout */ istream& inputStream, 98: /* out */ Time& t) 99: // Pre: The input stream inputStream is open. 100: // Time values in the input stream have the form HH:MM:SS. 101: // Post: Time t has been read in. 102: // inputStream is still open. 103: { 104: char ch; 106: inputStream >> t.hours >> ch 107: >> t.minutes >> ch 108: >> t.seconds; 110: return inputStream; 111: } 115: //****************************************************************** 116: ostream& operator<<(/* inout */ ostream& outputStream, 117: /* in */ const Time& t) 118: // Pre: The output stream outputStream is open. 119: // Post: Time t has been output in the form HH:MM:SS. 120: // outputStream is still open. 121: { 122: if (t.hours < 10) 123: outputStream << '0'; 124: outputStream << t.hours << ':'; 125: if (t.minutes < 10) 126: outputStream << '0'; 127: outputStream << t.minutes << ':'; 128: if (t.seconds < 10) 129: outputStream << '0'; 130: outputStream << t.seconds; 132: return outputStream; 133: } 136: //****************************************************************** 137: Time operator +(/* in */ const Time& t1, 138: /* in */ const Time& t2) 139: // Pre: t1 and t2 have been initialized. 140: // Post: Value returned is equivalent to t1 advanced by t2. 141: { 142: Time t; 144: t.seconds = t1.seconds + t2.seconds; 145: t.seconds = t.seconds % 60; 146: t.minutes = t1.minutes + t2.minutes + t.seconds / 60; 147: t.minutes = t.minutes % 60; 148: t.hours = t1.hours + t2.hours + t.minutes / 60; 149: t.hours = t.hours % 24; 151: return t; 152: }