// timeret.cpp // a class that models a time data type // member function to adds times, returns time value #include #include // for setw(), etc. #include //for getche() class airtime { private: int minutes; // 0 to 59 int hours; // 0 t0 23 public: void set() { char dummy; // for colon cout << "Enter time (format 23:59): "; cin >> hours >> dummy >> minutes; } void display() { cout << hours << ':' << setfill('0') << setw(2) << minutes; } airtime add(airtime at2) { airtime temp; temp.minutes = minutes + at2.minutes; // add minutes temp.hours = hours + at2.hours; // add hours if(temp.minutes > 59) // if carry, { temp.minutes = temp.minutes - 60; // adjust minutes temp.hours = temp.hours + 1; // and hours } if(temp.hours > 23) // if carry, temp.hours = temp.hours - 24; // adjust hours return temp; } }; // class airtime void main() { airtime t1, t2, t3; // create three airtime variables char choice; do { cout << "For t1, "; t1.set(); // set t1 cout << "For t2, "; t2.set(); // set t2 t3 = t1.add(t2); // add t1 and t2, result in t3 cout << "t3 = "; t3.display(); // display t3 cout << "\nDo another (y/n)? "; cin >> choice; } while(choice != 'n'); // stop the flow on the monitor getch(); } //end main