#include #include using namespace std; ///////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////Time Class Prototypes////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////// class Time { /* This class will hold valid time in 12-hour am/pm notation. The Time class will hold the time data in hh:mm:ss am/pm format, however it will only display value for seconds if it is requested. */ public: //----- Default Time Constructor -----// /* This function is the default Time constructor, when a new Time class variable is declared using the default constructor, it sets the Time to midnight, or 12:00:00 am. */ Time(); //----- Time Constructor without Second(s) Information -----// /* This function is a Time constructor, which when used will set the time to the values of the functions arguments. Note: sets second(s) value to zero. precondition: values of function argument must be valid to represent time postcondition: Time class variable will be set to argument values, as long as argument values represent a valid time. */ Time(int hours, int minutes, bool time_is_pm); //----- Time Constructor with Second(s) Information -----// /* This function is a Time constructor, which when used will set the time to the values of the functions arguments. Also this constructor inserts second(s) value. precondition: values of function argument must be valid to represent time postcondition: Time class variable will be set to argument values, as long as argument values represent a valid time. */ Time(int hours, int minutes, int seconds, bool time_is_pm); //----- Time Constructor in 24-hr Notation -----// /* This function is a Time constructor, which when used will set the time to the values of the functions arguments. Also this puts the time into 24-hr notation. precondition: values of function argument must be valid to represent time postcondition: Time class variable will be set to argument values, as long as argument values represent a valid time. */ Time(int hours, int minutes, int seconds); //--------- read -------- /* Read prompts the user to enter hours, minutes and whether time is pm or not, then validates this data and enters it into the value of "this". If they enter invalid data, they are repeatedly asked to reenter until they enter legal values. Note: This function does not include second information, and sets value for second(s) to zero. */ void read(); //--------- read_with_second(s)-------- /* Read prompts the user to enter hours, minutes, seconds and whether time is pm or not, then validates this data and enters it into the value of "this". If they enter invalid data, they are repeatedly asked to reenter until they enter legal values.*/ void read_with_seconds(); //--------- print -------- // print prints out "this's" hours, minutes, and am/pm in hh:mm am/pm format. void print(bool is_in_24hr) const; //--------- print_with_seconds -------- // print prints out "this's" hours, minutes, seconds, and am/pm in hh:mm:ss am/pm format. void print_with_seconds(bool is_in_24hr) const; // -------- time_set ----------- /* time_set sets "this" time to be hours, minutes, seconds, and am/pm if it's a valid time, and doesn't change "this" if the arguments don't represent a real time. */ void time_set_12hr(int hours, int minutes, int seconds, bool time_is_pm); void time_set_24_hr(int hours, int minutes, int seconds); //-------- add_hours ------- /* add_hours adds num hours to "this", changing "this", if num is not positive, "this" will not be changed */ void add_hours (int number_of_hours); //-------- add_minutes ------- /* add_minutes adds num minutes to "this", changing "this", if num is not positive, "this" will not be changed */ void add_minutes (int number_of_minutes); //-------- add_seconds ------- /* add_seconds adds num seconds to "this", changing "this", if num is not positive, "this" will not be changed */ void add_seconds (int number_of_seconds); // ------- happens_before --------- /* happens_before returns true if "this" time is earlier than other_time (and false if "this" time is not earlier) */ bool happens_before (Time other_time) const; void convert_12hr_to_24hr (void); void convert_24hr_to_12hr (void); private: int num_hours; int num_minutes; int num_seconds; bool true_if_time_is_pm; bool is_in_24hr; /* This function is called by the class constructors to validate that arguments represent a valid time and then if date is legal it will set "this's" value to that time */ void constructor_set(int hours, int minutes, int seconds, bool is_pm); }; ///////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////Start of Main////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////// int main() { Time foo; foo.print_with_seconds(); foo.time_set(9,17,1,false); foo.print_with_seconds(); return EXIT_SUCCESS; } ///////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////Time Class Functions/////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////// //----- Default Time Constructor -----// /* This function is the default Time constructor, when a new Time class variable is declared using the default constructor, it sets the Time to midnight, or 12:00:00 am. */ Time::Time() { constructor_set(12,0,0,false); } //----- Time Constructor without Second(s) Information -----// /* This function is a Time constructor, which when used will set the time to the values of the functions arguments. Note: sets second(s) value to zero. precondition: values of function argument must be valid to represent time postcondition: Time class variable will be set to argument values, as long as argument values represent a valid time. */ Time::Time(int hours, int minutes, bool time_is_pm) { constructor_set(hours, minutes, 0, time_is_pm); } //----- Time Constructor with Second(s) Information -----// /* This function is a Time constructor, which when used will set the time to the values of the functions arguments. Also this constructor inserts second(s) value. precondition: values of function argument must be valid to represent time postcondition: Time class variable will be set to argument values, as long as argument values represent a valid time. */ Time::Time(int hours, int minutes, int seconds, bool time_is_pm) { constructor_set(hours, minutes, seconds, time_is_pm); } //----- Time Constructor in 24-hr Notation -----// /* This function is a Time constructor, which when used will set the time to the values of the functions arguments. Also this puts the time into 24-hr notation. precondition: values of function argument must be valid to represent time postcondition: Time class variable will be set to argument values, as long as argument values represent a valid time. */ Time::Time(int hours, int minutes, int seconds) { constructor_set(hours, minutes, seconds); } //--------- read -------- /* Read prompts the user to enter hours, minutes and whether time is pm or not, then validates this data and enters it into the value of "this". If they enter invalid data, they are repeatedly asked to reenter until they enter legal values. Note: This function does not include second information, and sets value for second(s) to zero. */ void Time::read() { do { cout<<"Please enter hours: "; cin>>num_hours; } while( (num_hours <= 0) || (num_hours > 12) ); do { cout<<"Please enter minutes: "; cin>>num_minutes; } while( (num_minutes < 0) || (num_minutes >= 60) ); char am_pm; do { cout<<"Please enter whether time is am or pm (a = am, p = pm): "; cin>>am_pm; } while( (am_pm != 'a' && am_pm != 'A') && (am_pm != 'p' && am_pm != 'P') ); if ( (am_pm == 'p') || (am_pm == 'P') ) true_if_time_is_pm = true; else true_if_time_is_pm = false; } //--------- read_with_second(s)-------- /* Read prompts the user to enter hours, minutes, seconds and whether time is pm or not, then validates this data and enters it into the value of "this". If they enter invalid data, they are repeatedly asked to reenter until they enter legal values.*/ void Time::read_with_seconds() { do { cout<<"Please enter hours: "; cin>>num_hours; } while( (num_hours <= 0) || (num_hours > 12) ); do { cout<<"Please enter minutes: "; cin>>num_minutes; } while( (num_minutes < 0) || (num_minutes >= 60) ); do { cout<<"Please enter seconds: "; cin>>num_seconds; } while( (num_seconds < 0) || (num_seconds >= 60) ); char am_pm; do { cout<<"Please enter whether time is am or pm (a = am, p = pm): "; cin>>am_pm; } while( (am_pm != 'a' && am_pm != 'A') && (am_pm != 'p' && am_pm != 'P') ); if ( (am_pm == 'p') || (am_pm == 'P') ) true_if_time_is_pm = true; else true_if_time_is_pm = false; } //--------- print -------- // print prints out "this's" hours, minutes, and am/pm in hh:mm am/pm format. void Time::print(void) const { if (is_in_24hr) { cout< 0) && (hours <= 24) ) && ( (minutes >= 0) && (minutes < 60) ) && ( (seconds >= 0) || (seconds < 60) ) ) { num_hours = hours; num_minutes = minutes; num_seconds = seconds; is_in_24hr = true; } } void Time::constructor_set(int hours, int minutes, int seconds, bool is_pm) { if ( ( (hours > 0) && (hours <= 12) ) && ( (minutes >= 0) && (minutes < 60) ) && ( (seconds >= 0) || (seconds < 60) ) && ( (is_pm == true) || (is_pm == false) ) ) { num_hours = hours; num_minutes = minutes; num_seconds = seconds; true_if_time_is_pm = is_pm; is_in_24hr = false; } }