加号重载
class Clock
{
private:
int Time;
int Minute;
int Seecond;
public:
Clock();
Clock(int time, int minute, int seecond);
Clock operator++(int);
//ostream流 operator重载运算符关键字
friend std::ostream &operator <<(std::ostream &out,const Clock& b);
Clock::Clock(){}
std::ostream &operator <<(std::ostream &out, Clock& b)
{
return out <<b.Time << "h" << b.Minute << "m" <<b.Seecond << "s"<< std::endl;
}
Clock::Clock(int time, int minute, int seecond)
:Time(time),Minute(minute),Seecond(seecond){
}
//后缀++,如果++写前面就不需要int
Clock Clock::operator++(int)
{
this->Seecond++;
if (this->Seecond == 59)
{
this->Minute++;
this->Seecond = 0;
if (this->Minute == 59)
{
this->Time++;
this->Minute = 0;
}
if (this->Time == 24)
{
this->Time = 0;
this->Minute = 0;
this->Seecond = 0;
}
return Clock(Time,Minute,Seecond);
}
}
void main()
{
Clock b(0,0,1);
while(1)
{
b++;
//std::cout << b;
Sleep(100);
system("cls");//cls清空界面
}