您的位置:首页 > 编程语言 > C语言/C++

C++对象的++,--运算符重载 374-39

2006-12-06 21:26 330 查看
#include <iostream.h>

class CPoint
{
public:
CPoint(int i=0,int j=0)
{
x=i;
y=j;
}

CPoint operator++();
CPoint operator++(int);
CPoint operator--();
CPoint operator--(int);

void print()
{
cout<<"x="<<x<<" y="<<y<<endl;
}

private:
int x,y;
};

CPoint CPoint::operator++()
{
x++;
y++;
return *this;
}

CPoint CPoint::operator ++(int)
{
CPoint a;
a.x =x;
a.y=y;
x++;
y++;
return a;

}

CPoint CPoint::operator--()
{
x--;
y--;
return *this;
}

CPoint CPoint::operator --(int)
{
CPoint a;
a.x=x;
a.y=y;
x--;
y--;
return a;
}

void main()
{
CPoint a;
CPoint b;

cout<<"CPoint a; /na.print() ";
a.print();
cout<<endl;

b=a++;
cout<<"CPoint b;"<<endl;
cout<<"b=a++; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
cout<<endl;

b=++a;
cout<<"b=++a; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
cout<<endl;

b=a--;
cout<<"b=a--; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
cout<<endl;

b=--a;
cout<<"b=--a; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: