The file Time.h contains a Time class. Design a class called MilTime that is der
ID: 3603407 • Letter: T
Question
The file Time.h contains a Time class. Design a class called MilTime that is derived from the Time class.
//Time.h
#indef TIME_H
#define TIME_H
class Time
{
protected:
int hour;
int min;
int sec;
public;
//Default constructor
Time()
{hour = 0; min = 0; sec = 0;}
//Constructor
Time(int h, int m, int s)
{hour=h; min=m, sec=s;}
//Accessor functions
int getHour() const
{return hour;}
int getMin() const
{return min;}
int getSec() const
{return sec;}
};
#endif
The MilTime class should convert time in military (24 hour) format to the standard time format used by the Time class. The class should include the following member variables:
-milHours: Contains the hour in 24-hour format.
-milSeconds: Contains the seconds in standard format.
-Constructor: Should accept arguments for the hour and seconds, in military format. The time should then be converted to standard time and stores in the hours,min, and sec variables of the Time class.
-setTime: Accepts arguments to be stored in the milHours and milSeconds variables. The time should then be converted to standard time and stores in the hours,min, and sec variables of the Time class.
-getHour: Returns the hour in military format.
-getStandHr: Returns the hour in standard format.
Demonstrate the class in a program that asks the user to enter the time in military format. The program should display the time in both military and standard format.
Design a class named TimeClock. This class should be derived from the MilTime class. 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. Ex: starting time of 900 hours, and ending time of 1300 hours, the elapsed time between the starting and ending time is 4 hours.
For both classes they should not accept hours greater than 2359, or less than 0. It should not accept seconds greater than 59 or less than 0.
Explanation / Answer
// Include the required head files
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;
class Time
{
protected:
int hour;
int min;
int sec;
public:
// Default constructor
Time(){ hour = 0; min = 0; sec = 0; }
// Constructor
Time(int h, int m, int s)
{
hour = h; min = m; sec = s;
}
// Accessor functions
int getHour() const
{ return hour; }
// Method to get the minutes
int getMin() const
{ return min; }
// Method to get the second
int getSec() const
{ return sec; }
};
// Class MilTime
class MilTime:public Time
{
//Declare the required variables
protected:
int milHrs;
int milSec;
bool blpm;
public:
// Constructor
MilTime()
{
milHrs = 0;
milSec = 0;
blpm = false;
}
// Constructor
MilTime(int milho, int misc)
{
blpm = false;
setTime(milho, misc);
}
// Constructor
MilTime(long seconds)
{
hour = seconds/3600;
int rem = seconds%3600;
min = rem/60;
rem = rem%60; //take out minutes
sec = rem;
blpm = false;
setTime(((hour * 100) + min), sec);
}
// Method to set the time
void setTime(int milho, int misc)
{
milHrs = milho;
milSec = misc;
hour = (milho/100);
if(hour > 12)
{ hour = hour-12;
blpm = true;
}
min = milho%100;
sec = misc;
}
// Method to get the military hour
int getHour()
{return milHrs;}
// Method to get the standard hour
int getStandHr()
{return hour;}
//VMethod to verify the time
static MilTime getTime(string aString)
{
MilTime myTime(0,0);
int h, s;
bool flag = false;
while(flag == false)
{
cout << aString;
cin >> h;
if(h < 0 || h > 2359)
{
flag = false;
cout << "the time is wrong, please try it again. ";
}
else
flag = true;
}
flag = false;
while(flag == false)
{
cout << "Enter a second : ";
cin >> s;
if(s < 0 || s > 59)
{
flag = false;
cout << "the time is wrong, please try it again. ";
}
else
flag = true;
}
myTime.setTime(h,s);
return myTime;
}
// Method to convert the military time to seconds
long totalSecond()
{
long totalS = milSec;
totalS = totalS + (min * 60);
totalS = totalS + (hour * 3600);
if(blpm)
totalS = totalS + (12 * 3600);
return totalS;
}
};
// Class timeclock
class TimeClock:public MilTime
{
protected:
MilTime endtm;
public:
//Constructor
TimeClock(): MilTime(0,0)
{ MilTime endTime(0,0);
}
// Constructor to set the end time
TimeClock(MilTime sttm, MilTime ettm): MilTime(sttm)
{
endtm = ettm;
}
// Method to return the end time
MilTime getEndTime()
{ return endtm;}
//Method to calculate elapsed time
MilTime elapsedTime()
{
MilTime elpstm;
elpstm = MilTime( endtm.totalSecond()- totalSecond());
return elpstm;
}
};
int main()
{
MilTime sttme, endtm, elpsTime;
MilTime a1(1923,12);
cout<<"initial hour as military time: " <<a1.getHour()<<endl;
cout<<"initial hour as standard time: " <<a1.getStandHr()<<endl;
sttme = sttme.getTime("Enter Start Time in military time : ");
endtm = endtm.getTime("Enter End Time in military time : ");
TimeClock tClock(sttme, endtm);
cout << " total start sec : " << sttme.totalSecond() << endl;
cout << " total end sec : " << endtm.totalSecond() << endl;
cout << " " << endl;
elpsTime = tClock.elapsedTime();
cout << "ElapsedTime is : " << elpsTime.getStandHr() << " : " << elpsTime.getMin();
cout << " : " << elpsTime.getSec()<< endl;
cout << " " << endl;
// converted the military time to standard time.
cout << "Military hours for end time is : " << endtm.getHour() << " and " << endtm.getSec();
cout << " seconds " << endl;
cout << "Standard Time for end time is: " << endtm.getStandHr() << ":" << endtm.getMin() << ":" << endtm.getSec();
cout << endl;
return 0;
}
Result:
Initial hour as military time: 1923
Initial hour as standard time: 7
Enter Start Time in military time: 2112
Enter a second: 21
Enter End Time in military time: 1201
Enter a second: 25
Total start sec: 76341
Total end sec: 43285
Elapsed Time is: -9 : -10 : -56
Military hours for end time is: 1201 and 25 seconds
Standard Time for end time is: 12:1:25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.