![]() |
#2
lin51616782018-05-15 22:33
|

#include<iostream>
using namespace std;
class Point
{
public:
Point(){}
Point(int vx,int vy)
{
x=vx;
y=vy;
}
void display()
{
cout<<"<"<<x<<","<<y<<">"<<endl;
}
Point & operator++(); //前置自增
Point operator++(int); //后置自增
friend Point & operator--(Point &p); //前置自减
friend Point operator--(Point &p,int); //后置自减
private:
int x,y;
};
Point &Point::operator++() //前置自增
{
if(x<640)
x++;
if(y<480)
y++;
return *this;
}
Point Point::operator++(int) //后置自增
{
Point temp(*this);
if(x<640)
x++;
if(y<480)
y++;
return temp;
}
Point &operator--(Point &p) //前置自减
{
if(p.x>0)
p.x--;
if(p.y>0)
p.y--;
return p;
}
Point operator--(Point &p,int) //后置自减
{
Point temp(p);
if(p.x>0)
p.x--;
if(p.y>0)
p.y--;
return temp;
}
int main()
{
Point p1(200,200),p2(300,300),p3(400,400),p4(50,50);
cout<<"p1="; //测试前置自增
p1.display();
++p1;
cout<<"++p1=";
p1.display();
cout<<"p2="; //测试前置自减
p2.display();
++p2;
cout<<"++p2=";
p2.display();
cout<<"p3="; //测试后置自增
p3.display();
p3++;
cout<<"p3++=";
p3.display();
cout<<"p4="; //测试后置自减
p4.display();
p4--;
cout<<"p4--";
p4.display();
return 0;
}
using namespace std;
class Point
{
public:
Point(){}
Point(int vx,int vy)
{
x=vx;
y=vy;
}
void display()
{
cout<<"<"<<x<<","<<y<<">"<<endl;
}
Point & operator++(); //前置自增
Point operator++(int); //后置自增
friend Point & operator--(Point &p); //前置自减
friend Point operator--(Point &p,int); //后置自减
private:
int x,y;
};
Point &Point::operator++() //前置自增
{
if(x<640)
x++;
if(y<480)
y++;
return *this;
}
Point Point::operator++(int) //后置自增
{
Point temp(*this);
if(x<640)
x++;
if(y<480)
y++;
return temp;
}
Point &operator--(Point &p) //前置自减
{
if(p.x>0)
p.x--;
if(p.y>0)
p.y--;
return p;
}
Point operator--(Point &p,int) //后置自减
{
Point temp(p);
if(p.x>0)
p.x--;
if(p.y>0)
p.y--;
return temp;
}
int main()
{
Point p1(200,200),p2(300,300),p3(400,400),p4(50,50);
cout<<"p1="; //测试前置自增
p1.display();
++p1;
cout<<"++p1=";
p1.display();
cout<<"p2="; //测试前置自减
p2.display();
++p2;
cout<<"++p2=";
p2.display();
cout<<"p3="; //测试后置自增
p3.display();
p3++;
cout<<"p3++=";
p3.display();
cout<<"p4="; //测试后置自减
p4.display();
p4--;
cout<<"p4--";
p4.display();
return 0;
}
代码里运算符重载里的return *this指针不是特别理解,求大神解!!!!还有那个Point temp(*this)是个什么操作。。。