#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); 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. 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. }; /*-------------- End of class time definition ------------------*/ /*prototype of addTime as an external friend function that takes two time objects and returns the times added*/ Time addTime(Time&, Time&); /*End of prototype of addtime*/ /*------------- 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() { // cout << "Bye bye "; // showTime12(); // cout << endl; } 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 } int Time::toSec() const { return hour*3600 + minute*60 + second; } /*---------- End of definitions of class Time function members --------------*/ /*Definition of external function addtime*/ 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); } /*End of definition of addTime and subTime*/ /*-----------------main part of the program---------------------*/ int main() { Time now{13,14,15};//Delete this for question 1 of the exam now.showTime24();//Delete this for question 1 of the exam } /*---------------- End of main part --------------------*/