当前位置: 首页 > 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;
}

实现效果

My_string 添加逻辑判断运算符重载

代码

#include <iostream>
#include <cstring>using namespace std;
char c = '\0';
class My_string
{char *str;     //记录C风格的字符串int size;      //记录字符串长度
public://无参构造My_string():str(new char('\0')),size(0){}//有参构造My_string(const char *str):size(strlen(str)+1),str(new char [strlen(str)+1]){strcpy(this->str,str);}//拷贝构造My_string(const My_string &other):size(other.size),str(new char [other.size+1]){strcpy(str,other.str);}//拷贝赋值My_string &operator=(const My_string &other){delete this->str;this->size=other.size;str=new char[other.size+1];strcpy(str,other.str);return *this;}//析构函数~My_string (){delete []str;}//at函数char &my_at(int num);bool operator==(const My_string other);bool operator>(const My_string other);bool operator<(const My_string other);bool operator!();My_string operator+(const My_string other);void show();
};
void My_string::show()
{cout <<this->str<<endl;
}
My_string My_string::operator+(const My_string other)
{My_string temp;delete []temp.str;temp.str=new char[this->size+other.size+1];temp.str=strcat(this->str,other.str);return temp;
}
bool My_string::operator==(const My_string other)
{return (strcmp(this->str,other.str)==0);
}bool My_string::operator>(const My_string other)
{return (strcmp(this->str,other.str)>0);
}bool My_string::operator<(const My_string other)
{return (strcmp(this->str,other.str)<0);
}bool My_string::operator!()
{return !(str);
}
char &My_string::my_at(int num)
{if(num<0||num>=size){cout<<"访问越界"<<endl;return c;}return str[num];
}
int main()
{My_string s1("zzz");My_string s2("www");My_string s3=s1;cout <<(s1==s2)<<endl;cout <<(s1==s3)<<endl;cout <<(s1<s2)<<endl;cout <<(s1>s2)<<endl;cout <<(!s1)<<endl;My_string s4=s1+s2;s4.show();return 0;
}

实现效果

知识点思维导图


http://www.mrgr.cn/news/80031.html

相关文章:

  • 【深度学习入门】深度学习介绍
  • appium学习之二:adb命令
  • ️️️ 避坑指南:如何修复国密gmssl 库填充问题并提炼优秀加密实践20241212
  • Activity的finish()流程
  • Python Bokeh库:实现实时数据可视化的实战指南
  • 力扣题目 - 2931.购买物品的最大开销
  • Python爬虫之Scrapy框架基础入门
  • 安宝特分享 | AR技术助力医院总院与分院间的远程面诊
  • Redis经典面试题
  • Mac使用总结
  • Elasticsearch Java Api Client中DSL语句的查询方法汇总(二)
  • 电脑直连设备+window使用nfs挂载设备
  • Android显示系统(10)- SurfaceFlinger内部结构
  • 【常考前端面试题总结】---2025
  • ansible自动化运维(三)jinja2模板roles角色管理
  • uni-app实现小程序、H5图片轮播预览、双指缩放、双击放大、单击还原、滑动切换功能
  • 【数据结构进阶】AVL树深度剖析 + 实现(附源码)
  • 【电机控制器】FM33LF015芯片——FLASH模拟EEPROM
  • 黑皮书-计算机科学导论02
  • react-dnd 拖拽事件与输入框的文本选中冲突