Time Clock Design a class named TimeClock. The class should be derived from the
ID: 3620545 • Letter: T
Question
Time ClockDesign a class named TimeClock. The class should be derived from the MilTime class you designed in Programming Challenge 1. The class should allow the programmer to pass two times to it: starting time and ending time. The class should have a member function that returns the amount of time elapsed between the two times. For example, if the starting time is 900 hours (9:00 am), and the ending time is 1300 hours (1:00 pm), the elapsed time is 4 hours.
Input Validation: The class should not accept hours greater than 2359 or less than 0.
Explanation / Answer
please rate - thanks you did not provide the class to derive from, so I had to start from scratch#include<iostream>
#include<iomanip.h>
using namespace std;
void convert(int,int&,int&);
class Time
{
public:
int hour,minute;
Time() //constructor
{hour=0;
minute=0;
}
~Time() //destructor
{hour=0;
minute=0;
}
Time(int h,int m)
{hour=h;
minute=m;
}
void get() //get the time from the user
{cout<<"Enter the time ";
cout<<"Hour: ";
cin>>hour;
while(hour<0||hour>23)
{cout<<"Invalid hour ";
cout<<"Hour: ";
cin>>hour;
}
cout<<"Minutes: ";
cin>>minute;
while(minute<0||minute>59)
{cout<<"Invalid minute ";
cout<<"Minutes: ";
cin>>minute;
}
}
void show() //print the time with 2 characters and 0 fill to make all digits 2, leading 0 if needed
{cout<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute<<endl;
}
Time operator - ( Time t) //overloaded subtraction operator
{int h,m,totalmin;
totalmin=(hour*60+minute)-(t.hour*60+t.minute);
cout<<totalmin<<endl;
if(totalmin<0) //since subtraction if larger # was second the answer will be negative so change to positive
totalmin=-totalmin;
convert(totalmin,h,m);
return Time(h,m);
}
void convert(int totalmin,int& h,int& m)
{ m=totalmin%60;
h=(totalmin/60)%12;
if(h==0&&totalmin>60)
h=12;
return;
}
};
int main()
{ Time time1,time2,added,subtracted;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
time1.get(); //get 1st time
time2.get(); //get 2nd time
subtracted=time1-time2; //subtract
cout<<"Time 1 : "; //do the output nicely
time1.show();
cout<<"Time 2 : ";
time2.show();
cout<<" Subtracted: ";
time1.show();
cout<<"-";
time2.show();
cout<<"---------- ";
subtracted.show();
system("pause");
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.