C++重载复合赋值运算符、算术运算符和关系运算符

原文链接:http://blog.csdn.net/lyh__521/article/details/49622601

复合赋值运算符

  形如‘*=’、‘+=’、‘/=’等这样的运算符称为复合赋值运算符,它们其实是算术运算和赋值运算的结合,即先执行算术运算,得到的值再赋值给左侧对象。


重载复合赋值运算符:

  1. 赋值运算符必须定义为类的成员,复合赋值运算符通常也应该定义为成员函数(但并不是必须这样)。
  2. 复合赋值运算符函数返回左侧对象的引用(为了与内置的复合赋值运算符保持一致)。

下面是一个例子:

//重载了 += 运算符
#include<iostream>
using namespace std;

class Goods{
    public: 
        //构造函数     
        Goods(int x=0,double y=0.0){
            num = x;
            money = y;
        }
        //重载复合赋值运算符
        Goods   &operator+=(const Goods &gd2){
            num   += gd2.num;
            money += gd2.money;
            return *this;
        }
        //打印类的内容
        void print(){
            cout<<num<<" "<<money<<endl;
        }

    private:
        int      num;
        double   money;
};

int main(){
    Goods       gd1(5,20);
    Goods       gd2(10,40);

    //将类gd2加到类gd1上
    gd1 += gd2;
    gd1.print();
    return 0;
}

这里写图片描述

重载算术运算符

   形如 +、-、*、/、等这类的是算术运算符。


  1. 通常定义为非成员函数,这样可以允许对左侧或右侧的运算对象进行转换。例如: “hello”+string(“world”) 和 string(“hello”)+”world” 。
  2. 形参一般是常量的引用,因为不需要改变运算对象的状态。
  3. 一般在函数内定义一个局部变量保存运算结果,函数返回该局部变量的副本(见下面例子)。
  4. 一般声明为友元函数。
  5. 一般重载了算术运算符,同时也会重载复合赋值运算符(此时,应注意代码复用原则,使用复合赋值运算符定义算术运算符,见下面例子)。

将上面的类加入算术运算符:

//重载了 += 和 +
#include<iostream>
using namespace std;

class Goods{
    public: 
        //构造函数     
        Goods(int x=0,double y=0.0){
            num = x;
            money = y;
        }
        //重载复合赋值运算符+=
        Goods   &operator+=(const Goods &gd2){
            num   += gd2.num;
            money += gd2.money;
            return *this;
        }
        //重载算术运算符+
        friend Goods  operator+(const Goods &gd1,const Goods &gd2);
        //打印类的内容
        void print(){
            cout<<num<<" "<<money<<endl;
        }

    private:
        int      num;
        double   money;
};

Goods operator+(const Goods &gd1,const Goods &gd2){
    Goods     tmp = gd1;   //定义一个局部变量接收运算结果
    tmp += gd2;            //注意代码复用,使用已定义的复合赋值运算符
    return tmp; //返回局部变量的副本
}

int main(){
    Goods       gd1(5,20);
    Goods       gd2(10,40);

    //将类gd1 与 gd2 相加
    (gd1 + gd2).print();
    return 0;
}

这里写图片描述

重载关系运算符

定义相等运算符

   类通过定义相等运算符来检验两个对象是否相等,一般会比较对象的每一个数据成员,当所有对应的成员都相等是认为两个对象相等。


设计准则:

  1. 定义的相等运算符应该具有传递性,即若a==b 和 b==c都为真,则a==c为真。
  2. 一般定义了==运算符,同时也应该定义 operator != 。此时,应注意代码复用,不需要重复定义(见下面例子)。
  3. 一般定义为非成员函数。

//定义==和!=运算符 
#include<iostream>
#include<string>
using namespace std;

class Foo{
    public:
        Foo(int x=0,string s=""){
            num = x;
            name = s;
        }
friend  bool operator==(const Foo &f1,const Foo &f2);
friend  bool operator!=(const Foo &f1,const Foo &f2);
    private:
        int       num;
        string    name;
};

bool operator==(const Foo &f1,const Foo &f2){
    return f1.num == f2.num && f1.name == f2.name;
}

bool operator!=(const Foo &f1,const Foo &f2){
    //代码复用,使用定义好的==
    return !(f1==f2);
}

int main()
{
    Foo   f1(1,"liu");
    Foo   f2(1,"zhang");

    cout<<(f1 == f1)<<" "<<(f1 != f2)<<endl;
    return 0;
}

这里写图片描述

除此之外,我们还可以为类定义 > 、< 关系运算符。

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值