定义时间类
class tm{
int h, m, s;
//时分秒
};
重载运算符:
+:两个时间对象相加,表示时分秒各自相加
时间对象[ o]取出时,对象[1]取出分
#include <iostream>
using namespace std;
int temp;
class TM{
public:
TM():sec(0),min(0),hour(0){cout<<"无参数初始化"<<endl;}
TM(int sec,int min,int hour){this->sec=sec;this->min=min;this->hour=hour;cout<<__LINE__<<endl;}
TM operator+ (TM &obj)
{
TM reobj;
reobj.sec=this->sec+obj.sec;
reobj.min=this->min+obj.min;
reobj.hour=this->hour+obj.hour;
return reobj;
}
int &operator[] (int i)
{
if(i==0){temp=this->hour;}
else if(i==1){temp=this->min;}
return temp;
}
void show(){cout<<hour<<"时 "<<min<<"分 "<<sec<<"秒"<<endl;}
private:
int sec;
int min;
int hour;
};
int main(int argc, const char *argv[])
{
TM obj1(0,0,15);
TM obj2(1,1,1);
TM obj3;
obj3=obj1+obj2;
obj3.show();
cout<<obj1 [0]<<endl;
return 0;
}