news 2026/4/22 23:55:45

C++之双目运算符重载

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++之双目运算符重载

文章目录

  • 运算符重载
  • 双目运算符重载

运算符重载

#include<iostream>usingnamespacestd;classComplex{private:doubler;doublei;public:Complex(doubler,doublei){this->r=r;this->i=i;}voidprint(void){cout<<r<<" + "<<i<<"i"<<endl;}};intmain(void){Complexa(1,2);Complexb(3,4);a.print();b.print();intx=10;inty=20;intz=x+y;a+b;//想像x+y 实现Complex对象加运算 +运算符新的逻辑功能 要实现+运算符的重载return0;}

双目运算符重载

  • 双目运算符:有左右两个操作数的操作符 L#R
    • 算术运算:*、/、%、+、-
    • 关系运算:>、>=、<、<=、==、!=
    • 逻辑运算:&&、||
    • 位运算:&、|、^、<< 、>>
    • 赋值与复合赋值:=、+=、-=、*=、/=、%=、&=、|=、^=、<<= 、>>=
    • … …
  • 注意:
    • 表达式结果是右值
    • 左右操作数既可以是左值也可以是右值
  • 实现方式:
    • 成员函数形式:L.operator#®
    • 友元函数形式:operator#(L,R)
#include<iostream>usingnamespacestd;classComplex{private:doubler;doublei;public:Complex(doubler,doublei){this->r=r;this->i=i;}voidprint(void){cout<<r<<" + "<<i<<"i"<<endl;}constComplexoperator+(constComplex&c){Complextmp(r+c.r,i+c.i);returntmp;}friendconstComplexoperator-(constComplex&l,constComplex&r);};constComplexoperator-(constComplex&l,constComplex&r){Complextmp(l.r-r.r,l.i-r.i);returntmp;}intmain(void){Complexa(1,2);Complexb(3,4);a.print();b.print();Complex c=a+b;// a.operator+(b);c.print();Complex d=c-a;// operator-(c, a);d.print();return0;}
  • 对赋值类双目运算符重载时需要注意的事项:
    • 表达式的结果是左值,就是左操作数的自身
    • 左操作数必须是左值,右操作数可以是左值也可以是右值
#include<iostream>usingnamespacestd;classComplex{private:doubler;doublei;public:Complex(doubler,doublei){this->r=r;this->i=i;}voidprint(void){cout<<r<<" + "<<i<<"i"<<endl;}constComplexoperator+(constComplex&c){Complextmp(r+c.r,i+c.i);returntmp;}Complex&operator+=(constComplex&c){r=r+c.r;i=i+c.i;return*this;}friendconstComplexoperator-(constComplex&l,constComplex&r);friendComplex&operator-=(Complex&L,constComplex&R);};constComplexoperator-(constComplex&l,constComplex&r){Complextmp(l.r-r.r,l.i-r.i);returntmp;}Complex&operator-=(Complex&L,constComplex&R){L.r-=R.r;L.i-=R.i;returnL;}intmain(void){Complexa(1,2);Complexb(3,4);a.print();b.print();Complex c=a+b;// a.operator+(b);c.print();Complex d=c-a;// operator-(c, a);d.print();a+=b;//a.operator+=(b);a.print();(a-=b).print();return0;}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 12:23:58

【计算机毕业设计案例】基于springboot的食品安全管理系统基于SpringBoot的在线食品安全信息平台(程序+文档+讲解+定制)

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/4/23 13:43:47

机房IP和住宅IP有什么区别?

当我们选择要使用的代理时&#xff0c;最常见的两种代理是动态住宅代理和数据中心代理。 市场上有成千上万的代理提供商&#xff0c;这使得选择更加困难&#xff0c;尤其是对于新手来说。 为了减少代理测试的费用&#xff0c;我建议优先选择提供免费试用的公司。 我现在正在测试…

作者头像 李华
网站建设 2026/4/11 1:36:09

AI效率加速器选购指南:10款工具基础版VS专业版深度评测

&#xfffd;&#xfffd; 10大降AIGC平台核心对比速览 排名 工具名称 降AIGC效率 适用场景 免费/付费 1 askpaper ⭐⭐⭐⭐⭐ 学术论文精准降AI 付费 2 秒篇 ⭐⭐⭐⭐⭐ 快速降AIGC降重 付费 3 Aibiye ⭐⭐⭐⭐ 多学科论文降AI 付费 4 Aicheck ⭐⭐⭐⭐…

作者头像 李华
网站建设 2026/4/23 12:18:12

Java毕设选题推荐:基于Java+SpringBoot的校园外卖服务系统基于springboot的校园外卖服务系统【附源码、mysql、文档、调试+代码讲解+全bao等】

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/4/23 12:11:25

如何查看 vmware esxi中 虚拟机硬盘对应系统的哪块盘

查看 vmware esxi中 虚拟机硬盘对应系统的哪块盘1. 需求如下2. 如何实现3. 在Linux系统中查看3.1 查看主机控制器编号3.2. 查看所有硬盘SCSI 信息3.3. 根据已有的信息判断VC上的硬盘对应虚拟机的物理设备4. 在 Windows系统中查看1. 需求如下 在VC界面上可以看出我的虚拟机有3块…

作者头像 李华