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

C++运算符重载(日期类的运算符重载为例)

一、运算符重载简介

C++为了增强代码的可读性,引入了运算符重载。

运算符重载是一个拥有特殊函数名的函数,其函数名为 operator+重载的运算符,其余的返回值类型和参数列表与普通函数类似。调用时可以使用函数名调用,也可以直接使用重载的运算符来调用。

如下代码,d1 == d2会被编译器替换为operator==(d1, d2);d1 == d2会被编译器替换为d1.operator==(d2)

运算符重载注意事项:

1.不能用operator连接其他符号来创建新的运算符,如operator@(只能是C/C++中已存在的运算符)

2.不能改变用于内置类型的运算符含义

3.以下五个运算符不能重载:

.(类成员访问运算符)

.*(类成员指针访问运算符)

::(域运算符)

sizeof(长度运算符)

? :(条件运算符)

二、重载日期类中的运算符 ==  <  <=  >  >=  

1.全局的运算符重载(全局运算符重载时,类中的成员变量需要改为公有的,否则需要提供Get接口或者利用友元解决,此处以成员变量为公有的为例)

class Date {
public:int _year = 1;int _month = 1;int _day = 1;
public://有参构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}
};
//Date类==重载
bool operator==(const Date& d1, const Date& d2)
{return d1._year == d2._year&& d1._month == d2._month&& d1._day == d2._day;
}
//Date类<重载
bool operator<(const Date& d1, const Date& d2)
{if (d1._year < d2._year){return true;}else if (d1._year == d2._year){if (d1._month < d2._month){return true;}else if (d1._month == d2._month){if (d1._day < d2._day){return true;}}}return false;
}//Date类>重载
bool operator>(const Date& d1, const Date& d2)
{return !((d1 == d2) || (d1 < d2));
}//Date类<=重载
bool operator<=(const Date& d1, const Date& d2)
{return (d1 < d2) || (d1 == d2);
}//Date类>=重载
bool operator>=(const Date& d1, const Date& d2)
{return (d1 > d2) || (d1 == d2);
}int main()
{Date d1(2024, 2, 17);Date d2(2024, 2, 16);cout << operator==(d1, d2) << endl;//0cout << (d1 == d2) << endl;//0cout << operator<(d1, d2) << endl;//0cout << (d1 < d2) << endl;//0cout << operator>(d1, d2) << endl;//1cout << (d1 > d2) << endl;//1cout << operator>=(d1, d2) << endl;//1cout << (d1 >= d2) << endl;//1cout << operator<=(d1, d2) << endl;//0cout << (d1 <= d2) << endl;//0return 0;
}

2.运算符重载为成员函数

class Date {
public:int _year = 1;int _month = 1;int _day = 1;
public://有参构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//Date类==重载bool operator==(const Date& d){return this->_year == d._year&& this->_month == d._month&& this->_day == d._day;}//Date类<重载bool operator<(const Date& d){if (this->_year < d._year){return true;}else if (this->_year == d._year){if (this->_month < d._month){return true;}else if (this->_month == d._month){if (this->_day < this->_day){return true;}}}return false;}//Date类>重载bool operator>(const Date& d){return !((*(this) < d) || (*(this) == d));}//Date类<=重载bool operator<=(const Date& d){return ((*this) < d) || (*(this) == d);}//Date类>=重载bool operator>=(const Date& d){return (*(this) > d) || (*(this) == d);}
};int main()
{Date d1(2024, 2, 17);Date d2(2024, 2, 16);cout << d1.operator==(d2) << endl;//0cout << (d1 == d2) << endl;//0cout << d1.operator<(d2) << endl;//0cout << (d1 < d2) << endl;//0cout << d1.operator<=(d2) << endl;//0cout << (d1 <= d2) << endl;//0cout << d1.operator>(d2) << endl;//1cout << (d1 > d2) << endl;//1cout << d1.operator>=(d2) << endl;//1cout << (d1 >= d2) << endl;//1return 0;
}

相关文章:

  • js---webAPI
  • 原型设计模式
  • 工作心得——css让元素居中的方法
  • 嵌入式linux驱动开发篇之设备树
  • php 数组函数
  • 关于jupyter的一些小笔记
  • Linux第48步_编译正点原子的出厂Linux内核源码
  • 【精选】Java面向对象进阶——接口细节:成员特点和接口的各种关系
  • 随想录刷题笔记 —二叉树篇7 617合并二叉树 700二叉搜索树中的搜索 98验证二叉搜索树
  • C++数据结构与算法——双指针法
  • python-使用ffmpeg批量修改文件的后缀名
  • vue自定义指令(图文示例)
  • Leetcode3026. 最大好子数组和
  • 基于BP算法的SAR成像matlab仿真
  • Sora时代,我们的AI应该何去何从?——关于Sora大模型的思考
  • ----------
  • 《微软的软件测试之道》成书始末、出版宣告、补充致谢名单及相关信息
  • Android优雅地处理按钮重复点击
  • Angular 4.x 动态创建组件
  • Angular Elements 及其运作原理
  • Effective Java 笔记(一)
  • Java多线程(4):使用线程池执行定时任务
  • Laravel 中的一个后期静态绑定
  • Netty源码解析1-Buffer
  • node 版本过低
  • SpiderData 2019年2月23日 DApp数据排行榜
  • Windows Containers 大冒险: 容器网络
  • 免费小说阅读小程序
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 如何抓住下一波零售风口?看RPA玩转零售自动化
  • 实现简单的正则表达式引擎
  • 使用Gradle第一次构建Java程序
  • 一道闭包题引发的思考
  • 译有关态射的一切
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • 在Unity中实现一个简单的消息管理器
  • const的用法,特别是用在函数前面与后面的区别
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (function(){})()的分步解析
  • (八)c52学习之旅-中断实验
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (考研湖科大教书匠计算机网络)第一章概述-第五节1:计算机网络体系结构之分层思想和举例
  • (理论篇)httpmoudle和httphandler一览
  • (三)模仿学习-Action数据的模仿
  • (学习日记)2024.01.09
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • .NET : 在VS2008中计算代码度量值
  • .NET CF命令行调试器MDbg入门(四) Attaching to Processes
  • .net core使用ef 6
  • .NET delegate 委托 、 Event 事件,接口回调