Question: Modify the MilTime class you created for Programming Challenge 4 of Chapter 15 (Time Format). The class should implement the following exceptions: BadHour Throw when
Modify the MilTime class you created for Programming Challenge 4 of Chapter 15 (Time Format). The class should implement the following exceptions:
BadHour Throw when an invalid hour (< 0 or > 2359) is passed to the class. BadSeconds Throw when an invalid number of seconds (< 0 or > 59) is passed to the class.
Demonstrate the class in a driver program.
THIS IS MY PROGRAM:
#include
#include
#include
using namespace std;
class Time
{
protected:
int hour;
int min;
int sec;
public:
// Default constructor
Time()
{
hour = 0; min = 0; sec = 0;
}
// Constructor 2
Time(int h, int m, int s)
{
hour = h; min = m; sec = s;
}
// Accessors
int getHour() const
{
return hour;
}
// returns the minutes
int getMin() const
{
return min;
}
// returns the second
int getSec() const
{
return sec;
}
};
//Class MilTime
class MilTime :public Time
{
//Declaring initial variables
protected:
int milHrs;
int milSec;
bool ampm;
public:
// Constructor
MilTime()
{
milHrs = 0;
milSec = 0;
ampm = false;
}
// Constructor 2
MilTime(int milho, int misc)
{
ampm = false;
setTime(milho, misc);
}
// sets the time
void setTime(int milho, int misc)
{
milHrs = milho;
milSec = misc;
hour = (milho / 100);
if (hour > 12)
{
hour = hour - 12;
ampm = true;
}
min = milho % 100;
sec = misc;
}
// returns the military hour
int getHour()
{
return milHrs;
}
// returns the standard hour
int getStandHr()
{
return hour;
}
string getAMPM()
{
if (ampm)
{
return " p.m.";
}
else
{
return " a.m.";
}
}
//func for verification
static MilTime getTime(string aString)
{
MilTime myTime(0, 0);
int h, s;
bool valid = false;
while (valid == false)
{
cout << aString;
cin >> h;
if (h < 0 || h > 2359)
{
valid = false;
cout << "something is wrong with the input, please try it again. ";
}
else
valid = true;
}
valid = false;
while (valid == false)
{
cout << "Enter a second :";
cin >> s;
if (s < 0 || s > 59)
{
valid = false;
cout << "something is wrong with the input, please try it again. ";
}
else
valid = true;
}
myTime.setTime(h, s);
return myTime;
}
};
// main function to execute the program
int main()
{
MilTime time;
MilTime a1(1923, 12);
time = time.getTime("Enter Time in military time: ");
cout << " " << endl;
cout << "Military time is: " << time.getHour() << ":" << time.getSec() << endl;
cout << "Standard Time is: " << time.getStandHr() << ":" << time.getMin() << ":" << time.getSec() << time.getAMPM() << endl;
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
