#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 ------------------*/ /*------------- 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; } // Time subTime(Time t1, Time &t2) // { // } //now.addTime(1,2,3); adds 1hour, 2minutes, 3seconds to Time now. //Idea: write both times in seconds. Add the seconds. Convert back to hours, min, sec. /* if(getHour() < 12) "AM" else "PM" am/pm: if hour < 12, write am at the end if hour >= 12, , write pm at the end hour output: if hour < 13, print hour if hour >= 13, print hour - 12 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13=1pm, 14=2pm, 15=5pm I want addTime to do the following: If I have an object Time here(13,14,15), then here.addTime(1,2,50) should modify "here" to 14:16:65 (really 14:17:05) void addTime(int ho, int mi, int) */ /*---------- End of definitions of class Time function members --------------*/ /*-----------------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*/ /*---------------- End of main part --------------------*/