#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 { 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. Could also be done by reference. 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); 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 subTime(Time &t1, Time &t2); /*-------------- End of class time definition ------------------*/ /*prototype of addTime as an external 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 ---------------------*/ /*This part often goes in its own file, with ".cpp" extension (say "Time.cpp"). To do that you need to include the file above at the top: #include "Time.h"*/ /*-----Notice that we use "Time::" in front of each function so that the compiler knows that the function 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//How do we show the time? Try to output this as 01:03:34 instead of 1:3:34 - use setw and setfill { cout << setfill('0') << setw(2) << getHour() << ":" << setw(2) << minute << ":" << setw(2) << second << endl; } void Time::showTime12() const//How do we show the time? Try to output this as 01:03:34 instead of 1:3:34 - use setw and setfill { 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=hour*3600 + minute*60 + second;//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) { return 0; //Fill it in! } /*End of definition of addTime*/ /*-----------------main part of the program---------------------*/ /*Normally it has its own file as well. To include class Time you need to write #include "Time.h" at the top and you need to compile it at the same time as the file Time.cpp*/ int main() { Time now(15, 14, 15); //Declares now to be of class Time. -> Instantiation - uses custom constructor cout << "Time now in 24h format is "; now.showTime24(); cout << endl << "Time now in 12h format is "; now.showTime12(); Time now2; now2.setTime(13,49,34); cout << endl << "Time now2 in 24h format is "; now2.showTime24(); cout << endl << "Time now2 in 12h format is "; now2.showTime12();//We would use showTime12(now2) if it were not a member function const Time noon(12,0,0); cout << endl << "Time noon in 12h format is "; noon.showTime12(); cout << endl << endl << "Time now2 is: "; now2.showTime24(); now2.addTime(6,23,56); cout << "now2 + (6,23,56) is: "; now2.showTime24(); cout << endl << "******************NEW STUFF*******************"<< endl<< endl; Time sunset(7,15,00);//Time is just another type, just like int, or char, string, etc. cout << "Time sunset in 24h format is "; sunset.showTime24(); Time & dinnerTime = sunset; cout << endl << "dinnerTime is a reference to sunset. The output of dinnertime.showTime24(); is: "; dinnerTime.showTime24(); Time * sunPtr = & sunset; cout << endl << "sunPtr is a pointer to sunset. The output of sunPtr->showTime24(); is: "; sunPtr->showTime24();/*Use the ARROW ( -> ) operator instead of the. operator to call member functions on pointers to member functions. Arrow ONLY works for pointers.*/ cout << endl << "(*sunPtr).showTime12() also works since (*sunPtr) is just sunset"; (*sunPtr).showTime12(); cout << endl; //sunPtr.showTime12();//This gives an error: sunPtr is not an object of class time, rather is the ADDRESS of an object of class time array arrayOfTimes; cout << "Define an array of times of size 5, with no initalization parameters (so they are all initialized to 0). They print as"; for(auto element : arrayOfTimes) { element.showTime12(); cout << endl; } cout << "We can use sunPtr->addTime(3,56,45); to add 3h, 56m, 45s to Time sunset"; sunPtr->addTime(3,56,45); cout << "New value of sunset is: "; sunset.showTime12(); cout << endl << "******************NEW STUFF*******************"<< endl<< endl; cout << "Destructors" << endl << endl; { Time lunch(12,23,45); lunch.showTime12(); }//Here the default destructor is called (the computer destroys the object lunch) //lunch.showTime12(); cout << endl << "******************NEW STUFF*******************"<< endl<< endl; Time lunch(12,23,45); cout << "Time lunch is: "; lunch.showTime12(); cout << endl << "Time sunset is: "; sunset.showTime12(); cout << endl << "When we add lunch + sunset using the external function we get: "; // addTime(sunset, lunch).showTime12(); //version of addTime as an external friend function. input is two time objects. cout << endl << "The value of sunset has not changed: "; cout << "Time sunset is: "; sunset.showTime12(); cout << endl << "When we add sunset + 12:23:45 using the member function we get no output, but the value of sunset gets changed: "; sunset.addTime(12,23,45);/*version of addTime as a member function. Input is 3 integers. NOTE that this one changes the object that calls it (in this case, sunset) where as the external does not. */ cout << endl << "Now time sunset is: "; sunset.showTime12(); } /*---------------- End of main part --------------------*/