#include <iostream>
using namespace std;
class timer {
public:
timer():sec(0),min(0),hour(0){}
timer(int sec,int min,int hour){this->sec = sec;this->min = min;this->hour = hour;}
timer operator+(timer &t)
{
timer temp;
temp.sec = t.sec + this->sec;
temp.min = t.min + this->min;
temp.hour = t.hour + this->hour;
return temp;
}
timer operator-(timer &t)
{
timer temp;
temp.sec = t.sec - this->sec;
temp.min = t.min - this->min;
temp.hour = t.hour - this->hour;
return temp;
}
timer operator+(int n)
{
timer temp;
temp.sec = this->sec + n;
return temp;
}
int &operator[](int n)
{
timer temp;
if(n == 0)
{
temp.hour = this->hour;
}
else if(n == 1)
{
temp.min = this->min;
}
else
{
temp.sec = this->sec;
}
return temp;
}
void show()
{
cout<<"hour:"<<this->hour<<" min:"<<this->min<<" sec:"<<this->sec<<endl;
}
private:
int sec;
int min;
int hour;
};
int main(int argc, const char *argv[])
{
timer t1;
timer t3;
t3 = t1 + 2;
t3.show();
timer t[0];
t.show();
return 0;
}
C++第二天作业
于 2024-01-04 19:54:24 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)