Assume the existence of a Time class with the class specification shown below. A
ID: 3546223 • Letter: A
Question
Assume the existence of a Time class with the class specification shown below. Also assume a derived class extendedTime was defined that contains a single additional data member named timeZone (of type string) to hold the time zone (e.g. Eastern Time Zone ETZ, Central Time Zone CTZ, etc) . The derived class also has a constructor that accepts two integer parameters and one string parameter which are used to initialize the hour, minutes and timeZone data members (ETZ is the default time zone), a set function name setTime to set the time (hour, minute, and time zone), a function named getTimeZone that returns the timeZone, and a function named write to display the time on the standard output device in the format hour:minute followed by the time zone.
For this question write the implementation of the write() function of class extendedTime.
class Time {
private:
int hour;
int minute;
public:
Time(int hr=0, int mn= 0); // initializes hour to hr and minute to mn
void setTime(int hr. int mn); // sets the time to hr:mn
int getHour(); // returns hour
int getMinute(); // returns minute
void write(); // displays the time on the standard output device in the format hour:minute.
};
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
class Time {
private:
int hour;
int minute;
public:
Time(int hr=0, int mn= 0) // initializes hour to hr and minute to mn
{
setTime(hr,mn);
}
void setTime(int hr, int mn) // sets the time to hr:mn
{
hour = hr;
minute = mn;
}
int getHour() // returns hour
{
return hour;
}
int getMinute()// returns minute
{
return minute;
}
void write() // displays the time on the standard output device in the format hour:minute.
{
cout << hour << " : " << minute<< endl;
}
};
class extendedTime:public Time
{
private:
string timeZone;
public:
extendedTime(int hr=0,int mn=0,string zone="ETZ"):Time(hr,mn),timeZone(zone)
{
}
void setTime(int hr,int mn,string Zone)
{
Time::setTime(hr,mn);
timeZone = Zone;
}
string getTimeZone() { return timeZone; }
void write()
{
cout << getHour() << " : " << getMinute() << " " << timeZone << endl;
}
};
int main()
{
Time T1(8,34);
T1.write();
extendedTime ET1(4,23);
ET1.write();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.