Day9-C++基础之运算符重载

运算符重载

#include<iostream>
#include<string>
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;
    }
​
    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;
}
​
//左移运算符重载
class Man{
public:
    int m_A;
    int m_B;
};
//只能利用全局函数重载左移运算符
ostream &operator<<(ostream &cout,Man &m){ //简化成:cout<<p
    cout<<m.m_A<<","<<m.m_B;
    return cout;
}
​
//递增运算符重载
//自定义整型
class MyInteger{
    friend ostream &operator<<(ostream& cout,MyInteger myint);
public:
    MyInteger(){
        m_Num=0;
    }
    //重载前置++运算符, 返回引用是为了一直对一个数据进行操作
    MyInteger& operator++(){
        m_Num++;
        return *this;
    }
    //重载后置++运算符
    MyInteger operator++(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;
}
​
//赋值运算符重载
class Student{
public:
    Student(int age){
        m_Age=new int(age);
    }
    //析构函数手动释放堆区数据
    ~Student(){
        if (m_Age!=NULL){
            delete m_Age;
            m_Age=NULL;
        }//进行赋值操作后,存在堆区内存重复释放问题
    }
    //重载赋值运算符
    Student& operator=(Student &s){
        //应该先判断是否有属性在堆区,如果有先释放再深拷贝
        if(m_Age!=NULL){
            delete m_Age;
            m_Age=NULL;
        }
        //深拷贝
        m_Age=new int(*s.m_Age);
        return *this;
    }
    
    int *m_Age;
};
​
//重载关系运算符
class Teacher{
public:
    Teacher(string name,int age){
        m_Name=name;
        m_Age=age;
    }
    bool operator==(Teacher &t){
        if(this->m_Name==t.m_Name&&this->m_Age==t.m_Age){
            return true;
        }
        return false;
    }
    string m_Name;
    int m_Age;
};
​
//函数调用运算符重载
class MyPrint{
public:
    //这里第一个小括号就是函数调用运算符
    void operator()(string test){//使用非常类似函数,又叫仿函数
        cout<<test<<endl;
    }
};
class MyAdd{
public:
    int operator()(int n1,int n2){//由此可见,仿函数没有固定写法,非常灵活
        return n1+n2;
    }
};
​
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 p4=p1+100;
    cout<<p3.m_A<<","<<p3.m_B<<endl;
​
    //左移运算符测试
    Man m;
    cout<<m<<endl;//链式编程思想,所以将原来返回值void变为标准输出流
    
    //递增运算符测试
    MyInteger myint;
    cout<<++myint<<endl;
    cout<<myint++<<endl;
    
    //赋值运算符测试
    Student s1(18);
    Student s2(20);
    Student s3(30);
    p3=p2=p1;
    
    //关系运算符测试
    Teacher t1("Tom", 18);
    Teacher t2("Tom", 18);
    if(t1==t2){
        cout<<"两者相等"<<endl;
    }
    
    //函数调用运算符重载测试
    MyPrint myPrint;
    myPrint("Hello,World!");
    MyAdd myAdd;
    int res=myAdd(100,100);
    cout<<res<<endl;
    //匿名函数对象
    cout<<MyAdd()(100,100)<<endl;
​
    return 0;
}
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值