// timeclass6.cc #include #include using namespace std; class Time { private: int Hours; // 0 to 23 int Minutes; // 0 to 59 public: Time() { Hours = Minutes = 0; } Time(int hrs, int mins) :Hours(hrs),Minutes(mins) { TimeCheck(); } void printTime() { cout << Hours << " hours and " << Minutes << " minutes" << endl; } // do any rollovers necessary on hours or minutes void TimeCheck() { if (Minutes > 59) { Hours += Minutes / 60; Minutes = Minutes % 60; } if (Minutes < 0) { Hours -= (-Minutes / 60) + 1; Minutes = 60 - (-Minutes % 60); } if (Hours > 23) Hours = Hours % 23; if (Hours < 0) Hours = 24 - (-Hours % 24); } bool operator == (const Time &otherTime) { return((Minutes == otherTime.Minutes) && (Hours == otherTime.Hours)); } bool operator != (const Time &otherTime) { return !(this->operator==(otherTime)); } bool operator < (const Time &otherTime) { if (Hours < otherTime.Hours) return(true); else if (Hours > otherTime.Hours) return(false); // by now we know Hour == otherTime.Hours else if (Minutes < otherTime.Minutes) return(true); else return(false); } bool operator <= (const Time &otherTime) { if (this->operator==(otherTime)) return(true); else return(this->operator<(otherTime)); } bool operator > (const Time &otherTime) { return !(this->operator<=(otherTime)); } bool operator >= (const Time &otherTime) { if (this->operator== (otherTime)) return(true); else return(this->operator>(otherTime)); } }; int main() { Time T1(0, 0); cout << "T1(0, 0): "; T1.printTime(); Time T2(2, 5); cout << "T2(2, 5): "; T2.printTime(); cout << "T1 == T2 : " << (T1==T2) << endl; cout << "T1 < T2 : " << (T1 T2 : " << (T1>T2) << endl; cout << "T1 <= T2 : " << (T1<=T2) << endl; cout << "T1 >= T2 : " << (T1>=T2) << endl; }