In chapter 11, the class clocktype was designed to implement the time of day in
ID: 3625218 • Letter: I
Question
In chapter 11, the class clocktype was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derice the class extclocktype from the class clocktype by adding a member variable to store the time zone. Add the necessary member functions and contructors to make the class functional. Also write the definitions of the member functions amd the contructors. Finally, write a test program to test your class.Explanation / Answer
#include<iostream>
using namespace std;
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();
clockType( int, int, int );
private:
int hr;
int min;
int sec;
}; // end definition of clockType
// ----------------------------------------------------
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;
} // end function setTime
void clockType::getTime( int& hours, int& minutes,
int& seconds ) const
{
hours = hr;
minutes = min;
seconds = sec;
} // end function getTime
void clockType::printTime() const
{
cout << " The time is ";
if ( hr < 10 )
cout << "0";
cout << hr << ":";
if ( min < 10 )
cout << "0";
cout << min << ":";
if ( sec < 10 )
cout << "0";
cout << sec;
} // end function printTime
void clockType::incrementHours()
{
hr++;
if ( hr > 23 )
hr = 0;
} // end function incrementHours
void clockType::incrementMinutes()
{
min++;
if ( min > 59 )
{
min = 0;
incrementHours();
} // end if
} // end function incrementMinutes
void clockType::incrementSeconds()
{
sec++;
if ( sec > 59 )
{
sec = 0;
incrementMinutes();
} // end if
} // end function incrementSeconds
bool clockType::equalTime( const clockType& otherClock ) const
{
return ( hr == otherClock.hr &&
min == otherClock.min &&
sec == otherClock.sec );
} // end function equalTime
clockType::clockType()
{
hr = 0;
min = 0;
sec = 0;
} // end default constructor
clockType::clockType( int hours, int minutes, int seconds )
{
setTime( hours, minutes, seconds );
} // end parameterized constructor
#include<iostream>
#include "clockType.h"
using namespace std;
class extClockType : public clockType
{
public :
extClockType();
extClockType( int h, int m, int s );
void setTimeZone( bool zone );
void displayTimeZone();
void printTimeAndZone();
private :
int timeZone;
}; // end
#endif
extClockType::extClockType()
{
clockType();
setTimeZone( false );
} // end default constructor
extClockType::extClockType( int h, int m, int s )
{
setTime( h, m, s );
setTimeZone( true );
} // end parameterised constructor
void extClockType::setTimeZone( bool zone )
{
if ( !zone )
{
timeZone = 0;
return;
} // end if
int temp;
// prompt and read time-zone hours
cout << " Enter time zone difference.";
cout << " Enter hours : ";
cin >> temp;
// set the time zone hours
timeZone = temp * 60;
// prompt and read from user time-zone minutes
cout << " Enter minutes : ";
cin >> temp;
// modify time-zone with minutes
timeZone += temp;
} // end function setTimeZone
void extClockType::displayTimeZone()
{
cout << " in hours "
<< ( timeZone / 60 ) << ":"
<< ( timeZone % 60 ) << " mins time zone";
} // end function displayTimeZone
void extClockType::printTimeAndZone()
{
printTime(); // print the time
displayTimeZone(); // print time-zone
} // end function printTimeAndZone
int main() // function main begins program execution
{
// let the user know about the program
cout << " A program that works with extClockType.";
// declare variables
extClockType clock1;
extClockType clock2( 23, 13, 75 );
clock1.printTimeAndZone();
cout << endl << endl;
clock2.printTimeAndZone();
cout << endl << endl;
clock1.setTime( 6, 59, 39 );
clock1.printTimeAndZone();
cout << endl;
clock1.incrementMinutes();
clock1.printTimeAndZone();
cout << endl;
clock1.setTime( 0, 13, 0 );
if ( clock1.equalTime( clock2 ) )
cout << " Clock1 time is the same as clock2 time";
else
cout << " The two times were different.";
return 0; // indicate program executed successfully
} // end of function, main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.