Write a class TimeStamp that represents a time of the day. It must have variables to store the number of seconds, minutes and hours passed. It also must have a function to print all the values. You will also need to overload a few operators.
Task 1: Create a list of objects of class TimeStamp. Task 2: Insert 5 time values in the format ssmmhh
i. 15 34 23
ii. 13 13 02
iii. 43 45 12
iv. 25 36 17
v. 52 02 20
Task 3: Delete the timestamp 25 36 17
Task 4: Print the list
Output should be: 15:34:23
13:13:02
43:45:12
52:02:20
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class TimeStamp
{
public:
TimeStamp();
TimeStamp(int s, int m, int h);
void print();
bool operator == (TimeStamp T);
private:
int sec;
int min;
int hour;
};
void TimeStamp::print()
{
if (sec >= 10)
cout << sec;
else
cout << 0 << sec;
cout << " : ";
if (min >= 10)
cout << min;
else
cout << 0 << min;
cout << " : ";
if (hour >= 10)
cout << hour;
else
cout << 0 << hour;
}
TimeStamp::TimeStamp() {}
TimeStamp::TimeStamp(int s, int m, int h) : sec(s), min(m), hour(h) {}
bool TimeStamp::operator==(TimeStamp T)
{
return (this->sec == T.sec && this->min == T.min && this->hour == T.hour);
}
int main()
{
vector<TimeStamp> v;
v.push_back(TimeStamp(15, 34, 23));
v.push_back(TimeStamp(13, 13, 02));
v.push_back(TimeStamp(43, 45, 12));
v.push_back(TimeStamp(25, 36, 17));
v.push_back(TimeStamp(52, 02, 20));
cout << "Print list." << endl;
for (int i = 0; i < v.size(); i++)
{
v[i].print();
cout << endl;
}
TimeStamp toRemove(25, 36, 17);
cout << endl << "Delete 25 36 17." << endl;
for (int i = 0; i < v.size(); i++)
{
if (v[i] == toRemove)
{
v.erase(v.begin() + i);
i--;
}
}
cout << endl << "Print list again." << endl;
for (int i = 0; i < v.size(); i++)
{
v[i].print();
cout << endl;
}
return 0;
}
Comments
Leave a comment