自增自减

来源:互联网 发布:安卓无忧软件 编辑:程序博客网 时间:2024/04/28 03:26
#include <iostream.h>
class Point   //复数类
{
private:
int x,y;
public:
Point(int i=0, int j=0)//构造函数
{
    x=i;
    y=j;
}
void Show() //显示点值,格式:(x,y)
{
    cout<<"("<<x<<","<<y<<")"<<endl;
}
    Point operator ++(); //声明前置自增运算符“++”重载函数
    Point operator ++(int);  //声明后置自增运算符“++”重载函数(傻瓜式定义)
};
Point Point::operator ++()  //定义前置自增运算符“++”重载函数
{
    ++x;
    ++y;
    return *this;
}
Point Point::operator ++(int) //定义后置自增运算符“++”重载函数
{
    Point ptold(x,y);
    ptold.x=x++;
    ptold.y=y++;
    
    return ptold;
}
int main()
{
Point p1(20,40),p2;
p1.Show();
p2=p1++;
p1.Show();
p2.Show();
p2=++p1;
p1.Show();
p2.Show();


return 0;
}