第三十课:操作符重载的概念----------狄泰软件学院

解决复数相加的问题: 一、定义一个全局的函数,参数为对象的引用,通过函数调用的方法来解决

#include<iostream>

using namespace std;

class Complex
{
private:
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }

    int getA()
    {
        return a;
    }

    int getB()
    {
        return b;
    }   

    friend Complex Add(const Complex& p1, const Complex& p2);
};

Complex Add(const Complex& p1, const Complex& p2)
{
    Complex ret;
    ret.a = p1.a + p2.a;
    ret.b = p1.b + p2.b;
    return ret;
}

int main()
{
    Complex p1(1, 2);
    Complex p2(3, 4);
    Complex ret = Add(p1, p2);
    cout<<ret.getA()<<endl;
    cout<<ret.getB()<<endl;
    return 0;
}

二、操作符重载的方法

本质:用特殊形式的函数扩展操作符的功能(通过operator关键字来定义)
将上面的函数的add都换成operator +

#include<iostream>

using namespace std;

class Complex
{
private:
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }

    int getA()
    {
        return a;
    }

    int getB()
    {
        return b;
    }   

    friend Complex operator + (const Complex& p1, const Complex& p2);
};

Complex operator + (const Complex& p1, const Complex& p2)
{
    Complex ret;
    ret.a = p1.a + p2.a;
    ret.b = p1.b + p2.b;
    return ret;
}

int main()
{
    Complex p1(1, 2);
    Complex p2(3, 4);
    Complex ret = p1 + p2;//===>ret.operator +(p1, p2);编译器遇到这种加法就会去寻找看是否有扩展了功能的函数
    cout<<ret.getA()<<endl;
    cout<<ret.getB()<<endl;
    return 0;
}

三、可以将操作符重载函数定义为类的成员函数

1.比全局操作符重载函数少一个参数(左操作数,由this来充当)
2.不依赖友元可以完成操作符重载
3.编译器优先在成员函数中寻找操作符重载函数(同时存在成员函数版本和全局版本时)

#include<iostream>

using namespace std;

class Complex
{
private:
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }

    int getA()
    {
        return a;
    }

    int getB()
    {
        return b;
    }   

    Complex operator + (const Complex& p)
    {
        Complex ret;
        cout<<"Complex operator + (const Complex& p)"<<endl;
        ret.a = this->a + p.a;
        ret.b = this->b + p.b;
        return ret;
    }


    friend Complex operator + (const Complex& p1, const Complex& p2);
};

Complex operator + (const Complex& p1, const Complex& p2)
{
    Complex ret;
    cout<<"Complex operator + (const Complex& p1, const Complex& p2)"<<endl;
    ret.a = p1.a + p2.a;
    ret.b = p1.b + p2.b;
    return ret;
}

int main()
{
    Complex p1(1, 2);
    Complex p2(3, 4);
    Complex ret = p1 + p2;//===>ret = p1.operator + (p2);等价于调用p1的成员函数,且以p2为参数
    cout<<ret.getA()<<endl;
    cout<<ret.getB()<<endl;
    return 0;
}
打印结果:
Complex operator + (const Complex& p)//说明优先调用成员函数版本的重载函数
4
6

小结:

1.操作符重载是c++的强大特性之一
2.操作符重载的本质是通过函数扩展操作符的功能
3.operator关键字是实现操作符重载的关键
4.操作符重载遵循函数重载的原则
5.全局函数和成员函数都可以实现操作符重载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值