The class clockType was designed to implement the time of day in a program. Cert
ID: 3636759 • Letter: T
Question
The class clockType was designed to implement the time of day in a program. Certain applications, might require you to also store the time zone. Derive the class extClockType from the class clockType by adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors.
I also need a test program to test the class but I will post a second question for that part.
class clockType
{public:
void setTime(int, int,int);
void getTime (int&, int&, int&) const;
void printTime() const;
void incrementSeconds();
void incrementMinutes();
void incrementHours();
bool equalTime(const clockType&) const;
private:
int hr;
int min;
int sec;
};
Explanation / Answer
please rate - thanks
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class clockType
{
public:
clockType(int,int,int);
clockType();
void setTime(int, int,int);
void getTime (int&, int&, int&) const;
void printTime() const;
void incrementSeconds();
void incrementMinutes();
void incrementHours();
bool equalTime(const clockType&) const;
private:
int hr;
int min;
int sec;
};
void clockType::setTime(int h,int m,int s)
{if(h>=0&&h<24)
hr=h;
else
hr=0;
if (m>=0&&m<60)
min=m;
else
min=0;
if (s>=0&&s<60)
sec=s;
else
sec=0;
}
void clockType::getTime(int& h,int& m,int& s)const
{h=hr;
m=min;
s=sec;
}
void clockType::printTime() const
{cout.width(2);
cout.fill('0');
cout<<hr<<":";
cout.width(2);
cout.fill('0');
cout<<min<<":";
cout.width(2);
cout.fill('0');
cout<<sec;
}
void clockType::incrementHours()
{hr=(hr+1)%24;
}
void clockType::incrementSeconds()
{sec=(sec+1)%60;
if(sec==0)
incrementMinutes();
}
void clockType::incrementMinutes()
{min=(min+1)%60;
if(min==0)
incrementHours();
}
bool clockType::equalTime(const clockType& c) const
{if(hr==c.hr&&min==c.min&&sec==c.sec)
return true;
return false;
}
clockType::clockType(int h,int m,int s)
{setTime(h,m,s);
}
clockType::clockType()
{setTime(0,0,0);
}
class extClockType: public clockType
{public:
void setTime(int,int,int,string);
void printTime();
extClockType(int=0,int=0,int=0,string="EST");
private:
string timeZone;
};
void extClockType::setTime(int h,int m,int s,string zone)
{clockType::setTime(h,m,s);
timeZone=zone;
}
void extClockType::printTime()
{clockType::printTime();
cout<<" "<<timeZone;
}
extClockType::extClockType(int h,int m,int s,string zone):clockType(h,m,s)
{timeZone=zone;
}
int main()
{extClockType t1;
extClockType t2(23,59,59,"MST");
cout<<"All times are based on a 24 hour clock ";
cout<<"Time 1: as initialized with default ";t2.incrementSeconds();
cout<<"After incrementing time 2 by one second ";
t2.printTime();
cout<<endl;
t1.printTime();
cout<<endl;
t1.incrementSeconds();
cout<<"After incrementing time 1 by one second ";
t1.printTime();
cout<<endl;
cout<<"Time 2 as initilized ";
t2.printTime();
cout<<endl;
t2.incrementSeconds();
cout<<"After incrementing time 2 by one second ";
t2.printTime();
cout<<endl;
t2.setTime(8,9,10,"PST");
cout<<"Time 2 after setting time: ";
t2.printTime();
cout<<endl;
t2.incrementHours();
cout<<"After incrementing time 2 by one hour: ";
t2.printTime();
cout<<endl;
system("pause");
return 0;
}//end main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.