//单目运算符++
#include<iostream>
using namespace std;
class clock
{public:
clock(){h=0;s=0;m=0;}
clock(int h,int m,int s)
{
this->h=h;
this->m=m;
this->s=s;
}
void display();
clock operator++();//++a;//前置单目运算符重载成员函数
clock operator++(int);//a++
private:
int h;
int s;
int m;
};
void clock::display()
{
cout<<h<<" "<< m<<" "<<s<<endl;
}
clock clock::operator++()
{
s++;
if(s>=60)
{
s-=60;
m++;
if(m>=60)
{
m-=60;
h++;
// if(h>24)
h%=24;
}
}
}
clock clock::operator++(int)
{
clock c(h,m,s);
s++;
if(s>=60)
{
s-=60;
m++;
if(m>=60)
{
m-=60;
h++;
// if(h>24)(可有可无)
h%=24;
}
}
return c;
}
int main()
{
clock c1(23,59,59);
clock c2(0,0,0),c;
c=c1++;
++c2;
c1.display();
c.display();
c2.display();
}
c++多态(operator++单目运算符---clock类)
最新推荐文章于 2023-10-30 18:17:09 发布