time.h : #include <iostream> using namespace std; class Time { protected:// int
ID: 3842573 • Letter: T
Question
time.h :
#include <iostream>
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;
}
int getMin() const
{
return min;
}
int getSec() const
{
return sec;
}
};
class MilTime : public Time
{
protected:
int milHours;
int milSeconds;
public:
MilTime(int h = 0, int s = 0) : Time()
{
if (h <= 2359 && h >= 0 && s <= 59 && s >= 0)
setTime(h, s);
else
cout << "Invalid input" << endl;
}
void setTime(int milH = 0, int milS = 0)
{
if (milH <= 2359 && milH >= 0 && milS <= 59 && milS >= 0)
{
milHours = milH;
milSeconds = milS;
sec = milSeconds;
min = milHours % 100;
if (milHours > 1259)
hour = (milHours - 1200) / 100;
else
hour = milHours / 100;
}
else
cout << "Invalid input" << endl;
}
int getHour() const
{
return milHours;
}
int getStandHr() const
{
return hour;
}
};
class TimeClock : public MilTime
{
private:
int startingTime;
int endingTime;
public:
TimeClock(int startTime = 0, int endTime = 0) : MilTime()
{
if (startTime >= 0 && startTime <= 2359 && endTime >= 0 && startTime <= 2359)
{
startingTime = startTime;
endingTime = endTime;
}
else
cout << "Invalid input" << endl;
}
void setStartTime(int startTime)
{
if (startTime >= 0 && startTime <= 2359)
startingTime = startTime;
else
cout << "Invalid input" << endl;
}
void setEndTime(int endTime)
{
if (endTime >= 0 && endTime <= 2359)
endingTime = endTime;
else
cout << "Invalid input" << endl;
}
TimeClock timeElapsed()
{
TimeClock obj;
obj.setTime (endingTime - startingTime) ;
return obj;
}
};
int main()
{
TimeClock TC;
TC.setStartTime(2200);
TC.setEndTime(2300);
cout << "Start time: 2200" << endl;
cout << "End time: 2300" << endl;
TC = TC.timeElapsed();
cout << TC.getStandHr() << " hour(s) and " << TC.getMin() << " minute(s) passed between " << endl;
TimeClock TC2(900, 1330);
cout << endl << "Start time: 900" << endl;
cout << "End time: 1330" << endl;
TC2 = TC2.timeElapsed();
cout << TC2.getStandHr() << " hour(s) and " << TC2.getMin() << " minute(s) passed between " << endl;
cout << endl << "These should generate an error: " << endl;
TimeClock TC3(2500, 0);
TimeClock TC4(2359, -1);
cout << "Press Enter to quit." << endl;
cin.ignore();
cin.get();
return 0;
}
//MilTime.cpp
#include "MilTime.h"
using namespace std;
MilTime::MilTime()
{
hour = 0;
sec = 0;
min = 0;
milHours = 0;
milSeconds = 0;
}
MilTime::~MilTime()
{
}
void MilTime::setTime(int mh, int ms) {
milHours = mh;
milSeconds = ms;
hour = mh % 100;
min = mh / 100;
sec = ms;
}
string MilTime::getStandHr() const {
return to_string(milHours / 100) + ":" + to_string(milHours % 100) + ":" + to_string(milSeconds);
}
_________________________
Source:
#include <iostream>
#include "MilTime.h"
using namespace std;
int main()
{
int h, s;
do {
cout << "MilHour: ";
cin >> h;
if (h<0 || h>2359) cout << "Invalid milHour" << endl;
} while (h<0 || h>2359);
do {
cout << "MilSecond: ";
cin >> s;
if (s<0 || s>59) cout << "Invalid milSecond" << endl;
} while (s<0 || s>59);
MilTime mt;
return 0;
}
MilTime.h;
--------------------------------------------------------
MilTime.h:
#ifndef MILTIME_H
#define MILTIME_H
#include "Time.h"
#include <string>
class MilTime : public Time
{
public:
MilTime();
~MilTime();
void setTime(int, int);
string getStandHr() const;
int getHour() const { return milHours; }//get hours
private:
int milHours;
int milSeconds;
};
#endif
// Specification file for the Time class
#ifndef 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
Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
//Bad Hour exception
class BadHour: public exception
{
public:
virtual const char* what() const throw()
{
return "Invalid hour";
}
};
//bad seconds exception
class BadSeconds: public exception
{
public:
virtual const char* what() const throw()
{
return "Invalid seconds";
}
};
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;
}
};
class MilTime : public Time
{
protected:
int milHours;
int milSeconds;
public:
MilTime(int h = 0, int s = 0) : Time()
{
if (h <= 2359 && h >= 0)
{
if(s <= 59 && s >= 0)
setTime(h, s);
else throw new BadSeconds;
}
else
throw new BadHour;
}
void setTime(int milH = 0, int milS = 0)
{
if (milH <= 2359 && milH >= 0)
{
if(milS <= 59 && milS >= 0)
{
milHours = milH;
milSeconds = milS;
sec = milSeconds;
min = milHours % 100;
if (milHours > 1259)
hour = (milHours - 1200) / 100;
else
hour = milHours / 100;
}
else throw new BadSeconds;
}
else
throw new BadHour;
}
int getHour() const
{
return milHours;
}
int getStandHr() const
{
return hour;
}
};
class TimeClock : public MilTime
{
private:
int startingTime;
int endingTime;
public:
TimeClock(int startTime = 0, int endTime = 0) : MilTime()
{
if (startTime >= 0 && startTime <= 2359 && endTime >= 0 && startTime <= 2359)
{
startingTime = startTime;
endingTime = endTime;
}
else
cout << "Invalid input" << endl;
}
void setStartTime(int startTime)
{
if (startTime >= 0 && startTime <= 2359)
startingTime = startTime;
else
cout << "Invalid input" << endl;
}
void setEndTime(int endTime)
{
if (endTime >= 0 && endTime <= 2359)
endingTime = endTime;
else
cout << "Invalid input" << endl;
}
TimeClock timeElapsed()
{
TimeClock obj;
obj.setTime (endingTime - startingTime) ;
return obj;
}
};
int main()
{
//modified the program to cathc exceptions
int h, s;
bool isContinued = false;
do {
try
{
isContinued = false;
cout << "MilHour: ";
cin >> h;
cout << " MilSecond: ";
cin >> s;
MilTime mt(h, s);
cout<<endl<<"Standard Time: "<<mt.getStandHr();
}catch (const BadHour *e1) {
cout<<endl<<e1->what()<<endl<<endl;
isContinued = true;
}catch (const BadSeconds *e2) {
cout<<endl<<e2->what()<<endl<<endl;
isContinued = true;
}
} while (isContinued);
return 0;
}
OUTPUT:
MilHour: 2500
MilSecond: 2345
Invalid hour
MilHour: 2300
MilSecond: 32
Standard Time: 11
As part of this question, change was made only in two files. MilTime.h and MilTime.cpp
MilTime.h:
#ifndef MILTIME_H
#define MILTIME_H
#include "Time.h"
#include <string>
//Bad Hour exception
class BadHour: public exception
{
public:
virtual const char* what() const throw()
{
return "Invalid hour";
}
};
//bad seconds exception
class BadSeconds: public exception
{
public:
virtual const char* what() const throw()
{
return "Invalid seconds";
}
};
class MilTime : public Time
{
public:
MilTime();
~MilTime();
void setTime(int, int);
string getStandHr() const;
int getHour() const { return milHours; }//get hours
private:
int milHours;
int milSeconds;
};
#endif
MilTime.cpp:
#include "MilTime.h"
using namespace std;
MilTime::MilTime()
{
hour = 0;
sec = 0;
min = 0;
milHours = 0;
milSeconds = 0;
}
MilTime::~MilTime()
{
}
void MilTime::setTime(int mh, int ms) {
if (mh<0 || mh>2359)
throw new BadHour;
if (ms<0 || ms>59)
throw new BadSeconds;
milHours = mh;
milSeconds = ms;
hour = mh % 100;
min = mh / 100;
sec = ms;
}
string MilTime::getStandHr() const {
return to_string(milHours / 100) + ":" + to_string(milHours % 100) + ":" + to_string(milSeconds);
}
int main()
{
//modified the program to cathc exceptions
int h, s;
bool isContinued = false;
do {
try
{
isContinued = false;
cout << "MilHour: ";
cin >> h;
cout << " MilSecond: ";
cin >> s;
MilTime mt(h, s);
cout<<endl<<"Standard Time: "<<mt.getStandHr();
}catch (const BadHour *e1) {
cout<<endl<<e1->what()<<endl<<endl;
isContinued = true;
}catch (const BadSeconds *e2) {
cout<<endl<<e2->what()<<endl<<endl;
isContinued = true;
}
} while (isContinued);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.