// Program to create class to represent time in hh:mm:ss format. // Assignment-15 // timhms.cpp, Author - Krishna Kumar Khatri. // Uncomment ONLY the following line if you are using Turbo/Borland compiler. // #define TC #ifdef TC #include #else #include using namespace std; #endif #include // For getch. class Time { int hh, mm, ss; public: Time(int h = 0, int m = 0, int s = 0) : hh(h), mm(m), ss(s) { } void display(); void add(const Time&, const Time&); }; void Time::display() { hh < 10 ? cout << "0" << hh : cout << hh; cout << ":"; mm < 10 ? cout << "0" << mm : cout << mm; cout << ":"; ss < 10 ? cout << "0" << ss : cout << ss; } void Time::add(const Time& t1, const Time& t2) { (t1.hh + t2.hh > 12) ? hh = t1.hh + t2.hh - 12 : hh = t1.hh + t2.hh; if (t1.mm + t2.mm > 60) { hh++; mm = t1.mm + t2.mm - 60; } else mm = t1.mm + t2.mm; if (t1.ss + t2.ss > 60) { mm++; ss = t1.ss + t2.ss - 60; } else ss = t1.ss + t2.ss; } int main() { #ifdef TC clrscr(); #endif Time tm1(3, 42, 38); Time tm2(5, 32, 31); Time tm3; cout << "First time - "; tm1.display(); cout << endl << "Second time - "; tm2.display(); tm3.add(tm1, tm2); cout << endl << "Summed up - "; tm3.display(); cout << endl << endl << "Program over. Press any key to exit..."; getch(); return 0; // Successful termination. }