【08】c++运算符重载

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

例如:C++运算符重载


提示:以下是本篇文章正文内容,下面案例可供参考

一、加号运算符

完成自定义类型加号运算符
使用成员函数和全局函数都可以完成

#include<iostream>
using  namespace std;
#include<string>
/*函数运算符重载*/

class Operate
{
private:
    /* data */
public:
    Operate(/* args */);
    ~Operate();
    Operate(int a, int b) {
        this->m_numa = a;
        this->m_numb = b;
    }
    //成员函数方式
    // Operate operator+(Operate &temp1) {
    //     Operate temp;
    //     temp.m_numa = this->m_numa+temp1.m_numa;
    //     temp.m_numb = this->m_numb+temp1.m_numb;
    //     return temp;
    // }
    int m_numa;
    int m_numb;
};

Operate::Operate(/* args */)
{
}

Operate::~Operate()
{
}
//全员函数方式
Operate operator+(Operate &temp1,Operate &temp2) {
    Operate temp;
    temp.m_numa = temp1.m_numa + temp2.m_numa;
    temp.m_numb = temp1.m_numb + temp2.m_numb;
    return temp;
}
//运算符 函数重载
Operate operator+(Operate &temp1, int b) {
    temp1.m_numb = temp1.m_numb + b;
    return temp1;
}
int main()
{
   
    Operate p1(10, 10);
    cout << "numa = "<< p1.m_numa << endl;
    cout << "numb = "<< p1.m_numb << endl;
    Operate p2(10, 10);
    cout << "numa = "<< p2.m_numa << endl;
    cout << "numb = "<< p2.m_numb << endl;
    Operate p3 = p1 + p2;
    cout << "numa = "<< p3.m_numa << endl;
    cout << "numb = "<< p3.m_numb << endl;

}

二、左移运算符

目的:为了可以直接实现cout输出一个对象。
只能使用全局函数实现

class Operate
{
    friend ostream & operator<<(ostream &cout,Operate &temp);
private:
    /* data */
public:
    Operate(/* args */);
    ~Operate();
    Operate(int a, int b) {
        this->m_numa = a;
        this->m_numb = b;
    }
    //成员函数方式
    // Operate operator+(Operate &temp1) {
    //     Operate temp;
    //     temp.m_numa = this->m_numa+temp1.m_numa;
    //     temp.m_numb = this->m_numb+temp1.m_numb;
    //     return temp;
    // }
    int m_numa;
    int m_numb;
};

ostream & operator<<(ostream &cout,Operate &temp) {
    cout <<"numa = "<<temp.m_numa << " numb = " << temp.m_numb ;
    return cout;
}

三.递增或者递减运算符

代码如下(示例):


class Myprint
{
private:
    /* data */
    int M_a;
public:
    Myprint(/* args */);
    Myprint(int a){
        this->M_a = a;
    }
    ~Myprint();
    //前置++返回引用,为了操作是同一个对象
    Myprint& operator++() {
        this->M_a++;
        return *this;
    }
    //后置++返回值,int 是占位符,函数重载的条件
    Myprint operator++(int) {
        Myprint temp = *this;
        this->M_a++;
        return temp;
    }
     //前置--返回引用,为了操作是同一个对象
    Myprint& operator--() {
        this->M_a--;
        return *this;
    }
    //后置--返回值,int 是占位符,函数重载的条件
    Myprint operator--(int) {
        Myprint temp = *this;
        this->M_a--;
        return temp;
    }
    friend ostream & operator<<(ostream &cout, Myprint temp);
};

Myprint::Myprint(/* args */)
{
}

Myprint::~Myprint()
{
}

ostream & operator<<(ostream &cout, Myprint temp) {
    cout << temp.M_a;
    return cout;
}
int main()
{
   
    Myprint a(10);

    cout << a << endl;
    //++ test
    cout << ++a << endl;
    cout << a << endl;
    cout << a++ << endl;
    cout << a << endl;
     //-- test
    cout << --a << endl;
    cout << a << endl;
    cout << a-- << endl;
    cout << a << endl;
}

四.赋值运算符重载

c++编译器至少给一个类添加4个函数
1.默认构造函数
2.默认析构函数
3.默认拷贝构造函数
4.默认运算符operator=,对属性进行值拷贝
问题:如果在堆区申请内存,使用自带的赋值函数(浅拷贝),会导致同一块内存被重复释放,使用深拷贝方案解决
在这里插入图片描述

class NumTest
{
private:
    /* data */
public:
    NumTest(/* args */int age);
    ~NumTest();
    NumTest& operator=(NumTest&temp) {
        if (!M_data) {
            delete M_data;
            M_data = NULL;
        }
        //编译器提供的代码是浅拷贝
        // M_data = temp.M_data;

        //提供深拷贝构造函数
        M_data = new int(*temp.M_data);
        
        // 返回自身
        return *this;
    }

    int *M_data;
};

NumTest::NumTest(/* args */int data)
{
    this->M_data = new int(data);
    
}

NumTest::~NumTest()
{
    if (!this->M_data) {
        delete this->M_data;
        this->M_data = NULL;
    }
    
}

int main()
{
   
    NumTest p1(10);
    NumTest p2(20);
    NumTest p3(30);
    p2 = p1;
    p3 = p2 = p1;

    cout << "p1.data = " << *p1.M_data << endl;
    cout << "p2.data = " << *p2.M_data << endl;
    cout << "p3.data = " << *p3.M_data << endl;


}

五、关系运算符


class NumTest
{
private:
    /* data */
public:
    NumTest(/* args */int age);
    ~NumTest();
    NumTest& operator=(NumTest&temp) {
        if (!M_data) {
            delete M_data;
            M_data = NULL;
        }
        //编译器提供的代码是浅拷贝
        // M_data = temp.M_data;

        //提供深拷贝构造函数
        M_data = new int(*temp.M_data);
        
        // 返回自身
        return *this;
    }
    bool operator==(NumTest&temp) {
        if (*this->M_data == *temp.M_data)
        {
            return true;
        }
        return false;
    }
    bool operator!=(NumTest&temp) {
        if (*this->M_data != *temp.M_data)
        {
            return true;
        }
        return false;
    }

    int *M_data;
    int M_num;
};

NumTest::NumTest(/* args */int data)
{
    this->M_data = new int(data);
    
}

NumTest::~NumTest()
{
    if (!this->M_data) {
        delete this->M_data;
        this->M_data = NULL;
    }
    
}

int main()
{
    NumTest m1(10);

    NumTest m2(10);
    if(m1 == m2) {
        cout << "m1 == m2 = " << *m1.M_data << endl;
    } else {
        cout << "m1 != m2  " << "m1 = " << *m1.M_data << "m2 = "  << *m2.M_data  << endl;
    }
}

六、仿函数

重载的()被称作仿函数

class NumTest
{
private:
    /* data */
public:
    NumTest(/* args */int age);
    ~NumTest();
    int operator()(int num1, int num2) {
        return num1 + num2 ;
    }
    int *M_data;
    int M_num;
};

NumTest::NumTest(/* args */int data)
{
    this->M_data = new int(data);
    
}

NumTest::~NumTest()
{
    if (!this->M_data) {
        delete this->M_data;
        this->M_data = NULL;
    }
    
}

int main()
{
    NumTest m1(10);

    //仿函数
    cout << m1(20, 20) << endl;

}

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值