当前位置: 首页 > news >正文

C++类的运算符重载

目标

让自定义的类直接使用运算符运算

代码

头文件及类定义

#include <iostream>using namespace std;
class Complex
{int rel;int vir;
public:void show(){cout <<"("<<this->rel<<","<<this->vir<<"i"<<")"<<endl;}Complex(int rel,int vir):rel(rel),vir(vir){}Complex(){}Complex operator+(const Complex other);Complex operator-(const Complex other);Complex operator/(const Complex other);Complex operator*(const Complex other);Complex operator%(const Complex other);bool operator==(const Complex other);bool operator>(const Complex other);bool operator<(const Complex other);bool operator&&(const Complex other);bool operator||(const Complex other);bool operator!();Complex operator++();Complex operator--();friend Complex operator++(Complex &c1,int);friend Complex operator--(Complex &c1,int);};

自增自减运算符

//后自增
Complex operator++(Complex &c1,int)
{Complex temp;temp.rel=(c1.rel)++;temp.vir=(c1.vir)++;cout <<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//后自减
Complex operator--(Complex &c1,int)
{Complex temp;temp.rel=c1.rel--;temp.vir=(c1.vir)--;cout <<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//前自增
Complex Complex::operator++()
{Complex temp;temp.rel=++(this->rel);temp.vir=++(this->vir);cout <<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//前自减
Complex Complex::operator--()
{Complex temp;temp.rel=--(this->rel);temp.vir=--(this->vir);cout <<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}

逻辑判断运算符

//等于判断
bool Complex::operator==(const Complex other)
{cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"=="<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout<<boolalpha<<((this->rel==other.rel)&&(this->vir==other.vir))<<endl;return (this->rel==other.rel)&&(this->vir==other.vir);
}
//大于判断
bool Complex::operator>(const Complex other)
{cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<">"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout<<boolalpha<<((this->rel>other.rel)&&(this->vir>other.vir))<<endl;return (this->rel>other.rel)&&(this->vir>other.vir);
}//小于判断
bool Complex::operator<(const Complex other)
{cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"<"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout<<boolalpha<<((this->rel<other.rel)&&(this->vir<other.vir))<<endl;return (this->rel<other.rel)&&(this->vir<other.vir);
}

逻辑与或非

//逻辑与
bool Complex::operator&&(const Complex other)
{cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"&&"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout<<boolalpha<<((this->rel||this->vir)&&(other.rel||other.vir))<<endl;return (this->rel||this->vir)&&(other.rel||other.vir);
}
//逻辑或
bool Complex::operator||(const Complex other)
{cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"||"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout<<boolalpha<<((this->rel||this->vir)||(other.rel||other.vir))<<endl;return (this->rel||this->vir)||(other.rel||other.vir);
}
//逻辑非
bool Complex::operator!()
{cout <<"!"<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<":";cout<<noboolalpha<<(!(this->rel||this->vir))<<endl;return !(this->rel||this->vir);
}

算术运算符

//成员函数版本的加法运算符重载
Complex Complex::operator+(const Complex other)
{Complex temp;temp.rel =  this->rel+other.rel;temp.vir = this->vir+other.vir;cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"+"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout <<"="<<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//成员函数版本的减法运算符重载
Complex Complex::operator-(const Complex other)
{Complex temp;temp.rel = this->rel-other.rel;temp.vir = this->vir-other.vir;cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"-"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout <<"="<<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//成员函数版本的除法运算符重载
Complex Complex::operator/(const Complex other)
{Complex temp;temp.rel = this->rel/other.rel;temp.vir = this->vir/other.vir;cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"/"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout <<"="<<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//成员函数版本的乘法运算符重载
Complex Complex::operator*(const Complex other)
{Complex temp;temp.rel = this->rel*other.rel;temp.vir = this->vir*other.vir;cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"*"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout <<"="<<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//成员函数版本的除余运算符重载
Complex Complex::operator%(const Complex other)
{Complex temp;temp.rel = this->rel%other.rel;temp.vir = this->vir%other.vir;cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"%"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout <<"="<<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}

主函数

int main()
{Complex c1(9,25);Complex c2(3,5);c1.operator+(c2);c1.operator-(c2);c1.operator*(c2);c1.operator/(c2);c1.operator%(c2);c1.operator==(c2);c1.operator<(c2);c1.operator>(c2);c1.operator&&(c2);c1.operator||(c2);!(c1);++(c1);--(c1);(c1)++;(c1)--;c1.show();return 0;
}

实现效果

知识点思维导图

http://www.lryc.cn/news/502437.html

相关文章:

  • 泷羽Sec学习笔记-zmap搭建炮台
  • 分析M0G突破后急剧下跌内因,x.game阐述不利面延续多久
  • 网络爬虫全解析
  • 《孤岛惊魂4》无法启动提示缺少“msvcp100.dll”快速修复方法!
  • GS-SLAM论文阅读--RGBDS-SLAM
  • 条件编译->enable_if和 if constexpr使用区别
  • 介绍一下CSS中伪类和伪元素的概念
  • 【橘子ES】熔断器Circuit breaker
  • 6.4 CPU性能分析--Intel处理器跟踪技术
  • 期权懂|如何用第三方平台开通期权?
  • JS中const有没有变量提升
  • Axure RP全面介绍:功能、应用与中文替代方案
  • WordPress用户首次登录强制修改密码
  • AI开源南京分享会回顾录
  • 基于事件驱动的websocket简单实现
  • 【leetcode100】反转链表
  • 禅道Bug的一次迁移
  • c段和旁站讲解(附查询网址)
  • Linux编译Kernel时的文件zImage、文件dtb(dtbs)、核心模块分别是什么东西?
  • 【深度学习】深刻理解“变形金刚”——Transformer
  • 75_pandas.DataFrame 中查看和复制
  • 打电话玩手机识别-支持YOLO,COCO,VOC格式的标记,超高识别率可检测到手持打电话, 非接触式打电话,玩手机自拍等
  • 生产慎用之调试日志对空间矢量数据批量插入的性能影响-以MybatisPlus为例
  • 单片机:实现倒计时(附带源码)
  • 什么是多线程中的上下文切换
  • 如何在windwos批量拉取go mod
  • 【Three.js基础学习】29.Hologram Shader
  • 文件包含进阶玩法以及绕过姿态
  • Markdown编辑器工具--Typora
  • PyTorch 的 torch.unbind 函数详解与进阶应用:中英双语