Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Modify the Time class of Figure 16.

10 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 driver 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)
// Section 16, Question 8
// time.h
#ifndef time_h
#define time_h
class Time {
public:
Time( int = 0, int = 0, int = 0 );
void setTime( int, int, int );
void setHour( int );
void setMinute( int );
void setSecond( int );
int getHour();
int getMinute();
int getSecond();
void printStandard();
void tick( const int );
private:
int hour;
int minute;
int second;
};
#endif
// Section 16, Question 8
// time.cpp
#include <iostream>
using std::cout;
#include "time.h"

Time::Time( int hr, int min, int sec )


{ setTime( hr, min, sec ); }
void Time::setTime( int h, int m, int s )
{
setHour( h );
setMinute( m );
setSecond( s );
}
void Time::setHour( int h)
{ hour = ( h >= 0 && h < 24 ) ? h : 0; }
void Time::setMinute( int m)
{ minute = ( m >= 0 && m < 60 ) ? m : 0; }
void Time::setSecond( int s)
{ second = ( s >= 0 && s < 60 ) ? s : 0; }
int Time::getHour() { return hour; }
int Time::getMinute() { return minute; }
int Time::getSecond() { return second; }
void Time::printStandard()
{
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
<< ":" << ( minute < 10 ? "0" : "" ) << minute
<< ":" << ( second < 10 ? "0" : "" ) << second
<< ( hour < 12 ? " AM" : " PM" );
}
void Time::tick( const int count )
{
cout << "\n\nIncrementing second " << count
<< " times:\nStart Time: ";
printStandard();
for ( int i = 0; i < count; i++ ) {
setSecond( ( getSecond() + 1 ) %60 );
if ( getSecond() == 0 ) {
setMinute( ( getMinute() + 1 ) % 60 );
if ( getMinute() == 0 )

setHour( ( getHour() + 1 ) % 24 );
}
cout << "\nsecond + 1: ";
printStandard();
}
}

// Section 16, Question 8


// main.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "time.h"
int main()
{
Time t;
t.setTime( 11, 59, 55 );
cout << "Time is set to: ";
t.printStandard();
t.tick( 10 );
return 0;
}

Time is set to: 11:59:55 AM


Incrementing second 10 times:
Start Time: 11:59:55 AM
second + 1: 11:59:56 AM
second + 1: 11:59:57 AM
second + 1: 11:59:58 AM
second + 1: 11:59:59 AM
second + 1: 12:00:00 PM
second + 1: 12:00:01 PM
second + 1: 12:00:02 PM
second + 1: 12:00:03 PM
second + 1: 12:00:04 PM
second + 1: 12:00:05 PM

You might also like