I am trying to implement a time class which sets time, prints it and increases by one second. I want to make it by overloading the ++ operator, and it works fine but only when I define an argument in the parameter list. How can I make it work without defining any parameters, since I know that it increases the current time by one second and I don't need any arguments?
#include <iostream>
using namespace std;
class time
{
int h,m,s;
public:
time(int a, int b, int c);
void printtime();
time& operator++(int);
};
[...]
time& time::operator++(int x)
{
s++;
if (s>59) {
m++;
s -= 60;
if (m>59) {
h++;
m -= 60;
if (h>23) {
h = m = s = 0;
}
}
}
return *this;
}
int main()
{
try {
time now(22,58,59);
now.printtime();
now++;
now.printtime();
}
catch(const char* u) {
cout << u << endl;
}
return 0;
}
Also, I will need to implement the postfix operator, which increases time, but only after printing the old time, so that
time now(2,23,59);
(now++).printtime();
will print 2:23:59, but after that the value of now will be 3,0,0. How can I implement both prefix and postfix ++ operators and make a difference when calling them in the main function?