In Program 15-17, the file Time.h contains a Time class. Design a class called M
ID: 3625915 • Letter: I
Question
In Program 15-17, the file Time.h contains a Time class. Design a class called MilTime that is derived from the Time class. The MilTime class should convert time in military (24-hour) format to the standard time format used by the Time class. The class should have the following member variables:milHours: contains the hour in 24-hour format. For example, 1:00pm would be stored as 1300 hours, and 4:30pm would be stored as 1630 hours.
milSeconds: contains the seconds in standard format.
The class should have the following member functions:
Constructor: the constructor should accept arguments for the hour and seconds, in military format. The time should then be converted to standard time and stored in the hours, min, and sec variables of the Time class.
setTime: accepts arguments to be stored in the milHour and milSeconds variables. The time should then be converted to standard time and stored 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 then display the time in both military and standard format. This assignment MUST be broken into three separate files (specification file, implementation file, and driver program).
Input Validation: The MilTime class 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
please rate - thanks
sorry I don't do separate files, you ask for 3, but would need 4-2 headers, implementation, main
#include <iostream>
using namespace std;
class Time
{protected:
int hour;
int min;
int sec;
public:
Time(int h, int m, int s)
{hour=h;
min=m;
sec=s;
}
int getHour()
{return hour;
}
int getMin()
{return min;
}
int getSec()
{return sec;
}
};
class MilTime:public Time
{private:
int milH;
int milS;
public:
MilTime(int,int);
void change();
void setTime(int, int);
int getHour()
{ return milH;
}
int getStandHr()
{ return hour;
}
};
MilTime::MilTime(int h=0,int s=0):Time(0,0,s)
{if(h<0||h>2359)
{cout<<"Invalid input-Hours must be between 0 and 2359 ";
milH=0;
milS=0;
}
else if(s<0||s>59)
{cout<<"Invalid input-Seconds must be between 0 and 59 ";
milS=0;
}
else
{milH=h;
milS=s;
}
change();
}
void MilTime::change()
{if(milH>1200)
hour=milH-1200;
else
hour=milH;
hour/=100;
min=milH%100;
}
void MilTime::setTime(int h,int s)
{milH=h;
milS=s;
change();
}
int main()
{int h,m,s;
MilTime a(1923,12);
cout<<"initial hour as military time: "<<a.getHour()<<endl;
cout<<"initial hour as standard time: "<<a.getStandHr()<<endl;
cout<<"Enter new military hours: ";
cin>>h;
cout<<"Enter new military minutes: ";
cin>>m;
cout<<"Enter new military seconds: ";
cin>>s;
a.setTime (h*100+m,s);
cout<<"changed hour as military time: "<<a.getHour()<<endl;
cout<<"changed hour as standard time: "<<a.getStandHr()<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.