分享

对Point类重载++和――运算符

 BUPT-BYR 2010-12-08

 

9、对Point类重载++和――运算符

    编写C++程序完成以下功能:

(1)    Point类的属性包括点的坐标(xy);

(2)    实现 Point类重载++和――运算符:

l        ++p--pp++p--

l        ++和――分别表示xy增加或减少1

 

#include<iostream>

using namespace std;

 

class Point

{

private:

       float x;

       float y;

public:

       Point(float xx=0,float yy=0) {x=xx; y=yy;}

       void SetPoint(float xx=0,float yy=0) {x=xx; y=yy;}

       ~Point() {}

       void output();

    Point operator ++();

       Point operator --();

       Point operator ++(int);

       Point operator --(int);

 

};

 

Point Point::operator ++()

{

       Point b;

       b.x=x+1;

       b.y=y+1;

       return b;

}

 

Point Point::operator --()

{

       Point b;

       b.x=x-1;

       b.y=y-1;

       return b;

}

 

Point Point::operator ++(int)

{

       Point b;

       b.x=x+1;

       b.y=y+1;

       return b;

}

 

Point Point::operator --(int)

{

       Point b;

       b.x=x-1;

       b.y=y-1;

       return b;

}

 

 

 

void Point::output()

{

       cout<<"("<<x<<","<<y<<")";

}

 

int main()

{

       Point a,b;

       float m,n;

       cout<<"请输入一个点坐标:"<<endl;

       cin>>m>>n;

       a.SetPoint(m,n);

 

       cout<<"输入点坐标为:"<<endl;

       a.output();

       cout<<endl<<endl;

 

       b=(a++);

       a.output();

       cout<<"++";

       cout<<"=";

       b.output();

       cout<<endl<<endl;

 

       b=(a--);

       a.output();

       cout<<"--";

       cout<<"=";

       b.output();

       cout<<endl<<endl;

 

       b=(++a);

       cout<<"++";

       a.output();

       cout<<"=";

       b.output();

       cout<<endl<<endl;

 

       b=(--a);

       cout<<"--";

       a.output();

       cout<<"=";

       b.output();

       cout<<endl<<endl;

 

       system("pause");

       return 0;

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多