欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 学习记录——day41 运算符重载

学习记录——day41 运算符重载

2024/10/25 9:30:05 来源:https://blog.csdn.net/weixin_64918301/article/details/141816093  浏览:    关键词:学习记录——day41 运算符重载

        运算符默认不支持自定义的类,但是可以通过运算符重载扩展运算符的属于范围

        运算符重载,也是实现泛型编程的一种,能够实现“一符多用”,可以使用运算符重载来完成对象与对象之间的符号运算

        注:运算符重载仅针对类对象 不影响基础数据类型

一、运算符重载函数

1)函数名:统一为 operator# //这里的#表示要被重载的运算符

2)参数:根据运算符的功能和位置而定,

         如:全局函数版双目运算符参数有两个,成员函数版双目运算符参数一个

                全局函数版,单目运算符参数有一个,成员函数版单目运算符参数无

3> 返回值:根据实际情况而定

二、调用原则和调用机制

        调用时机:当对象使用该运算符时,系统自动调用运算符重载函数

        调用机制:左调右参原则,运算符左侧是函数的调用者,运算符右侧是函数的参数

                                例如:a = b;       //a.operator=(b);

                                           s1 + s2;   //s1.operator+(s2);

下列以此类为例:

class Complax
{
private:
        int real;            //实部    
        int vir;             //虚部
public:
         ......
};

三、算数运算符重载

1)种类:+、-、*、/、%

2)算术运算符属于双目运算符,格式:L # R; L表示左操作数,#表示运算符号,R表示右操作数

3)左操作(L):既可以是左值也可以是右值

     右操作数(R):既可以是左值也可以是右值

     运算结果:只能是右值

4)格式:

成员函数版:const 类名 operator#(const 类名 &R) const

           解析:第一个const保护运算结果不被修改

                      第二个const保护右操作数在运算过程中不被修改

                      第三个const保护左操作数自身在运算过程中不被修改

全局函数版:const 类名 operator#(const 类名 &L,const 类名 &R)

以 + 为例      (参数、返回值、函数体内部逻辑根据实际情况改变)

成员函数版

class Complax
{
private:int real;            //实部    int vir;             //虚部
public://成员函数版const Complax operator+(const Complax &R)const{Complax temp;       //无参构造temp.real = this->real + R.real;temp.vir = this->vir + R.vir;return temp;}
};    

 全局函数版

//全局函数版class Complax
{
private:int real;            //实部    int vir;             //虚部
public://实现全局函数版,需要将全局函数版设置成友元函数friend const Complax operator+(const Complax &L, const Complax &R);
};const Complax operator-(const Complax &L, const Complax &R)
{Complax temp;       //无参构造temp.real = L.real - R.real;temp.vir = R.vir - R.vir;return temp;
}

四、赋值类运算符重载

1)种类:=、+=、-=、*=、/=、%=

2)赋值运算符属于双目运算符,格式:L # R; L表示左操作数,#表示运算符号,R表示右操作数

3)左操作(L):只能是左值

     右操作数(R):既可以是左值也可以是右值

     运算结果:左操作数自身的引用

4)格式:

        成员函数版:类名 & operator#(const 类名 &R)

        全局函数版:类名 & operator#( 类名 &L,const 类名 &R)

五、关系运算符重载函数

1)种类:>、=、

2)关系运算符属于双目运算符,格式:L # R; L表示左操作数,#表示运算符号,R表示右操作数

3)左操作(L):既可以是左值也可以是右值

     右操作数(R):既可以是左值也可以是右值

     运算结果:bool类型的右值

4)格式:

        成员函数版:const bool operator#(const 类名 &R) const

        全局函数版:const bool operator#(const 类名 &L,const 类名 &R)

六、单目运算符

1)种类:-(负号) 、!

2)格式:# R; #表示运算符号,R表示右操作数

3)操作数(R):既可以是左值也可以是右值

     运算结果:同类的一个右值

4)格式:

        成员函数版:const 类名 operator#() const

        全局函数版:const bool operator#(const 类名 &R)

七、自增自减运算

1)种类:++ 、 --

2)格式:# R; #表示运算符号,R表示右操作数

3)操作数(R):只能是左值

      运算结果:  

              前置:是自身的引用

                         成员格式:类名 &operator#()

                         全局格式:类名 &operator#(类名 &R)              

           

              后置:临时值

                         成员格式:类名 operator#(int)

                         全局格式:类名 operator#(类名 &R, int)

八、插入和提取运算符重载

1)插入和提取运算符的来源

namespace std
{
    ostream cout;
    istream cin;
}

2)由于运算符重载函数的调用遵循左调有参原则,所以,自定义类的重载函数,需要流对象和自定义类的对象

        例如:cout

3)此时,原则上来说,需要在流所在的类对象中,写成员函数,难度较大,所以,对于该类的重载函数,我们只能实现全局函数版,将该函数作为类的友元函数

4)由于不需要使用istream和ostream类中的私有成员,所以,全局函数版的重载函数不需要在流对应的类中,声明友元函数

        如果,重载函数中,需要使用自定义的类中的私有成员,则需要在自定义类中,声明该全局函数为友元函数

5)格式:

        ostream& operator

        istream& operator>>(istream &L, 类名 &R)

九、类型转换运算符

1)有时,需要将类对象进行强制类型转换为其他数据类型,此时就需要在类体内提供类型转换运算符重载函数

