This is my question:
(Enhancing Class Time) Provide a constructor that’s capable of using the current time from the time and localtime functions—declared in the C++ Standard Library header —to initialize an object of the Time class.
Here's my code: .h file
#ifndef TIME
#define TIME
class Time
{
public:
Time();
Time(int, int, int);
void Display();
private:
int hour, minute, second;
};
#endif // !1
.cpp file
#include "Time.h"
#include <ctime>
#include <iostream>
using namespace std;
Time::Time(){}
Time::Time(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
time_t currenttime;
struct tm timeinfo;
time(¤ttime);
localtime_s(&timeinfo, ¤ttime);
h = timeinfo.tm_hour;
m = timeinfo.tm_min;
s = timeinfo.tm_sec;
}
void Time::Display()
{
cout << hour << ":" << minute << ":" << second << endl;
}
main.cpp file
#include <iostream>
#include "Time.h"
#include <ctime>
int main()
{
Time currentTime;
currentTime.Display();
system("pause");
return 0;
}
The output:
-858993460:-858993460:-858993460