打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
对Point类重载++和――运算符

 

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;

}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C++源码:重载的应用实例
c++中运算符的一些不同的使用 欧洲的有些需要这样
关于24点游戏的编程思路与基本算法
3.6 算数运算符
C/C++编程知识:运算符(七)丨三元/三目运算符知识详解
VS2013创建dll动态链接库文件心得
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服