2)定义格式: operator 要转换的类型() { 返回对应类型的数据 }

十、函数对象(仿函数)

1)本质上就是重载 () 运算符,由于调用时,跟函数调用一样,所以称为仿函数

2)定义格式: operator() (参数列表) {函数体内容}

3)调用格式:对象名(实参名); //对象 . operator() (实参名)

十一、运算符重载的限制

1>不能重载运算符 ::(作用域解析)、.(成员访问)、.*(通过成员指针的成员访问)及 ?:(三元条件)。

2>不能创建新运算符,例如 **、<> 或 &|。

3>运算符 && 与 || 的重载失去短路求值。

4>重载的运算符 -> 必须要么返回裸指针,要么(按引用或值)返回同样重载了运算符 -> 的对象。

5>不可能更改运算符的优先级、结合方向或操作数的数量。

6>&&、|| 和 ,(逗号)在被重载时失去其特殊的定序性质,并且即使不使用函数调用记法,也表现为与常规的函数调用相似。
(C++17 前)

示例

#include <iostream>using namespace std;//定义一个复数类: 由实部和虚部组成的一个数据  a + bi
class Complax
{
private:int real;            //实部int vir;             //虚部public:Complax() {cout<<"无参构造"<<endl;}            //无参构造Complax(int r, int v):real(r),vir(v) {cout<<"有参构造"<<endl;}   //有参构造~Complax() {cout<<"析构函数"<<endl;}           //析构函数//定义输出函数void show(){if(vir > 0){cout<<real << "+"<<vir<<"i"<<endl;}else{cout<<real << vir<<"i"<<endl;}}//定义成员函数版完成两个复数相加:实部+实部   虚部+虚部const Complax operator+(const Complax &R)const{Complax temp;       //无参构造temp.real = this->real + R.real;temp.vir = this->vir + R.vir;return temp;}//实现全局函数版,需要将全局函数版设置成友元函数friend const Complax operator-(const Complax &L, const Complax &R);//成员函数版完成加等于运算符重载: 实部 += 实部   虚部+=虚部Complax &operator+=(const Complax &R){this->real += R.real;this->vir += R.vir;return *this;}//定义成员函数版完成关系运算符重载: 实部>实部   && 虚部>虚部bool operator>(const Complax &R)const{return this->real>R.real && this->vir>R.vir;}//定义成员函数版完成负号运算符重载:   实部 = -实部    虚部=-虚部const Complax operator-()const{Complax temp;temp.real = -this->real;temp.vir = -this->vir;return temp;}//定义成员函数版完成++前置运算: 实部 +=1   虚部+=1Complax &operator++(){this->real++;this->vir++;return *this;}//定义成员函数版完成++后置运算: 实部 +=1   虚部+=1Complax operator++(int)        //这里括号中的int仅仅起到占位作用,使用的是哑元{Complax temp;temp.real = this->real++;temp.vir = this->vir++;return temp;}//将插入运算符重载函数,设置成友元函数friend ostream & operator<<(ostream &L, const Complax &R);//定义类型转换运算符重载函数operator int(){return this->real + this->vir;}//重载小括号运算符,完成仿函数void operator()(string msg){cout << msg<< endl;}};//定义全局函数版完成两个复数相减
const Complax operator-(const Complax &L, const Complax &R)
{Complax temp;       //无参构造temp.real = L.real - R.real;temp.vir = R.vir - R.vir;return temp;
}//自定义插入运算符重载函数:只能实现全局函数版
ostream & operator<<(ostream &L, const Complax &R)
{//输出内容if(R.vir > 0){L<<R.real << "+"<<R.vir<<"i"<<endl;}else{L<<R.real << R.vir<<"i"<<endl;}//返回左操作数自身return L;
}int main()
{Complax c1(5,7);           //有参构造c1.show();                //5+7iComplax c2(3, -2);            //有参构造c2.show();                    //3-2icout<<"**************************************"<<endl;Complax c3 = c1+c2;       //调用加号 运算符重载函数c3.show();                //8+5icout<<"**************************************"<<endl;(c1+=c2) += c3;          //调用加等于运算符重载函数c1.show();            //16+10ic2.show();             //3-2ic3.show();              //8+5icout<<"**************************************"<<endl;if(c1>c2)                      //调用关系运算符重载函数{cout<<"yes"<<endl;}else{cout<<"no"<<endl;}cout<<"**************************************"<<endl;Complax c4 = -c1;          //调用负号运算符重载c4.show();                  // -16 - 10icout<<"**************************************"<<endl;/*int num = 5;num++++;          //后置自增后,返回结果是一个右值++++num;          //前置自增后,返回的结果是一个左值*/Complax c5 = ++++c1;            //前置自增,先加再用c5.show();         //18+12ic1.show();         //18+12icout<<"**************************************"<<endl;Complax c6 = c1++;        //后置自增,先用再加c6.show();             //18+12ic1.show();             //19+13icout<<"**************************************"<<endl;cout<<c1 <<c2;          //cout.operator<<(c1);cout<<"**************************************"<<endl;int num = (int)c1;         //调用强制类型转换函数cout<<"num = "<<num<<endl;      //32cout<<"**************************************"<<endl;//仿函数的调用c1("hello world");cout<<"**************************************"<<endl;return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com