#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); friend ostream & operator<<(ostream &, Time &t2); public: Time(int =0, int =0, int =0);//Constructor which defaults to 00:00:00 when no arguments are given ~Time(); operator int() const;//Check this one 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. Time &operator+=(int ); Time &operator+=(Time ); bool operator==(Time ); bool operator!=(Time ); bool operator<(Time ); bool operator<=(Time );//Define this bool operator>(Time ); bool operator>=(Time );//Define this //Write the prototype of operator ++Time (prefix) Time & operator++();//prefix operator Time operator++(int );//The "int" there is only for the compiler to understand that it is a postfix operator instead of a prefix. bool greaterThan(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::Time(int ho, int mi, int se)//Custom constructor :hour(ho),minute(mi),second(se) { setTime(ho, mi, se); } Time::~Time() { } Time::operator int() const//Do automatic conversion from Time to int (seconds) { return hour*3600 + minute*60 + second; } 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) << this->getHour() << ":" << setw(2) << this->minute << ":" << setw(2) << (*this).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 } Time &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 return (*this); } Time &Time::operator+=(Time t1)//Adds t1 to calling time { (*this) = (*this) + t1; return (*this); } Time & Time::operator++()//Prefix { (*this) += 1; return (*this); } Time Time::operator++(int)//Postfix (int is there so that compiler knows it is postfix instead of prefix, nothing else) { Time temp{(*this)};//Store original value (*this) += 1;//Add one to calling object return temp; //Return original value } //How is the computer going to know the difference between now++ and ++now???? bool Time::operator==(Time t2) { return (toSec()==t2.toSec()); } bool Time::operator!=(Time t2) { return !( (*this) == t2 ); } bool Time::operator<(Time t2) { return (toSec() t2 );//version 2 } bool Time::operator>(Time t2) { return (toSec()>t2.toSec()); } bool Time::greaterThan(Time t2) { return (toSec()>t2.toSec()); } bool Time::operator>=(Time t2) { return (toSec()>=t2.toSec()); } 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)//same as addTime(t1, 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); } ostream & operator<<(ostream & strm, Time & t2) { strm << setfill('0') << setw(2) << t2.getHour() << ":" << setw(2) << t2.minute << ":" << setw(2) << t2.second; return strm; } /*-----------------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 != now2) gives " << (now2 != now2) << endl << endl; cout << "(now2 == now) gives " << (now2 == now) << endl << endl; cout << "(now2 != now) gives " << (now2 != now) << endl << endl; cout << "(now2 < now) gives " << (now2 < now) << endl << endl; cout << "(now2 > now) gives " << (now2 > now) << endl << endl; cout << "(now2 > now) gives " << now2.greaterThan(now) << endl << endl; cout << "(now2 < now2) gives " << (now2 < now2) << endl << endl; cout << "(now2 <= now2) gives " << (now2 <= now2) << endl << endl; cout << "now + now2 gives "; (now + now2).showTime24(); cout << endl; cout << "now - now2 gives "; (now - now2).showTime24(); cout << endl; cout << "now += now2 gives: "; (now += now2).showTime24(); cout << (now += now2) << endl; cout << "now += 1234 gives: "; (now += 1234).showTime24(); now.showTime24(); cout << now <<" " << now2 << endl;//it is calling operator<<(cout, now) cout << "Time now before doing ++now" << now; cout << "Output of \"cout << ++now\" is: " << ++now << endl; cout << "Output of \"now\" after doing \"now++\" is: " << now << endl; (now++).showTime24(); Time *timePtr{new Time}; cout << "*timePtr It is initialized to 00:00:00 by default constructor " << *timePtr << endl; timePtr->setTime(12,34,56); cout << *timePtr << endl; delete timePtr; cout << *timePtr << endl; cout << static_cast(now) << endl; } /*---------------- End of main part --------------------*/ /*About the this pointer: When you call a member function say showTime24(), you write: object.showtime24(); In the definition of showTime24(), hour, minute, second mean the data of the CALLING OBJECT (called "object in this case"). In order to refer to the WHOLE object (not only hour, minute, second but all of it), there is the "this" pointer: it points to the calling object. In other words, this = &(calling object) */