(Enhancing Class Time) Modify the Time class of Figs. 9.10–9.11 to include a tick member function that increments the time stored in a Time object by one second. The Time object should always remain in a consistent state. Write a program that tests the tick member function in a loop that prints the time in standard format during each iteration of the loop to illustrate that the tick member

function works correctly. Be sure to test the following cases:

a) Incrementing into the next minute.
b) Incrementing into the next hour.
c) Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).


```
#ifndef TIME_H
#define TIME_H

class Time
{
public:
Time( int = 0, int = 0, int = 0 ); // default constructor

// set functions
void setTime( int, int, int ); // set hour, minute, second
void setHour( int ); // set hour (after validation)
void setMinute( int ); // set minute (after validation)
void setSecond( int ); // set second (after validation)

// get functions
int getHour(); // return hour
int getMinute(); // return minute
int getSecond(); // return second

void tick(); // increment one second
void printUniversal(); // output time in universal-time format
void printStandard(); // output time in standard-time format
private:
int hour; // 0 - 23 (24-hour clock format)
int minute; // 0 - 59
int second; // 0 - 59
}; // end class Time

#endif
```
```
// Member-function definitions for class Time.
#include
#include
#include "Time.h" // include definition of class Time from Time.h
using namespace std;

// Time constructor initializes each data member to zero;
// ensures that Time objects start in a consistent state
Time::Time( int hr, int min, int sec )
{
setTime( hr, min, sec ); // validate and set time
} // end Time constructor

// set new Time value using universal time; ensure that
// the data remains consistent by setting invalid values to zero
void Time::setTime( int h, int m, int s )
{
setHour( h ); // set private field hour
setMinute( m ); // set private field minute
setSecond( s ); // set private field second
} // end function setTime

// set hour value
void Time::setHour( int h )
{
hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour
} // end function setHour

// set minute value
void Time::setMinute( int m )

{ minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute
} // end function setMinute

// set second value
void Time::setSecond( int s )
{
second = ( s >= 0 && s < 60 ) ? s : 0; // validate second
} // end function setSecond

// return hour value
int Time::getHour()
{
return hour;
} // end function getHour

// return minute value
int Time::getMinute()
{
return minute;
} // end function getMinute

// return second value
int Time::getSecond()
{
return second;
} // end function getSecond

// increment one second
void Time::tick()
{
setSecond( getSecond() + 1 ); // increment second by 1

if ( getSecond() == 0 )
{
setMinute( getMinute() + 1 ); // increment minute by 1

if ( getMinute() == 0 )
setHour( getHour() + 1 ); // increment hour by 1
} // end if
} // end function tick

// print Time in universal-time format (HH:MM:SS)
void Time::printUniversal()
{
cout << setfill( '0' ) << setw( 2 ) << getHour() << ":"
<< setw( 2 ) << getMinute() << ":" << setw( 2 ) << getSecond();
} // end function printUniversal

// print Time in standard-time format (HH:MM:SS AM or PM)
void Time::printStandard()
{
cout << ( ( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 )
<< ":" << setfill( '0' ) << setw( 2 ) << getMinute()
<< ":" << setw( 2 ) << getSecond() << ( hour < 12 ? " AM" : " PM" );
} // end function printStandard
```
```
#include
#include "Time.h" // include definition of class Time
using namespace std;

const int MAX_TICKS = 30;

int main()
{
Time t; // instantiate object t of class Time

t.setTime( 23, 59, 57 ); // set time

// output Time object t's values
for ( int ticks = 1; ticks < MAX_TICKS; ++ticks )
{
t.printStandard(); // invokes function printStandard
cout << endl;
t.tick(); // invokes function tick
} // end for
} // end main
```
11:59:57 PM
11:59:58 PM
11:59:59 PM
12:00:00 AM
12:00:01 AM
.
.
.

Computer Science & Information Technology

You might also like to view...

What is a source repository? Write down the sequence of commands needed to import the files to be versioned (assume some file names). Place these files in the module myproject. Assume that the source repository is ~/cvsroot.

What will be an ideal response?

Computer Science & Information Technology

A record can be implemented as a _____________, a ________ or a ___________ in Python.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

Every function must return one or more values.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Authentication is the part of the operating system that determines what resources can be accessed and used across the network

Indicate whether the statement is true or false

Computer Science & Information Technology