#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 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();//Function that returns hour stored in object int getMinute();//Function that returns minute stored in object int getSecond();//Function that returns second stored in object void showTime24();//Function to output the 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 }; /*-------------- 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} { } void Time::setHour(int ho)//Function to set hour { hour=ho; } void Time::setMinute(int mi)//Function to set minute { minute=mi; } void Time::setSecond(int se)//Function to set second { second=se; } 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()//Function that returns hour stored in object { return hour; } int Time::getMinute()//Function that returns minute stored in object { return minute; } int Time::getSecond()//Function that returns second stored in object { return second; } void Time::showTime24()//How do we show the time? Try to output this as 01:03:34 instead of 1:3:34 - use setw and setfill { cout << getHour() << ":" << getMinute() << ":" << getSecond() << endl; } /*---------- 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*/ int main() { Time now{30, -14, 15}; //Declares now to be of class Time. -> Instantiation - uses custom constructor Time later;//uses default constructor later.showTime24(); later.setTime(4,5,6); later.showTime24(); now.showTime24(); now.setTime(15,12,32); now.showTime24(); } /*---------------- End of main part --------------------*/