C++运算符重载

为什么要有运算符重载?
    如果类没有重载运算符,类的对象不能进行运算符的操作 运算符重载的本质是函数重载,函数名是以运算符的形式来命名,调用函数也是通过运算符来调用。

1、定义

1. 重载:给运算符重新赋予新的含义,在类的内部定义的运算符重载和成员函数是一样的
2. 重载方法:定义一个重载运算符的函数 在需要执行被重载的运算符时,系统会自动调用该函数;
3. 重载运算符格式:
函数类型 operator 运算符名称(形参列表)
例子:bool operator == (const Person &s);
4. 运算符重载的参数个数由运算符本身决定,但是类型可以自定义
5. 由运算符的左值调用运算符的重载
6. 如果类没有重载运算符,类的对象不能进行运算符的操作
7. 运算符重载虽然对返回值类型和参数类型没有要求,但是我们依然不能随便定义;返回值类型和参数的 类型一定要符合习惯才能让代码变得更优雅。

2.  +号的重载

下面是+号运算符重载示例

#include <iostream>
using namespace std;
class Person
{
    int age;
public:
    Person(int age):age(age){}
    void show()
    {
        cout<<age<<endl;
    }
    //运算符重载函数 +
    void operator +(Person& other)//p2
    {
        int res = this->age+other.age;
        cout<<res<<endl;
    }
};

int main()
{
    Person p1(12);
    Person p2(4);
    //调用成员函数
    // p1.operator +(p2);
    //1.左侧调用运算符 右侧为参数
    p1+p2;//自动调用 运算符重载
    p2+p1;
    return 0;
}

3.  >=运算符重载

#include <iostream>
using namespace std;
class Person
{
public:
    int _age;
    Person(int age):_age(age)
    {
    }
    int operator >=(Person& other)  // >=运算符重载
    {
        if(this->age>=other.age)
            return 1;
        else
            return 0;
    }
};
int main()
{
    Person per1(14);
    Person per2(6);
    cout << (per1 >= per2) << endl;
    return 0;
}

4.  =赋值运算符重载

注意:C++ 会对每个类默认重载赋值运算符,默认的逻辑是成员变量逐个赋值。
示例:
#include <iostream>
using namespace std;
class Person
{
    int age;
    string name;
public:
    Person(int a):age(a){
        cout<<"构造"<<endl;
    }
    Person(string name,int a):name(name),age(a)
    {}
    Person(const Person& o){
        cout<<"拷贝"<<endl;
    }
    void operator=(Person& other){     //=赋值运算符重载
        cout<<"=重载"<<endl;  
        this->age = other.age;
    }
    void show(){
        cout<<age<<endl;
    }
};

int main()
{
    Person p(10);
    Person p1(20);
    Person p2(p);//创建新对象,拷贝
    p = p1;//两个已经存在的对象,调用赋值运算符的重载
    p.show();
    p1.show();
    return 0;
}
示例 :系统默认的赋值运算符
Person& operator=(const Person& other)
{
    this->age = other.age;   //age为成员变量
    return *this;
}

5. 数组对 [] 的重载

#include <iostream>
using namespace std;
class Person
{
public:
    int length;
    int *p;
public:
    Person(int length):length(length){
        p = new int[length];
        for(int i = 0 ; i < length ;i++)
        p[i] = i;
    }
    int operator[](int index)  //2
    {
        if(index == 0)
        {
            cout<<"error"<<endl;
            return -1;
        }
        return p[index];
    }
    ~Person()
    {
        if(NULL != p)
        delete[] p;
    }
};

int main()
{
    Person p(5);
    cout<<p.operator [](2)<<endl;  //p[2]
}

6.  <<输出运算符重载

格式:
ostream &operator << (ostream &os, const class &classname) {return os}
1 )参数 1 ostream 引用,参数 2 是对象的常引用
2 )返回值保证连续输出
3 )必须在类外实现
4 )类友元。因为一般成员数据都是私有的,外部定义的运算符无法访问私有成员,这时就需要将运算符 重载在类中声明为友元函数。
示例:
#include <iostream>
using namespace std;
class Cperson
{
    //istream 输入流 ostream 输出流
    friend ostream &operator << (ostream &os, const Cperson &ob);
public:
    Cperson(int age):_age(age){}
private:
    int _age;
};

//必须是全局的
ostream &operator << (ostream &os, const Cperson &ob)
{
    os << ob._age;//私有的变量,要加友元
    return os;
}

int main(){
    Cperson per1(14);
    Cperson per2(18);
    cout << per1 << " " << per2 << endl;
    return 0;
}

7.  不能重载的运算符 5

?:(三目运算符)   

sizeof()   

 . (成员运算符)

:: (作用域运算符)

.* (成员指针运算符)

除了以上的运算符重载外还有其他的运算符重载,这里就不一一展示了,本次代码分享到此结束,感谢大家观看,希望大家点点赞,点点关注,谢谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值