#include #include #include using namespace std; class Time //Class time definition { friend ostream & operator<<(ostream &, Time ); friend Time operator+(Time&, Time&);//Adds two times and returns a new time object // friend Time operator-(Time&, Time&);//Subtracts two times and returns a new time object public: Time(int , int , int );//Constructor. Time(int =0);/*Takes int sec and converts to Time object. Defaults to 0 when nothing is entered. Works also as conversion operator from int (seconds) to time*/ ~Time();//Destructor void setHour(int &ho);//Set hour. void setMinute(int &mi);//Set minute void setSecond(int &se);//Set second void setTime(int & ho, int &mi, int &se);//Set time all at once. int getHour() const;//Returns hour int getMinute() const;//Returns minute int getSecond() const;//Returns second void showTime12() const;//Outputs the time in 12 hour format. void addTime(int &ho, int &mi, int &se);//Adds ho, mi, se to a time object. Time & operator+=(int ); Time & operator+=(Time ); // bool operator==(Time ); Not necessary anymore with the conversion // operator operator int() const; below - all is done implicitly! When the compiler // receives the command t1 == t1 (where t1 and t2 are time objects) // it automatically converts t1 and t2 to seconds using the // conversion operator and performs == in the seconds. // bool operator<(Time );//Not necessary with the conversion // operator below - all is done implicitly as explained above. // bool operator>(Time );//Not necessary with the conversion // operator below - all is done implicitly as explained above. operator int() const;//Conversion operator - automatic conversion from Time to int - No need for toSec() anymore! Time & operator++();//Adds one second to current time - prefix (returns time with second added). Time operator++(int);//Adds one second to current time - postfix (returns time before second was added). private: int hour; int minute; int second; }; /*------------- Definition of the function members of class Time ---------------------*/ Time::Time(int ho, int mi, int se)//Custom constructor :hour(ho),minute(mi),second(se) { setTime(ho, mi, se); } Time::Time(int sec) { if(sec<0) sec += 24*3600; hour = (sec/3600)%24; //Gives number of hours in sec seconds. sec = sec%3600;//Gives number of remaining seconds. minute = sec/60;//Gives number of minutes. second = sec%60;//Remainder seconds } Time::~Time() { } void Time::setHour(int &ho) { if(ho>=0 && ho<24) hour=ho; else hour=0; } void Time::setMinute(int &mi) { if(mi>=0 && mi<60) minute=mi; else minute=0; } void Time::setSecond(int &se) { if(se>= 0 && se<60) second=se; else second=0; } void Time::setTime(int &ho, int &mi, int &se) { setHour(ho); setMinute(mi); setSecond(se); } int Time::getHour() const { return hour; } int Time::getMinute() const { return minute; } int Time::getSecond() const { return second; } void Time::showTime12() const { cout << setfill('0') << setw(2) << ( (hour < 13) ? getHour() : getHour()-12 ) << ":" << setw(2) << getMinute() << ":" << setw(2) << getSecond() << ( (getHour() < 12) ? " AM" : " PM" ) << endl; } void Time::addTime(int &ho, int &mi, int &se) { (*this) = (*this) + Time(ho,mi,se); } Time &Time::operator+=(int sec) { Time addsec = (*this) + sec; (*this) = addsec; return (*this); } Time &Time::operator+=(Time t1) { (*this) = (*this) + t1; return (*this); } Time::operator int() const { return hour*3600 + minute*60 + second; } Time & Time::operator++() { (*this) +=1; return (*this); } Time Time::operator++(int) { Time temp =(*this); (*this)+=1; return temp; } /*---------- End of definitions of class Time function members --------------*/ /*--------------- Definition of external friend functions ---------------*/ Time operator+(Time &t1, Time &t2) { int totsec1=t1; int totsec2=t2; int addsec=totsec1+totsec2; return Time(addsec); } Time operator-(Time &t1, Time &t2) { int totsec1=t1; int totsec2=t2; int addsec=totsec1-totsec2 + 24*3600; return Time(addsec); } ostream &operator<<(ostream & ost, Time ti) { ost << setfill('0') << setw(2) << ti.getHour() << ":" << setw(2) << ti.minute << ":" << setw(2) << ti.second; return ost; } /*-----------------main part of the program---------------------*/ int main() { Time now{12,00,01}; cout < now) gives " << (now2 > now) << endl; cout << "(now2 < now2) gives " << (now2 < now2) << endl; cout << "(now2 <= now2) gives " << (now2 <= now2) << endl << endl; cout << "now + now2 gives " << now + now2 << endl; cout << "now - now2 gives " << now - now2 << endl << endl; cout << "now2 += now gives: "<< (now2 += now) << endl; cout << "New value of \"now2\" is: "<< now2 << endl << endl; cout << "now += 7200 (+ two hours) gives: " << (now2 += 7200) << endl; cout << "New value of \"now\" is: "<< now2 << endl << endl; cout << "Output of \"cout << ++now\" is: " << ++now << endl; cout << "New value of \"now\" is: "<< now << endl << endl; cout << "Output of \"cout << now++\" is: " << now++ << endl; cout << "New value of \"now\" is: "<< now << endl << endl; // cout << "Input a time in the format hh:mm:ss: "; // Time enteredTime; // cin >> enteredTime; // cout << enteredTime << endl; } /*---------------- End of main part --------------------*/