1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <iostream>
#include <string>
using namespace std;
class Time
{
protected: //protected do they will be known in 'ExtTime'
int hour, minute; //declare hour & min
char zone; //declare time zone
public:
Time() {} //calling function
Time(int h, int m, char zone); //initialize hr and min to input
void setHour(); //sets hours
void setMinute(); //sets mins
int getHour(){return hour;};
int getMinute(){return minute;};
void addHour();
void addMinute();
void printTime();
};
class ExtTime : public Time
{
public:
ExtTime();
ExtTime(int h, int m, string z);
void timeZone();
void printTime();
private:
string zone;
};
void ExtTime::timeZone()
{
if (zone == "PST")
cout << "The time is: " << (getHour()-3) << ":" << getMinute() << endl;
if (zone == "EST")
cout << "The time is: " << getHour() << ":" << getMinute() << endl;
if (zone == "CST")
cout << "The time is: " << (getHour()-1) << ":" << getMinute() << endl;
if (zone == "MST")
cout << "The time is: " << (getHour()-2) << ":" << getMinute() << endl;
}
Time::Time(int h, int m, char z)
{
hour = h;
minute = m;
}
ExtTime::ExtTime(int h, int m, string z)
{
zone = z;
hour = h;
minute = m;
}
void Time::setHour()
{
cout << "Enter the hour: ";
cin >> hour;
}
void Time::setMinute()
{
cout << "Enter the minutes: ";
cin >> minute;
}
void ExtTime::timeZone()
{
cout << "Enter time zone (EST, PST, MST, CST): ";
cin >> zone;
}
void Time::addHour()
{
hour++;
if(hour == 24)
hour = 0;
}
void Time::addMinute()
{
minute++;
if(minute == 60)
{hour++;
minute = 0;
if(hour == 24)
hour = 0;
}
}
void Time::printTime()
{
cout << "The time is: ";
if(hour < 10)
cout << "0";
cout << hour << ":";
if(minute < 10)
cout << "0";
cout << minute;
}
void ExtTime::printTime()
{
Time::printTime();
cout << " " << zone;
}
int main()
{
ExtTime tz(3, 4, "EST");
tz.printTime();
cout << endl;
Time myTime;
myTime.setHour();
myTime.setMinute();
int h = myTime.getHour();
int m = myTime.getMinute();
cout << "The hour is " << h << " and the minute is " << m << endl;
myTime.printTime();
cout << endl;
myTime.addHour();
myTime.addMinute();
cout << "After adding 1 hour and 1 minute is\n";
myTime.printTime();
cout << "\n\nPress 'Enter' to exit." << flush;
cin.sync(); //flush cin stream
cin.get();
return 0;
}
|