#include #include #include using namespace std; /*---- Class time definition. Contains only the list of members and the prototypes of the member functions -----*/ /*This part usually goes in a different file with extension ".h" (h is for header). "Time.h" for example*/ class Time //Class time definition - note that functions are defined later, outside of the class { friend Time addTime(Time &t1, Time &t2); friend Time operator+(Time&, Time&); friend Time operator-(Time &t1, Time &t2); public: Time(int =0, int =0, int =0);//Constructor which defaults to 00:00:00 when no arguments are given ~Time(); void setHour(int &ho);//Prototype of function to set hour. void setMinute(int &mi);//Prototype of function to set minute void setSecond(int &se);//Prototype of function to set second void setTime(int ho, int mi, int se);//Prototype of function to set time all at once. int getHour() const;//Function that returns hour stored in object int getMinute() const;//Function that returns minute stored in object int getSecond() const;//Function that returns second stored in object void showTime24() const;//Function to output the time. void showTime12() const;//Function to output the time. void addTime(int ho, int mi, int se);//Prototype of function that adds ho, mi, se to a time object. void operator+=(int ); void operator+=(Time ); bool operator==(Time ); bool operator<(Time ); bool operator>(Time ); private://Here are the private members int hour; //Should be between 0 and 23. int minute;//Should be between 0 and 59 int second;//Should be between 0 and 59 int toSec() const;//Utility function that converts time to seconds. }; Time addTime(Time&, Time&); Time operator+(Time&, Time&); Time::Time(int ho, int mi, int se)//Custom constructor :hour(ho),minute(mi),second(se) { setTime(ho, mi, se); } Time::~Time() { } void Time::setHour(int &ho)//Function to set hour { if(ho>=0 && ho<24) hour=ho; else hour=0; } void Time::setMinute(int &mi)//Function to set minute { if(mi>=0 && mi<60) minute=mi; else minute=0; } void Time::setSecond(int &se)//Function to set second { if(se>= 0 && se<60) second=se; else second=0; } void Time::setTime(int ho, int mi, int se) { setHour(ho);//you can also write hour=ho; setMinute(mi);//you can also write minute=mi; setSecond(se);//you can also write second=se; } int Time::getHour() const//Function that returns hour stored in object { return hour; } int Time::getMinute() const//Function that returns minute stored in object { return minute; } int Time::getSecond() const//Function that returns second stored in object { return second; } void Time::showTime24() const//shows time in 24 h format { cout << setfill('0') << setw(2) << getHour() << ":" << setw(2) << minute << ":" << setw(2) << second << endl; } void Time::showTime12() const//shows time in 12 hour format { 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) { int totsec1=toSec();//time of calling object in seconds int totsec2=ho*3600 + mi*60 + se;//time of added time in seconds int addsec=totsec1+totsec2; //Now we need to convert addsec to hours, minutes, seconds. hour = (addsec/3600)%24; //Gives number of hours in addsec seconds. addsec = addsec%3600;//Gives number of remaining seconds. minute = addsec/60;//Gives number of minutes. second = addsec%60;//Remainder seconds } void Time::operator+=(int sec)// { int addsec = toSec() + sec; hour = (addsec/3600)%24; //Gives number of hours in addsec seconds. addsec = addsec%3600;//Gives number of remaining seconds. minute = addsec/60;//Gives number of minutes. second = addsec%60;//Remainder seconds } void Time::operator+=(Time t1)//Adds t1 to calling time { (*this) = (*this) + t1;//Just adding the two times // int totsec1=toSec();//time of calling object in seconds // int totsec2=t1.toSec(); //time of t1 in seconds // int addsec=totsec1+totsec2; // //Now we need to convert addsec to hours, minutes, seconds. // hour = (addsec/3600)%24; //Gives number of hours in addsec seconds. // addsec = addsec%3600;//Gives number of remaining seconds. // minute = addsec/60;//Gives number of minutes. // second = addsec%60;//Remainder seconds } bool Time::operator==(Time t2) { return (toSec()==t2.toSec()); } bool Time::operator<(Time t2) { return (toSec()(Time t2) { return (toSec()>t2.toSec()); } // 4 < 5 int Time::toSec() const { return hour*3600 + minute*60 + second; } Time addTime(Time &t1, Time &t2) { int totsec1=t1.toSec(); int totsec2=t2.toSec(); int addsec=totsec1+totsec2; //Now we need to convert addsec to hours, minutes, seconds. int ho = (addsec/3600)%24; //Gives number of hours in addsec seconds. addsec = addsec%3600;//Gives number of remaining seconds. int mi = addsec/60;//Gives number of minutes. int se = addsec%60;//Remainder seconds return Time(ho,mi,se); } Time operator+(Time &t1, Time &t2) { int totsec1=t1.toSec(); int totsec2=t2.toSec(); int addsec=totsec1+totsec2; //Now we need to convert addsec to hours, minutes, seconds. int ho = (addsec/3600)%24; //Gives number of hours in addsec seconds. addsec = addsec%3600;//Gives number of remaining seconds. int mi = addsec/60;//Gives number of minutes. int se = addsec%60;//Remainder seconds return Time(ho,mi,se); } Time operator-(Time &t1, Time &t2) { int totsec1=t1.toSec(); int totsec2=t2.toSec(); int addsec=totsec1-totsec2; ((addsec < 0)? addsec += 24*3600 : addsec+=0); //Now we need to convert addsec to hours, minutes, seconds. int ho = (addsec/3600)%24; //Gives number of hours in addsec seconds. addsec = addsec%3600;//Gives number of remaining seconds. int mi = addsec/60;//Gives number of minutes. int se = addsec%60;//Remainder seconds return Time(ho,mi,se); } /*-----------------main part of the program---------------------*/ int main() { Time now{13,14,15}; cout << "now is: "; now.showTime24(); Time now2{15,56,45}; cout << "now2 is: "; now2.showTime24(); cout << "(now2 == now2) gives " << (now2 == now2) << endl << endl; cout << "(now2 == now1) gives " << (now2 == now) << endl << endl; cout << "(now2 < now) gives " << (now2 < now) << endl << endl; cout << "now + now2 gives "; (now + now2).showTime24(); cout << endl; cout << "now - now2 gives "; (now - now2).showTime24(); cout << endl; } /*---------------- End of main part --------------------*/