//重载++(将Point类似于8进制的两位)
#include <iostream>
using namespace std;
class Point{
public:
Point(int x=0,int y=0):x(x),y(y){}
void display() const;
Point &operator++(); //++前置
Point operator++(int); //++后置
public:
int x,y;
};
Point &Point::operator ++(){
y++; //前置重载时会有后置重载的操作,这个是系统内部的++还是我这个类里面的后置运算符++??
if(y>=8){
y=0;
x++;
if(x>=8)
x=0;
}
return *this;
}
Point Point::operator ++(int){
//cout<<"Hello"<<endl;
Point point=*this;
++ *this;
return point;
}
void Point::display()const{
cout<<x<<y<<endl;
}
int main()
{
Point p(1,7),p1(1,7),p2,p3;
p2=++p1;
p2.display();
p3=p++;
p3.display();
return 0;
} |