// 22.5HV2 Software Engineering II // Unit 3 Exercise 1 #include #include #include "clock.h" Clock::Clock(int h, int m, int s): hour(h), minute(m), second(s) {normalise();} Clock& Clock::operator++(int) { second++; normalise(); return *this; // allows increment expression to be used on rhs of assignment } void Clock::normalise() { minute = minute + second / 60; // change minute if second > 60 hour = hour + minute/60; // change hour if minute > 60 minute = minute%60; // modulo 60 minutes hour = hour%24; // modulo 24 hours second = second%60; // modulo 60 seconds if (second < 0) { second+=60; minute--; } if (minute < 0) { minute+=60; hour--; } if (hour < 0) { hour += 24; } } Clock& Clock::operator--(int) { second--; normalise(); return *this; } ostream& operator<<(ostream& ostr, const Clock& c) { return ostr << setfill('0') << setw(2) << c.hour << ":" << setw(2) << c.minute << ":" << setw(2) << c.second; }