C++——运算符重载operator

C++——运算符重载operator

C++ prime plus第11章,运算符重载是C++的一种多态。运算符重载格式如下:

operator运算符(argument-list)

1、做普通函数重载

示例:计算两个对象成员的和

#include <iostream>
using namespace std;

class A{
  private:
    int a;
  public:
    A(){
      this->a=55;
    }
    int get_a(){
      return this->a;
    }
    ~A(){}
};
class B{
  private:
    int b;
  public:
    B(){
      this->b=88;
    }
    int get_b(){
      return this->b;
    }
    ~B(){}
};
//计算两个对象成员的和
void operator+(A _A,B _B){
  cout<<"A.a+B.b = "<<_A.get_a()+_B.get_b()<<endl;
}
int main (int argc,char *argv[]){
  A _a;
  B _b;

  _a+_b;//加号前为函数重载第一个参数,加号后为第二个参数
  return 0;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HYK0ePRI-1620203790036)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505153421516.png)]

运算符前为函数重载第一个参数,运算符后为第二个参数,不能搞反。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0FPbMGYU-1620203790039)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505152309822.png)]

2、做类成员函数重载

例如:重量计算

#include <iostream>
using namespace std;

class weight{
  private:
    int gram;
    double kilogram;
    int total_gram;
  public:
    weight(){
      this->gram=10;
      this->kilogram=1;
      this->total_gram=0;
    }
    weight(int x,int y){
      this->gram=x;
      this->kilogram=y;
      this->total_gram=0;
    }
    ~weight(){}
    int get_gram(){
      return this->gram;
    }
    int get_kilogram(){
      return this->gram;
    }
    int get_total_gram(){
      return this->total_gram;
    }
    void operator+(weight A){
      this->total_gram=this->gram+A.gram+1000*(this->kilogram+A.kilogram);
    } 
};
int main (int argc,char *argv[]){
  weight A(500,2);
  weight B(200,3);

  A+B;//可以看到运算符前为被调用重载方法的对象,运算符后面是函数参数

  cout<<A.get_total_gram()<<endl;
  cout<<B.get_total_gram()<<endl;
  return 0;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZUXUxAjJ-1620203790042)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505153554887.png)]

可以看到运算符前为被调用重载方法的对象,运算符后面是函数参数

其实成员函数重载他的本质是:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AcJBMU3g-1620203790046)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505155357504.png)]

3、运算符重载限制

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RmbSKEPK-1620203790048)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505161241760.png)]

## [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xithMJh0-1620203790059)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505161307484.png)]

4、++运算符与–运算符重载

成员函数

int operator++(){
    this->gram++;
    cout<<1<<endl;
    return this->gram;
}
int operator++(int){
    weight C=*this;
    this->gram++;
    cout<<2<<endl;
    return C.gram;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2O2ppOt5-1620203790061)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505162345563.png)]

友元函数

int operator--(weight &A){
  A.gram--;
  cout<<1<<endl;
  return A.gram;
}
int operator--(weight &A,int){
  weight D=A;
  A.gram--;
  cout<<2<<endl;
  return D.gram;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wl1EqdTX-1620203790063)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505163131821.png)]

int main (int argc,char *argv[]){
  weight A(500,2);
  weight B(200,3);

  A+B;

  cout<<A.get_total_gram()<<endl;
  cout<<B.get_total_gram()<<endl;

  ++A;
  cout<<A.get_gram()<<endl;
  cout<<A++<<endl;
  cout<<A.get_gram()<<endl;

  --A;
  cout<<A.get_gram()<<endl;
  cout<<A--<<endl;
  cout<<A.get_gram()<<endl;
  return 0;
}

下图后置方式必须用int作为伪参数,不能用其他类型
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ttVCdpv3-1620203790065)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505163149282.png)]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值