C++. Can anyone help me write the program. It should look like the one on #3 (bottom of the page)
2 inde TIME 3 define TIE 4 finclude
Expert Answer
#include <cstdlib>
#include <iostream>
#include <string>
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;
clockType(int, int, int);
clockType();
private:
int hr;
int min;
int sec;
};
class zoneClockType: public clockType
{
public:
void setTimeZone(int, int, int, string);
void getTimeZone(int&, int&, int&, string, int&) const;
bool equalTime(const zoneClockType&) const;
void printTimeAndZone() const;
zoneClockType(int=0, int=0, int=0, string, string=”PST”);
zoneClockType();
private:
string timeZone;
int zoneVal;
};
using namespace std;
int main(int argc, char *argv[])
{
int hours, minutes, seconds;
string…=”PST”
clockType clock1;
clockType clock2( 23, 13, 75 );
clock1.setTime( 6, 59, 39 );
cout << “clock1: “;
clock1.printTimeAndZone();
cout << endl;
clock2.printTimeAndZone();
cout << endl;
clock1.incrementMinutes();
clock1.printTimeAndZone();
cout << endl;
clock1.setTime( 0, 13, 0 );
cout << “clock2: “;
clock2.printTime();
cout << endl;
clock2.setTime(5,45,16);
cout << “After setting, clock2: “;
clock2.printTime();
cout << endl;
if (clock1.equalTime(clock2))
cout << “Both times are equal.”
<< endl;
else
cout << “The two times are different.” << endl;
system(“PAUSE”);
return EXIT_SUCCESS;
}
void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}
void clockType::printTime() const
{
if (hr < 10)
cout << “0”;
cout << hr << “:”;
if (min < 10)
cout << “0”;
cout << min << “:”;
if (sec < 10)
cout << “0”;
cout << sec;
}
void clockType::incrementSeconds()
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes();
}
}
bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
clockType::clockType(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}
clockType::clockType()
{
hr = 0;
min = 0;
sec = 0;
}
void zoneClockType::getTimeZone()
{
hours=hr;
minutes=min;
seconds=sec;
if(timeZone=”EST”)
zoneVal=3
if(timeZone=”CST”)
zoneVal=2
if(timeZone=”MST”)
zoneVal=1
if(timeZone=”PST”)
zoneVal=0
}
void zoneClockType::printTimeAndZone()
{
clockType.printTime();
getTimeZone();
}
bool zoneClockType::equalTimeZone(const zoneClockType&) const
{
int h1, h2, m1, m2, s1, s2, v1, v2;
string z1, z2;
.
.
getTime(h1, m1, s1, z1, v1);
otherClock.getTime(h2, m2, s2, z2, v2);
if((m1==m2)&&(s1==s2))
if((h1-h2)-(v1-v2)==0)
return true;
else
return false;
else
return false;
zoneClockType::zoneClockType()
{
clockType();
setTimeZone(false);
}
zoneClockType::zoneClockType(int h, int m, int s)
{
setTime( h, m, s );
setTimeZone(true);
}