1: // TIME5.CPP 2: // Implementation file for a simple TimeType class. 3: // This version also includes two constructors, "accessor functions", 4: // "friend functions" and "overloaded operators". 6: #include <iostream> 7: using namespace std; 9: #include "TIME5.H" 11: // Private data members of the TimeType class: 12: // int hours; 13: // int minutes; 14: // int seconds; 17: //****************************************************************** 18: TimeType::TimeType(/* in */ int initialHours, 19: /* in */ int initialMinutes, 20: /* in */ int initialSeconds) 21: // Constructor 22: // Pre: 0 <= initialHours <= 23 and 23: /// 0 <= initialMinutes <= 59 and 24: // 0 <= initialSeconds <= 59 25: // Post: Class object is constructed 26: // and the time is set according to the incoming parameters. 27: { 28: hours = initialHours; 29: minutes = initialMinutes; 30: seconds = initialSeconds; 31: } 34: //****************************************************************** 35: void TimeType::Set(/* in */ int newHours, 36: /* in */ int newMinutes, 37: /* in */ int newSeconds) 38: // Pre: 0 <= newHours <= 23 39: // 0 <= newMinutes <= 59 40: // 0 <= newSeconds <= 59 41: // Post: Time is set according to the incoming parameters. 42: { 43: hours = newHours; 44: minutes = newMinutes; 45: seconds = newSeconds; 46: } 49: //****************************************************************** 50: void TimeType::Increment() 51: // Pre: none 52: // Post: Time has been advanced by one second, with 53: // 23:59:59 wrapping around to 0:0:0. 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: } 71: //****************************************************************** 72: int TimeType::Hours() const 73: // Pre: none 74: // Post: Value of data member "hours" is returned. 75: { 76: return hours; 77: } 80: //****************************************************************** 81: int TimeType::Minutes() const 82: // Pre: none 83: // Post: Value of data member "minutes" is returned. 84: { 85: return minutes; 86: } 89: //****************************************************************** 90: int TimeType::Seconds() const 91: // Pre: none 92: // Post: Value of data member "seconds" is returned. 93: { 94: return seconds; 95: } 99: //****************************************************************** 100: TimeType TimeType::operator+(/* in */ const TimeType& otherTime) const 101: // Pre: self and otherTime have been initialized. 102: // Post: Value returned is equivalent self advanced by "otherTime". 103: { 104: int hrs; 105: int mins; 106: int secs; 108: TimeType t; 110: secs = seconds + otherTime.seconds; 111: t.seconds = secs % 60; 112: mins = minutes + otherTime.minutes + secs / 60; 113: t.minutes = mins % 60; 114: hrs = hours + otherTime.hours + mins / 60; 115: t.hours = hrs % 24; 117: return t; 118: } 121: //****************************************************************** 122: bool TimeType::operator==(/* in */ const TimeType& otherTime) const 123: // Pre: none 124: // Post: Function value returned is 125: // - true, if this time equals otherTime 126: // - false, otherwise 127: { 128: return (hours == otherTime.hours && 129: minutes == otherTime.minutes && 130: seconds == otherTime.seconds); 131: } 134: //****************************************************************** 135: bool TimeType::operator<(/* in */ const TimeType& otherTime) const 136: // Pre: This time and otherTime represent times in the same day. 137: // Post: Function value returned is 138: // - true, if this time is earlier in the day than otherTime 139: // - false, otherwise 140: { 141: return ( (hours < otherTime.hours) 142: || 143: (hours == otherTime.hours && 144: minutes < otherTime.minutes) 145: || 146: (hours == otherTime.hours && 147: minutes == otherTime.minutes && 148: seconds < otherTime.seconds) ); 149: } 152: //****************************************************************** 153: ostream& operator<<(/* in */ ostream& outputStream, 154: /* in */ const TimeType& t) 155: // Pre: none 156: // Post: Time t has been output in the form HH:MM:SS. 157: { 158: if (t.hours < 10) 159: outputStream << '0'; 160: outputStream << t.hours << ':'; 161: if (t.minutes < 10) 162: outputStream << '0'; 163: outputStream << t.minutes << ':'; 164: if (t.seconds < 10) 165: outputStream << '0'; 166: outputStream << t.seconds; 168: return outputStream; 169: }