定义一个时间类Time,其数据成员包括,分和秒,成员函数包括1)输入函数input,从键盘读取数据成员的数值;2)输出函数show,输出格式为例如:“25:46”3)对“-”号进行运算符重载,返回两个Time对象之间相差的秒数(返回结果为数值型)。要求编写main函数,定义两个对象T1和T2,从键盘输入对象的数值,输出两个对象之间相差的秒数
#include <iostream>
using namespace std;
class Time {
private:
int minute, second;
public:
void input() {
cout << "Enter minutes: ";
cin >> minute;
cout << "Enter seconds: ";
cin >> second;
}
void show() {
cout << minute << ":" << second << endl;
}
int operator-(Time t) {
int sec1 = minute * 60 + second;
int sec2 = t.minute * 60 + t.second;
return sec1 - sec2;
}
};
int main() {
Time T1, T2;
cout << "Enter the time for T1: " << endl;
T1.input();
cout << "Enter the time for T2: " << endl;
T2.input();
cout << "T1: ";
T1.show();
cout << "T2: ";
T2.show();
int diff = T1 - T2;
cout << "The difference between T1 and T2 is " << diff << " seconds." << endl;
return 0;
}