题解 | 加号运算符重载
加号运算符重载
https://www.nowcoder.com/practice/b9e27fcf61fc4013875409ed78e0960b
#include <iostream>
using namespace std;
class Time {
public:
int hours; // 小时
int minutes; // 分钟
Time() {
hours = 0;
minutes = 0;
}
Time(int h, int m) {
this->hours = h;
this->minutes = m;
}
void show() {
cout << hours << " " << minutes << endl;
}
// write your code here......
Time operator+(Time &time1)
{
Time time2;
if((this->minutes+time1.minutes)>=60)
{
time2.minutes = this->minutes+time1.minutes-60;
time2.hours = this->hours + time1.hours +1;
}
else {
time2.minutes = this->minutes+time1.minutes;
time2.hours = this->hours + time1.hours;
}
return time2;
}
};
int main() {
int h, m;
cin >> h;
cin >> m;
Time t1(h, m);
Time t2(2, 20);
Time t3 = t1 + t2;
t3.show();
return 0;
}

