运算符重载

一、运算符重载。
什么是运算符重载?
运算符重载:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。

二、加号运算符重载。
作用: 实现两个自定义数据类型相加的运算。

#include <iostream>
using namespace std;

//加号运算符重载
class Person{
public:
    //1. 成员函数重载+号。
    /*
    Person operator+(Person &p)
    {
        Person temp;
        temp.m_A = this->m_A + p.m_A;
        temp.m_B = this->m_B + p.m_B;
        return temp;
    }
    */

public:
    int m_A;
    int m_B;
};

//2. 全局函数重载+号
Person operator+(Person &p1,Person &p2)
{
    Person temp;
    temp.m_A = p1.m_A + p2.m_A;
    temp.m_B = p1.m_B + p2.m_B;
    return temp; 
}

//函数重载版本
Person operator+(Person &p1,int num)
{
    Person temp;
    temp.m_A = p1.m_A + num;
    temp.m_B = p1.m_B + num;
    return temp;
}

int main()
{
    Person p1;
    p1.m_A = 10;
    p1.m_B = 10;

    Person p2;
    p2.m_A = 20;
    p2.m_B = 20;

    Person p3 = p1 + p2;  //+ 识别不了这种自定义数据类型

    //Person p3 = p1.operator+(p2);

    //Person p3 = operator+(p1,p2);
    Person p3 = operator+(p1,100);

    cout << "p3.m_A = " << p3.m_A << endl;
    cout << "p3.m_B = " << p3.m_B << endl;

    return 0;
}

三、左移运算符重载。
cout的数据类型是ostream,它是被定义在/usr/include/c++/iostream中。
extern ostream cout;


#include <iostream>
using namespace std;

//左移运算符重载
class Person{
    friend ostream& operator<<(ostream &cout,Person p);

public:
    Person(int a,int b)
    {
        m_A = a;
        m_B = b;
    }

    //1. 使用成员函数来做重载

    //如果这样写,将来出来的结果: p.operator<<(cout)  简化 p << cout
    //p << cout这个结果与我们平时使用的cout << p不一样
    //所以不会使用成员函数来重载<<运算符,因为无法实现cout在左侧。
    /*
    void operator<<(cout)
    {

    }
    */

private:
    int m_A;
    int m_B;
};

//2. 重载<<符号,只能使用全局函数来做重载
ostream& operator<<(ostream &cout,Person p)
{
    cout << "m_A = " << p.m_A << " m_B = " << p.m_B;
    return cout;
}

int main()
{
    Person p(10,10);
    //cout << "p.m_A:" << p.m_A << endl;
    //cout << "p.m_B:" << p.m_B << endl;

    cout << p << "helloworld" << endl;  //错误,需要重载左移运算符<<

    return 0;
}

四、递增运算符重载。
#include <iostream>
using namespace std;

//重载递增运算符

//整型类
class MyInteger{
    friend ostream& operator<<(ostream &cout,MyInteger myint);

public:
    MyInteger()
    {
        m_Num = 0;
    }

    //重置前置++运算符
    MyInteger& operator++() //默认重置前置的++
    {
        //先进行++运算。
        m_Num++;

        //再将自身返回。
        return *this;
    }

    //重置后置++运算符
    //因为C++本身就不支持连续的后置递增,所以不需要考虑返回&。
    MyInteger operator++(int)   //int就是占位参数  -> 加了int,就是后置++的重载
    {
        //先记录当前结果
        MyInteger temp = *this;

        //递增
        m_Num++;

        //最后将记录的结果返回就可以。
        return temp;
    }

private:
    int m_Num;
};

//重载<<运算符
ostream& operator<<(ostream &cout,MyInteger myint)
{
    cout << myint.m_Num;
    return cout;
}

int main()
{
    //前置递增
    //MyInteger myint;  //0
    //cout << ++(++myint) << endl;//2
    //cout << myint << endl; //2

    //后置递增
    MyInteger myint;//0
    cout << myint++ << endl; //0
    cout << myint << endl; //1

    return 0;
}


五、赋值运算符重载。
C++编译器至少给一个类添加4个函数。
1、默认构造函数。(无参,函数体为空)
2、默认析构函数。(无参,函数体为空)
3、默认拷贝构造函数,对属性进行值拷贝。(浅拷贝)
4、赋值运算符operator=,对属性进行值拷贝。(浅拷贝)

#include <iostream>
using namespace std;

//赋值运算符重载
class Person{
public:
    Person(int age)
    {
        m_Age = new int(age);
    }

    ~Person()
    {
        if(m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
    }

    //为了解决浅拷贝,我们自己重载一下=
    Person& operator=(Person &p)
    {
        //编译器默认提供的就是浅拷贝
        //m_Age = p.m_Age;  //这行话就是编译器默认做的

        //应该先判断是否有属性在堆区,如果有,先释放干净,再深拷贝。
        if(m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }

        m_Age = new int(*p.m_Age);

        return *this;
    }

    int *m_Age;
};

int main()
{
    Person p1(18);
    Person p2(20);
    Person p3(30);

    p3 = p2 = p1; //赋值操作

    cout << "p1的年龄是:" << *p1.m_Age << endl;
    cout << "p2的年龄是:" << *p2.m_Age << endl;
    cout << "p3的年龄是:" << *p3.m_Age << endl;
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值