Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

9.5 (The Time class) Design a class named Time. The class contains: Data fields

ID: 3601743 • Letter: 9

Question

9.5 (The Time class) Design a class named Time. The class contains: Data fields hour, minute, and second that represent a time. A no-arg constructor that creates a Time object for the current time. A constructor that constructs a Time object with a specified elapse time since the middle of night, Jan 1, 1970, in seconds. A constructor that constructs a Time object with the specified hour, minute, and second. Three get functions for the data fields hour, minute, and second. A function named setTime(int elapseTime) that sets a new time for the object using the elapsed time. Draw hc tt igrr fvr the Implement the class. Write a test program that creates two Time objects, one using a no-arg constructor and the other using Time (555550), and display their hour, minute, and second. (Hint: The first two constructors will extract hour, minute, and second from the elapse time. For example, if the elapse time is 555550 seconds, the hour is 10, the minute is 19, and the second is 9. For the no-arg constructor, the current time can be obtained using time CO).)

Explanation / Answer

#include<iostream>

#include<time.h>

using namespace std;

class Time

{

int hour, minute, second;

public:

Time();

Time(Time &obj);

Time(time_t elapsedTime);

Time(int h, int m, int s);

int getHour() const;

int getMinute() const;

int getSeconds() const;

void setTime(time_t elapsedTime);

};

Time::Time()

{

time_t now = time(NULL);

second = now % 60;

minute = ((now - second) / 60) % 60;

hour = ((((now - second) / 60) - minute) / 60) % 24;

}

Time::Time(time_t elapsedtime)

{

second = elapsedtime % 60;

minute = ((elapsedtime - second) / 60) % 60;

hour = ((((elapsedtime - second) / 60) - minute) / 60) % 24;

}

Time::Time(int h, int m, int s)

{

hour = h;

minute = m;

second = s;

}

void Time::setTime(time_t elapsedTime)

{

second = elapsedTime % 60;

minute = ((elapsedTime - second) / 60) % 60;

hour = ((((elapsedTime - second) / 60) - minute) / 60) % 24;

}

int Time::getHour() const

{

return hour;

}

int Time::getMinute() const

{

return minute;

}

int Time::getSeconds() const

{

return second;

}

int main()

{

Time t1(555550);

Time t2;

//display time for t1

cout << "(t1)h:m:s -> " << t1.getHour() << ":" << t1.getMinute() << ":" << t1.getSeconds() << endl;

//display time for t2

cout << "(t2)h:m:s -> " << t2.getHour() << ":" << t2.getMinute() << ":" << t2.getSeconds() << endl;

//set elapsed time

t2.setTime(555550);

//display time for t2

cout << "(t2)h:m:s -> " << t2.getHour() << ":" << t2.getMinute() << ":" << t2.getSeconds() << endl;

//test setting hour, minute and second

Time t3(4, 5, 40);

//display time for t3

cout << "(t3)h:m:s -> " << t3.getHour() << ":" << t3.getMinute() << ":" << t3.getSeconds() << endl;

}

/*output

(t1)h:m:s -> 10:19:10

(t2)h:m:s -> 7:29:0

(t2)h:m:s -> 10:19:10

(t3)h:m:s -> 4:5:40

*/