the ctime header file contains a special function called time(0) which returns t
ID: 3687848 • Letter: T
Question
the ctime header file contains a special function called time(0) which returns the number of seconds since 1/1/1970 when the function is called.
in Exercise 7, you will test this function as following:
create a class called TimeC, this class contains:
- a private integer call it creation
- default constructor, assign time(0) to creation
- a method that will return creation value, call it getCreationTime() (the method should return an integer)
- a method to calculate the age (call it age and it returns an integer). in this method, a new value for time(0) will be calculated. the previous creation value should be subtracted from it. [time(0)-creation]
test your class as following:
- creating an TimeC object
- print the creation value by calling the method getCreationTime()
- print a waiting message on the screen to hold the execution for few seconds.
* use system("pause"); or cin.get(x);
[for cin.get(x) you will need to create a char variable called x]
- print the age value by calling the function age();
Explanation / Answer
#include <iostream>
using namespace std;
class TimeC
{
private:
int creation;
public:
TimeC()
{
creation=time(0);
}
int getCreationTime()
{
return creation;
}
int age()
{
return time(0)-creation;
}
};
int main()
{
TimeC timec;
cout << "Object creation at: " <<timec.getCreationTime()<< endl;
cout<<"Please wait for few seconds...";
cin.get();
cout<<"Age of object is: "<<timec.age()<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.