(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

Computer Science & Information Technology

You might also like to view...

Which of the following does not generate GUI events?

a. Typing in a text field. b. Selecting an item from a menu. c. Viewing the text in a label. d. Moving the mouse.

Computer Science & Information Technology

Which Windows 10 feature provides an automated diagnosis and repair of boot problems plus a centralized platform for advanced recovery tool?

A. Windows Preinstallation Environment (Windows PE) B. System Reset C. Windows Recovery Environment (WinRE) D. Recovery Drive

Computer Science & Information Technology

The Sum function can be used with which of the following data types:

A) Number. B) AutoNumber. C) Date/Time. D) OLE Object.

Computer Science & Information Technology

The method transfers the focus to a control.

a) GetFocus b) Focus c) Transfer d) Activate

Computer Science & Information Technology