C++中的运算符重载

C++中的运算符重载

重载的概念:对已有的运算符重新定义,

赋予其另外一种功能,适应于不同的数据类型

加号运算符重载

    实现两个自定义数据类型相加的运算

注意运算符重载的总结

    对于内置的数据类型的表达式的运算符是不可能改变的额

    不要滥用运算符重载

 

#include<iostream>
#include<string>
using namespace std;

class Person{
public :
    Person(){};
    //1、通过成员函数重载+号
    // Person PersonAddPerson(Person &p){
    //     temp.m_A=this->m_A+p.m_A;
    //     temp.m_B=this->m_B+p.m_B;
    //     return temp;
    // }

    // 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;
}

// Person p5=operator+(p1,p2);
//简化为


void test01(){
    Person p1;
    p1.m_A=10;
    p1.m_B=10;
    Person p2;
    p2.m_A=10;
    p2.m_B=10;
    // Person p4=p1.operator+(p2);通过成员函数重载了+号
    //成员函数调用的本质是Person p3=p1.operator+(p2)
    Person p3=p1+p2;//没有与这些操作数匹配,Person +Person
    //全局函数调用的本质Person p3=operator+(p1,p2);
    cout<<"p3.m_A="<<p3.m_A<<endl;
    cout<<"p3.m_B="<<p3.m_B<<endl;
    //运算符重载,也可以发生函数重载
    Person p4=p1+520;
    cout<<"p4.m_A="<<p4.m_A<<endl;
    cout<<"p4.m_B="<<p4.m_B<<endl;
}


int main(){

    // test01();
    test01();
    system("pause");
    return 0;
}

 

 

2、左移运算符

#include<iostream>
#include<string>
using namespace std;

class Person{
    friend ostream & operator<<(ostream &cout,Person &p);
public :
    Person(int a, int b){
        m_A=a;
        m_B=b;
    }
private :
    // void operator<<(Person &p)
    int m_A;
    int m_B;

};

//只能够利用全局函数重载左移运算符
ostream & operator<<(ostream &cout,Person &p){
    cout<<"m_A="<<p.m_A<<" m_B"<<p.m_B;
    return cout;
}

void test01(){
    Person p(10,10);
    // p.m_A=10;
    // p.m_B=10;
    cout<<p<<endl;//后面加入endl会报错的
}

int main(){
    
    test01();
    system("pause");
    return 0;
}

 

3、递增运算符&递减运算符

/*
递增运算符 ++
除开本来内置的整型运算符



*/

#include<iostream>
#include<string>
using namespace std;

class MyInteger{
    friend ostream& operator<<(ostream & cout, MyInteger myint);
public :
    MyInteger(){
        m_Num=0;
    }

    //重载前置++运算符 返回引用是为了一直对一个数据进行递增操作
    MyInteger &operator++(){
        m_Num++;//先进行++运算
        return *this;//再将自身进行返回
    }

    //重载后置++运算符
    //void operator++(int) int 代表占位参数,可以用于区分前置和后置递增
     MyInteger  operator++(int){
         //先记录当时结果
        MyInteger temp=*this;
         //后递增
        m_Num++;
         //最后将记录结果返回
        return temp;
     }


     //重载前置--运算符
     MyInteger &operator--(){
         m_Num--;
         return *this;
     }
    //重载后置--运算符
    MyInteger operator--(int){
        MyInteger temp=*this;
        m_Num--;
        return temp;
    }
private:
    int m_Num;
};

//左移运算符
ostream & operator<<(ostream& cout, MyInteger myint){
    cout<<myint.m_Num;
    return cout;
}
 test01(){
     MyInteger myint;
     cout<<++(++myint)<<endl;
     cout<<myint<<endl;
 }

 void test02(){
     MyInteger myint;
     cout<<myint++<<endl;
     cout<<myint<<endl;
 }

 void test03(){
     MyInteger myint;
     cout<<myint--<<endl;
     cout<<myint<<endl;
 }


int main(){
    
    test03();
    system("pause");
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值