++ and +=
Completed
Write class Time (hours, minutes) with prefix ++ (adds one minute), += (adds minutes) and << printing HH:MM on a 24-hour clock. Read a start time and commands tick or add M; print the time after each.
Input
Line 1: HH MM. Line 2: n. Then n commands.
Output
n lines.
Example
Input
23 58 3 tick tick add 90
Output
23:59 00:00 01:30
Editor
C++
Hints
Store the time as minutes since midnight and wrap with % 1440.
Prefix ++ returns *this by reference.
Solution explanation
Implementing ++ in terms of += avoids duplicating logic.
Show solution
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Time {
public:
Time(int h, int m) : minutes_((h * 60 + m) % 1440) {}
Time &operator++() { return *this += 1; }
Time &operator+=(int m) {
minutes_ = ((minutes_ + m) % 1440 + 1440) % 1440;
return *this;
}
friend ostream &operator<<(ostream &out, const Time &t) {
return out << setfill('0') << setw(2) << t.minutes_ / 60 << ":" << setw(2) << t.minutes_ % 60;
}
private:
int minutes_;
};
int main() {
int h, m, n;
cin >> h >> m >> n;
Time t(h, m);
for (int i = 0; i < n; i++) {
string cmd;
cin >> cmd;
if (cmd == "tick") {
++t;
} else {
int mins;
cin >> mins;
t += mins;
}
cout << t << "\n";
}
return 0;
}