C++运算符重载

一、运算符重载的定义

对于编译器内置的类型(如int,double)之类的,我们常用的运算符(如“+”,“==”等),是可以自动识别的。但是如果是我们自己定义的类型,编译器并不知道要如何进行操作,如果我们将这个运算符要怎么用告诉编译器,就可以让运算符达到我们想要的操作效果。这个就是运算符重载的定义。

二、运算符重载的语法

这里涉及关键字operator。具体的语法与定义一个函数类似,为:
返回值类型+operator+要重载的运算符+()
示例:

int operator+ (student &stu_1, student &stu_2)

三、+号的重载

#include <iostream>
#include <string>

using namespace std;

class student {
public:
    int age;
    student(int age)
    {
        this->age = age;
    }
  

};

//int operator+ (student &stu_1, student &stu_2)
//{
//     return stu_1.age+stu_2.age;
   
//}


void test1()
{
    student stu_1(18);
    student stu_2(18);
   cout<<"两个学生的年龄之和为: "<<stu_1+stu_2<<endl<<endl;
   
    
}



int main()
{
    test1();            
    system("pause");    
}

如上述代码所示:如果没有对+号进行重载,它无法识别自定义的类型student。但如果我们进行了重载操作,则可以得到我们想要的成果(将两个学生的年龄相加)
在这里插入图片描述
在这里插入图片描述

四、左移运算符“<<”的重载

先查看"<<"的定义:
在这里插入图片描述
ostream是标准输出流,我们需要以它作为返回值类型。

#include <iostream>
#include <string>

using namespace std;

class student {
    friend ostream & operator<<(ostream &out, student &stu_1);
public:
    int age;
    string name;
    student(string name,int age,int score)
    {
        this->name=name;
        this->age = age;
        this->score=score;
    }

private:
    int score;
  

};

ostream & operator<<(ostream &out, student &stu_1)
{
    out<< stu_1.age<<"岁的";
    out << stu_1.name<<"考了";
    out << stu_1.score<<"分"<<endl;
    return out;
}


void test1()
{
   student stu_1("Tom",18,100);
   cout<<stu_1;

    
}



int main()
{
    test1();            
    system("pause");    
}
  1. 这里将返回类型定义为&ostream的原因是:如果返回类型是void,无法实现链式编程。
  2. 左移运算符只能定义为全局函数,因为若是定义为成员变量函数,在调用是会产生如下效果:
    生成一个对象,但是此时对象变成在左移运算符左边了,编译器会报错。

五、加号运算符的重载

灵活处理的话,加号可以把很多类型进行叠加。比如让同一类中的某个属性相加,或者两个字符串进行相加等。

#include <iostream>
#include <string>

using namespace std;

class student {
    friend ostream & operator<<(ostream &out, student &stu_1);
    friend int operator+ (student&stu_1,student&stu_2);
public:
    int age;
    string name;
    student(string name,int age,int score)
    {
        this->name=name;
        this->age = age;
        this->score=score;
    }

private:
    int score;
  

};

ostream & operator<<(ostream &out, student &stu_1)
{
    out<< stu_1.age<<"岁的";
    out << stu_1.name<<"考了";
    out << stu_1.score<<"分"<<endl;
    return out;
}

int operator+ (student&stu_1,student&stu_2)
{
    return stu_1.score+stu_2.score;
}

void test1()
{
   student stu_1("Tom",18,100);
   student stu_2("jerry",18,100);
   cout<<stu_1<<endl;
   cout<<stu_2<<endl;
   cout<<"它们两个加起来考了"<<stu_1+stu_2<<"分"<<endl;

    
}



int main()
{
    test1();            
   
    system("pause");    
}


六、自增和自减符号重载

自增和自减符号重载主要注意的是在如何区别重载的是变量前面的符号还是后面的。可以根据以下方法判别:
重载时,形参若是有占位符,则为变量后面的重载,否则为前面的重载

int operator++(student &stu_3)   //变量前面的++号重载
{
   return  ++stu_3.score;
}

int operator++(student &stu_3,int)   //变量后面的++号重载
{
   return  stu_3.score++;
}

int operator--(student &stu_3)  //变量前面的--号重载
{
   return  --stu_3.score;
}

int operator--(student &stu_3,int)   //变量后面的++号重载
{
   return  stu_3.score--;
}

七、函数调用运算符()的重载

()的重载必须作为成员函数来执行

int operator()(student &stu_)
    {   
     return this->score+stu_.score;
    }

八、不重载&&和||

先介绍&&和||的短路特性:
&& 短路特性: A && B 如果A为假 B将不会执行
|| 短路特性: A || B 如果A为真 B将不会执行
由于无法实现这种短路特性,所以不要选择重载这两个符号。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值