c++运算符重载
#include<iostream.h>class time
{
private:
int hour;
int minute;
int second;
public:
time();
time(int h,int m,int s); //初始化
time operator++(time &x); //完成秒数上加一的功能
friend ostream &operator<<(ostream &out,time c);
};
time::time()
{
hour=12;
minute=0;
second=0;
}
time::time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
time operator++(time &x);
{
x.second+=1;
return x;
}
ostream &operator<<(ostream &out,time c)
{
if(c.minute<10&&c.second>=10)
out<<c.hour<<":"<<"0"<<c.minute<<":"<<c.second<<endl;
else if(c.minute<10&&c.second<10)
out<<c.hour<<":"<<"0"<<c.minute<<":"<<"0"<<c.second<<endl;
else if(c.minute>=10&&c.second>=10)
out<<c.hour<<":"<<c.minute<<":"<<c.second<<endl;
else
out<<c.hour<<":"<<c.minute<<":"<<"0"<<c.second<<endl;
return out;
}
void main()
{
time t1(13,25,36),t2;
cout<<t1<<endl;
cout<<t2<<endl;
}