define a class called time. The class should have four datamembers: hour, minute
ID: 3612612 • Letter: D
Question
define a class called time. The class should have four datamembers: hour, minute, second ans AM/PM flag.a. it should have a constructor to initialize these data members.Remember midnight is 00:00:00 AM
b. ity should have a function member to increment the second.However, remember that when the member of second passes 60, secondmust reset to 0 and the minute must incremented.
c. It should have a function member to increment the time.
d. It should have a function member to compare two times and returna structure showing the difference in hours, minutes andseconds.
Explanation / Answer
#includeusing namespace std;class my_time{public:int hour, minute, second;string flag;my_time(int h,int m,int s,string f){hour = h;minute = m;second =s;flag =f;}void incrment_sec(){second++;if(second >= 60){second=0;minute++;if(minute >= 60){minute=0;hour++;if(hour > 12)hour=1;}}if(hour ==12 && minute==0 && second ==0 && flag == "AM"){flag = "PM";hour=12;}elseif(hour ==12 && minute==0 && second ==0 && flag == "PM"){flag = "AM";hour=0;}}void incrment_time(int h,int m,int s){second+=s;minute += m;hour+= h;if(second >= 60){second = second%60;minute += second /60;if(minute >= 60){minute=minute%60;hour=hour+minute/60;if(hour > 12){}}}if(hour ==12 && minute==0 && second ==0 && flag == "AM"){flag = "PM";hour=12;}elseif(hour ==12 && minute==0 && second ==0 && flag == "PM"){flag = "AM";hour=0;}}int get_secs(){int s;s = hour * 3600 + minute * 60 + second;if(flag == "PM")s += 12*3600;return s;}void diff(my_time t){int sec1,diff,sec2;sec1 = get_secs();sec2 = t.get_secs();diff = sec1 - sec2;if(diff < 0)diff = -diff;coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.