operator和&operator

// operator和&operator !

#include <iostream> 
using namespace std; 
//声明
class Point;
Point operator-(Point &a,Point &b);
Point &operator+(Point &a,Point &b);
//定义点类
class Point 
{ 
    public: 
    int x,y; 
    char name;
    Point(){}
    Point(char n1,int xx,int yy){name=n1;x=xx;y=yy;}
    void print(){ cout<<name<<"=("<<x<<","<<y<<")\n"; } 
    friend Point operator-(Point &a,Point &b); 
    friend Point &operator+(Point &a,Point &b); 
};
//重载-号操作(返回值)
Point operator-( Point &a,Point &b) 
{ 
    Point s('s',a.x-b.x,a.y-b.y); 
    return s; 
}
//重载+号操作(返引用)
Point &operator+( Point &a,Point &b) 
{ 
    a.x+=b.x;
    a.y+=b.y; 
    return a; 
}
//主函数
int main() 
{ 
    Point a('a',3,20),b('b',1,5),c('c',10,15); 
    a.print();
    b.print();
    c=a-b;
    c.print();
    a.print();
    b.print();
    c=a+b;
    c.print() ;
    cout<<"没有& 返回的是值,有&返回的是引用"<<endl;
    return 0;
}
//没有& 返回的是值,有&返回的是引用
//运行结果如下,自己研究一下,就会明白!

/*
a=(3,20)
b=(1,5)
s=(2,15)
a=(3,20)
b=(1,5)
a=(4,25)
没有& 返回的是值,有&返回的是引用

*/

结果

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值