#include #include #include //for the functions time and localtime using namespace std; class Time { public: //Declarations for the data members: int hour; //in the range 0 to 23 inclusive int minute; //in the range 0 to 59 inclusive int second; //in the range 0 to 59 inclusive //Declarations for the member functions: void print() const; //const member function void next(int n); //non-const member function void next(); }; int main() { //Output a fixed time. const Time midnight {0, 0, 0}; //hour, minute, second midnight.print(); cout << " is midnight.\n"; //Ask the operating system for the current time. const time_t t {time(nullptr)}; const tm *const p {localtime(&t)}; //Output the current time. const Time now {p->tm_hour, p->tm_min, p->tm_sec}; now.print(); cout << " is the current time.\n"; //Output the time one second from now. Time oneSecLater {now}; //Call the "copy constructor". oneSecLater.next(); //Advance one second. oneSecLater.print(); cout << " is one second later.\n"; //Output the time one minute from now. Time oneMinLater {now}; //Call the "copy constructor". oneMinLater.next(60); oneMinLater.print(); cout << " is one minute later.\n"; return EXIT_SUCCESS; } void Time::print() const //This member function can't change the Time object. { cout << (hour < 10 ? "0" : "") << hour << ":" << (minute < 10 ? "0" : "") << minute << ":" << (second < 10 ? "0" : "") << second; } void Time::next(int n) //This member function can change the Time object. { for (int i {0}; i < n; ++i) { next(); //Call the other next function, the one with no argument } } void Time::next() //Move this Time object one second into the future. { if (++second >= 60) { second = 0; if (++minute >= 60) { minute = 0; if (++hour >= 24) { hour = 0; } } } }