(Enhancing Class Time) Provide a constructor that is capable of using the current time from the time and localtime functions—declared in the C++ Standard Library header —to ini- tialize an object of the Time class.
What will be an ideal response?
```
#ifndef TIME_H
#define TIME_H
class Time
{
public:
Time(); // constructor
void setTime( int, int, int ); // set hour, minute and second
void printUniversal(); // print time in universal-time format
void printStandard(); // print 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
#include "Time.h" // include definition of class Time from Time.h
using namespace std;
Time::Time()
{
const time_t currentTime = time( 0 );
const tm *localTime = localtime( ¤tTime );
setTime( localTime->tm_hour, localTime->tm_min, localTime->tm_sec );
} // 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 )
{
hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour
minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute
second = ( s >= 0 && s < 60 ) ? s : 0; // validate second
} // end function setTime
// print Time in universal-time format (HH:MM:SS)
void Time::printUniversal()
{
cout << setfill( '0' ) << setw( 2 ) << hour << ":"
<< setw( 2 ) << minute << ":" << setw( 2 ) << second;
} // end function printUniversal
// print Time in standard-time format (HH:MM:SS AM or PM)
void Time::printStandard()
{
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":"
<< setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 )
<< second << ( hour < 12 ? " AM" : " PM" );
} // end function printStandard
```
```
#include
#include "Time.h"
using namespace std;
int main()
{
Time t; // create Time object
// display current time
cout << "The universal time is ";
t.printUniversal();
cout << "\nThe standard time is ";
t.printStandard();
cout << endl;
} // end main
```
The universal time is 14:54:06
The standard time is 2:54:06 PM
You might also like to view...
Richard just purchased a new computer. During the initial setup, he noticed he was given Microsoft Office. Most likely, this is a free trial for Richard known as ________
Fill in the blank(s) with correct word
A(n) ____ is a list of homogenous elements in which the addition and deletion of elements occurs only at one end.
A. stack B. queue C. array D. linked list