设计一个时间类time要求:1包含时(hour)分(minute)秒(second)私有数据成员2包含构造函数,重载关于时间加上另一时间的加法运算符"+",重载输出运算符"<<",重载输入运算符">>"
时间: 2024-01-01 21:06:10 浏览: 128
class Time {
private:
int hour;
int minute;
int second;
public:
Time() {
hour = 0;
minute = 0;
second = 0;
}
Time(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
Time operator+(const Time& t) const {
Time sum;
sum.second = second + t.second;
sum.minute = minute + t.minute + sum.second / 60;
sum.second %= 60;
sum.hour = hour + t.hour + sum.minute / 60;
sum.minute %= 60;
return sum;
}
friend ostream& operator<<(ostream& os, const Time& t) {
os << t.hour << ":" << t.minute << ":" << t.second;
return os;
}
friend istream& operator>>(istream& is, Time& t) {
is >> t.hour >> t.minute >> t.second;
return is;
}
};
阅读全文