10.3 C++运算符重载实现的过程,代码

目录

运算符重载背景(operator)

定义

重载的方法

不能重载的运算符

运算符重载注意事项

代码实现

运行结果


运算符重载背景(operator)

自定义的类中,系统默认只提供两个运算符供用户使用,分别是赋值运算符(=)和取地址运算符(&),其余运算符只使用用基本数据类型,对于构造数据类型而言,不可直接使用,除非将该运算符进行重载操作.

定义

运算符重载是静态多态的一种,能够实现"一符多用",使用运算符重载,能够完成运算符作用于类对象之间,使得代码更加简洁、可读性更强。

重载的方法

operator# (#表示运算符号)

不能重载的运算符

1> 成员运算符 .

2> 成员指针运算符 .*

3> 作用域限定符 ::

4> 求字节运算符 sizeof

5> 三目运算符 ?:

运算符重载注意事项

1> 运算符重载,不能改变运算符的优先级

2> 运算符重载,不能改变运算符操作数个数

3> 运算符重载,不能更改运算符的本质逻辑,如不允许在加法运算符重载函数中,实现加法

4> 运算符重载,不能自己造运算符,是在已有的运算符的基础上进行重载操作

5> 运算符重载,不能改变运算符的结合律

6> 运算符重载,一般不设置默认参数

代码实现
cpp 复制代码
#include <iostream>

using namespace std;
//定义一个复数类
class Complex
{
private:
    int real; //实部
    int vir;  //虚部
public:
    Complex(){}
    Complex(int r,int v):real(r),vir(v){}

    //定义展示函数
    void show()
    {
        if(vir>=0)
        {
            cout<<real<<" + "<<vir<<"i"<<endl;
        }else
        {
            cout<<real<<vir<<"i"<<endl;
        }
    }
    //成员函数版实现减法(-)运算符重载
    const Complex operator- (const Complex &R)
    {
        Complex c;
        c.real=this->real-R.real;
        c.vir=this->vir-R.vir;
        return c;
    }
    //重载:+=  实部+=实部 虚部+=虚部
    Complex & operator+=(const Complex &R)
    {
        this->real+=R.real;
        this->vir+=R.vir;
        return *this;
    }
  //重载:-=
    Complex & operator-=(const Complex &R)
    {
        this->real-=R.real;
        this->vir-=R.vir;
        return *this;
    }
     //重载中括号[]运算符
    int & operator[](int index)
    {
        if(index==0)
        {
            return real;
        }
        else if(index==1)
        {
            return vir;
        }
    }


    //重载前置自增运算符重载函数:实部 = 实部+1   虚部=虚部+1
    Complex &operator++()
    {
        ++this->real;
        ++this->vir;

        return *this;
    }
    //重载后置自增运算符重载函数:实部 = 实部+1   虚部=虚部+1
    Complex operator++(int)
    {
        Complex c;

        c.real = this->real++;
        c.vir = this->vir++;

        return c;
    }

};
int main()
{
    Complex c1(5,3);
    Complex c2(2,1);
    c1.show();    //测试

    Complex c3=c1-c2;
    c3.show();

    Complex c4=c1+=c2;   //测试+=
    c4.show();

    Complex c5=c1-=c2;
    c5.show();

    c5[0]=10;        //将实部参数进行修改
    c5.show();

    Complex c6=++c1;   //测试自增
    c6.show();
    Complex c7=c2++;;
    c7.show();
    return 0;
}
运行结果
相关推荐
郝学胜-神的一滴1 小时前
Effective Python 条款45:巧用属性注解平滑升级类属性
运维·开发语言·数据库·python·网络协议·numpy
不积跬步无以至千里mmbb261 小时前
【软件工艺师 rust相关课程】
开发语言·后端·rust
SEO_juper1 小时前
HTTPS 混合内容检查工具:页面全站 https 了,但还有 http 资源在拖后腿
开发语言·网络协议·http·https·node.js·json·bash
神一样的老师2 小时前
WS63 BLE 配网实战:用 Python 在 PC 上替代手机完成 WiFi 配网
开发语言·python·智能手机
geovindu3 小时前
python: Decision Tree
开发语言·后端·python·决策树
时间的情敌4 小时前
Three.js 工业场景性能优化合集|大批量管线、InstancedMesh 实例化、合并 Geometry、显存释放
开发语言·javascript·性能优化·three.js
步峰代理-盾山5 小时前
R语言爬虫实战:使用httr与XML解析网页数据并进行统计分析
xml·开发语言·爬虫·r语言·网络爬虫
sharemylove15 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习
郝学胜-神的一滴19 小时前
Effective Python 条款43:自定义容器类
开发语言·windows·python·pandas·scikit-learn·pyqt
好焊孙辉20 小时前
一条产线省下 30 个电机位:Z 轴和 R 轴,该合成一个了
开发语言·r语言