C++ 运算符重载

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

1.加号运算符重载

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

#include<iostream>
using namespace std;
class Person{
    public:
        int age;
        int count;
        Person(){
            this->age = 0;
            this->count = 0;
        }
        Person(int age, int count){
            this->age = age;
            this->count = count;
        }
//      成员函数实现 + 号运算符重载
        Person operator+(const Person& p){
            Person temp;
            temp.age = this->age + p.age;
            temp.count=this->count + p.count;
            return temp;
        }
};
//全局函数实现 + 号运算符重载
Person operator+(const Person& p1, const Person& p2){
    Person temp;
    temp.age = p1.age + p2.age;
    temp.count = p1.count + p2.count;
    return temp;
}
//运算符重载也可以函数重载
Person operator+(const Person& p1, int n){
    Person temp;
    temp.age = p1.age + n;
    temp.count = p1.count + n;
    return temp;
}
int main(){
    Person p1(10, 20);
    Person p2(20,22);
    Person p3 = p1 + p2;
    cout<< "p3.age = " << p3.age << endl;
    cout<< "p3.count = " << p3.count << endl;
    return 0;
}

2.左移运算符

作用:可以输出自定义数据类型

#include<iostream>
using namespace std;
class Person{
    public:
        int age;
        int count;
        Person(){
            this->age = 0;
            this->count = 0;
        }
        Person(int age, int count){
            this->age = age;
            this->count = count;
        }
};
//全局函数实现左移重载,ostream只能有一个
ostream& operator<<(ostream &out, Person &p){
    out << "Age: " << p.age << " Count:1   " << p.count;
    return out;
}
int main(){
    Person p1(10, 20);

    cout<<p1<<"hello"<<endl;
    return 0;
}

总结:重载左移运算符配合友元可以实现输出自定义数据类型

3.递增运算符

总结: 前置递增返回引用,后置递增返回值

作用: 通过重载递增运算符,实现自己的整型数据

#include<iostream>
using namespace std;
class myNum{
public :
    myNum(){
        num = 0;
    }
//    前置递增  返回引用
    myNum& operator++(){
        num++;
        return *this;
    }
//   后置递增  返回值
    myNum operator++(int){
        myNum temp = *this;
        num++;
        return temp;
    }
    int num;
};
ostream& operator<<(ostream &out, myNum n){
    out<<n.num;
    return out;
}
int main(){
    myNum n1;
    cout<< n1++<<endl;
    cout<<++n1<<endl;
    return 0;
}

4.赋值运算符

c++编译器至少给一个类添加4个函数
1 . 默认构造函数(无参,函数体为空)
2 . 默认析构函数(无参,函数体为空)
3 . 默认拷贝构造函数,对属性进行值拷贝
4 . 赋值运算符 operator=, 对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

#include<iostream>
using namespace std;
class Person
{
public:
    Person(int age)
    {
//将年龄数据开辟到堆区
        m_Age = new int(age);
    }
//重载赋值运算符
    Person& operator=(Person &p)
    {
        if (m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
//编译器提供的代码是浅拷贝
//m_Age = p.m_Age;
//提供深拷贝 解决浅拷贝的问题
        m_Age = new int(*p.m_Age);
//返回自身
        return *this;
    }
    ~Person()
    {
        if (m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
    }
//年龄的指针
    int *m_Age;
};
void test01()
{
    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;
    cout<<p1.m_Age<<endl;
}
int main() {
    test01();
int a = 10;
    int b = 20;
int c = 30;
c = b = a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;

    return 0;
}

5.关系运算符

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

#include<iostream>
using namespace std;
class Person
{
public:
    int age;
    string name;
    Person(){

    }
    Person(int age,string name){
        this->age = age;
        this->name = name;
    }
    bool operator==(const Person & p){
        if(this->age== p.age&&this->name==p.name){
            return true;
        }else{
            return false;
        }
    }
    bool operator!=(const Person & p){
        if(this->age== p.age&&this->name==p.name){
            return false;
        }else{
            return true;
        }
    }
};
void test01()
{
    Person p1(10, "Tom");
    Person p2(10, "Tom");
    if (p1 == p2)
    {
        cout << "p1 == p2" << endl;
    }
    else
    {
        cout << "p1 != p2" << endl;
    }
    if (p1 != p2)
    {
        cout << "p1 != p2" << endl;
    }
    else
    {
        cout << "p1 == p2" << endl;
    }
}
int main() {
    test01();
    return 0;
}

6.函数调用运算符重载

● 函数调用运算符 () 也可以重载
● 由于重载后使用的方式非常像函数的调用,因此称为仿函数
● 仿函数没有固定写法,非常灵活

#include<iostream>
using namespace std;
class Person
{
public:
    int age;
    string name;
    Person(){

    }
    Person(int age,string name){
        this->age = age;
        this->name = name;
    }
    void operator()(string name,int age){
        cout<<"name:"<<name<<endl;
        cout<<"age:"<<age<<endl;
    }
    Person operator()(Person & p){
        this->age+=p.age;
        this->name+=p.name;
        return *this;
    }
};
void test01()
{
    Person p1(10, "Tom");
    Person p2(10, "Tom");
    p1("Jerry", 20);
    p1(p2);
    cout << "p1.age = " << p1.age << " p1.name = " << p1.name << endl;
}
int main() {
    test01();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值