(Time Class Modification) It would be perfectly reasonable for the Time class of Figs. 10.17– 10.18 to represent the time internally as the number of seconds since midnight rather than the three integer values hour, minute and second. Clients could use the same public methods and get the same results. Modify the Time class of Fig. 10.17 to implement the time as the number of seconds since

midnight and show that there is no visible change in functionality to the clients of the class. [Note: This exercise nicely demonstrates the virtues of implementation hiding.]

What will be an ideal response?


```
// Time class definition; Member functions defined in Time.cpp.
#ifndef TIME_H
#define TIME_H

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

// set functions (the Time & return types enable cascading)
Time &setTime( int, int, int ); // set hour, minute, second
Time &setHour( int ); // set hour
Time &setMinute( int ); // set minute
Time &setSecond( int ); // set second

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

// print functions (normally declared const)
void printUniversal() const; // print universal time
void printStandard() const; // print standard time
private:
int totalSeconds; // number of seconds since midnight
}; // end class Time

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

// constructor function to initialize private data;
// calls member function setTime to set variables;
// default values are 0 (see class definition)
Time::Time( int hr, int min, int sec )
{
setTime( hr, min, sec );
} // end Time constructor

// set values of hour, minute, and second
Time &Time::setTime( int h, int m, int s ) // note Time & return
{
setHour( h );
setMinute( m );
setSecond( s );
return *this; // enables cascading
} // end function setTime

// set hour value
Time &Time::setHour( int h ) // note Time & return
{
int hours = ( h >= 0 && h < 24 ) ? h : 0;
totalSeconds = ( hours * 3600 ) + ( getMinute() * 60 ) + getSecond();
return *this; // enables cascading
} // end function setHour

// set minute value
Time &Time::setMinute( int m ) // note Time & return
{
int minutes = ( m >= 0 && m < 60 ) ? m : 0;
totalSeconds = ( getHour() * 3600 ) + ( minutes * 60 ) + getSecond();
return *this; // enables cascading
} // end function setMinute

// set second value
Time &Time::setSecond( int s ) // note Time & return
{
int seconds = ( s >= 0 && s < 60 ) ? s : 0;
totalSeconds = ( getHour() * 3600 ) + ( getMinute() * 60 ) + seconds;
return *this; // enables cascading
} // end function setSecond

// get hour value
int Time::getHour() const
{
return ( totalSeconds / 3600 );
} // end function getHour

// get minute value
int Time::getMinute() const
{
return ( ( totalSeconds % 3600 ) / 60 );
} // end function getMinute

// get second value
int Time::getSecond() const
{
return ( ( totalSeconds % 3600 ) % 60 );
} // end function getSecond

// print Time in universal-time format (HH:MM:SS)
void Time::printUniversal() const
{
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() const
{
int hour = getHour();
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
<< ":" << setfill( '0' ) << setw( 2 ) << getMinute()
<< ":" << setw( 2 ) << getSecond() << ( hour < 12 ? " AM" : " PM" );
} // end function printStandard
```
```
// Driver program for Time class.
#include
#include "Time.h" // Time class definition
using namespace std;

int main()
{
Time t; // create Time object

// cascaded function calls
t.setHour( 18 ).setMinute( 30 ).setSecond( 22 );

// output time in universal and standard formats
cout << "Universal time: ";
t.printUniversal();

cout << "\nStandard time: ";
t.printStandard();

cout << "\n\nNew standard time: ";

// cascaded function calls
t.setTime( 20, 20, 20 ).printStandard();
cout << endl;
} // end main
```
Universal time: 18:30:22
Standard time: 6:30:22 PM
New standard time: 8:20:20 PM

Computer Science & Information Technology

You might also like to view...

A(n) ________ is a variable that controls the number of times a loop iterates.

A) counter B) accumulator C) sentinel D) total E) loop control variable

Computer Science & Information Technology

How do you work with Microsoft software that is available as an ISO image file?

What will be an ideal response?

Computer Science & Information Technology

If you switch to ____ mode, Photoshop will display a warning.

a. RGB b. CMYK c. Indexed d. Grayscale

Computer Science & Information Technology

The Windows operating system simplifies the process of working with documents and apps by organizing the manner in which you interact with the computer.?

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

Computer Science & Information Technology