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

【C++】运算符重载练习——Date 类

文章目录

  • 👉日期类介绍👈
  • 👉日期类实现👈
    • 📕 成员变量
    • 📕 构造函数
    • 📕 对应月份天数
    • 📕 赋值重载
    • 📕 比较运算符重载
    • 📕 计算 运算符重载
  • 👉源代码👈
    • Date.h
    • Date.cpp

👉日期类介绍👈

日期类是用来描述具体日期的,涉及到 年、月、日,当然,也可以有多个日期之间的关系。

class Date
{friend istream& operator>>(istream& in, Date& d);friend ostream& operator<<(ostream& out,const Date& d);public:int GetMonthDay(int year, int month);Date(int year = 2023, int month = 2, int day = 3);bool operator==(const Date& d)const;bool operator!=(const Date& d)const;bool operator<(const Date& d)const;bool operator<=(const Date& d)const;bool operator>(const Date& d)const;bool operator>=(const Date& d)const;Date& operator=(const Date& d);Date& operator+=(int day);Date operator+(int day);Date& operator++();    // 前置++Date operator++(int);  // 后置++ ,int 参数只是和前置++ 区分Date& operator-=(int day);Date operator-(int day);int operator-(const Date& d)const;void operator<<(ostream& out);void Print()const;private:int _year;int _month;int _day;
};

👉日期类实现👈

📕 成员变量

private:int _year;int _month;int _day;

📕 构造函数

	Date::Date(int year,int month, int day){assert(month > 0 && month < 13);_year = year;_month = month;_day = day;}

📕 对应月份天数

由于申明和定义分离,申明是在 Date 这个命名空间里面的,所以要用访问限定修饰符。
每个月天数是对应的,可以用数组存储,除了闰年的二月,可以单独判断。

int Date::GetMonthDay(int year, int month)
{assert(month > 0 && month < 13);int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0)){return 29;}else{return monthArray[month];}
}

📕 赋值重载

要考虑一个情况,就是自己赋值给自己,这种情况什么都不用做,所以直接用 if 语句跳过。
当然,传参和传返回值都是引用,提高效率。

	Date& Date::operator=(const Date& d){if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;}

📕 比较运算符重载

实际只要写两个, == 和 < (当然, == 和 > 也可以),其他的可以复用这两个。

	bool Date::operator==(const Date& d)const{return _year == d._year&& _month == d._month&& _day == d._day;}bool Date::operator!=(const Date& d)const{return !(*this == d);}bool Date::operator<(const Date& d)const{if (_year < d._year)return true;else if (_year == d._year && _month < d._month)return true;else if (_year == d._year && (_month == d._month) && _day < d._day)return true;return false;}bool Date::operator<=(const Date& d)const{return *this < d || *this == d;}bool Date::operator>(const Date& d)const{return !(*this <= d);}bool Date::operator>=(const Date& d)const{return !(*this < d);}

📕 计算 运算符重载

如下, += 重载返回值是 Date& ,是为了方便 d = d1+=x; 这样的情况,d1 先加上 x天的日期,赋值给 d 。

+的重载可以直接复用 += 。因为,例如 d1 + 99 ,是返回 d1 这个日期 99天之后的具体日期,但是 d1 本身不会改变,所以,返回值绝对不可以是 d1 的引用,而要是一个另一个变量。返回该变量的时候,要创建临时变量并进行拷贝构造。
但是,如果先写+,再让+= 复用+ ,就会导致 += 也要进行拷贝构造,有小号。

另外,-= 和 - 的重载,也和上面类似的原理,- 复用 -=
而对于后置++,编译器会自动传一个int 类型的参数,我们不用去理会。
计算两个日期的天数,有了前面的重载就很容易,只需要日期小的一直++,直到和另一个日期相等即可。

Date& Date::operator+=(int day)
{if (day < 0){*this -= -day;return *this;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){_month = 1;_year++;}}return *this;
}Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}Date& Date::operator++()
{*this += 1;return *this;
}// 调用后置++ 的时候,编译器自动传一个 int 类型的参数
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}Date& Date::operator-=(int day)
{if (day < 0){*this += -day;return *this;}_day -= day;while (_day <= 0){_month -= 1;if (_month == 0){_year -= 1;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day)
{Date tmp = *this;tmp -= day;return tmp;
}int Date::operator-(const Date& d)const
{Date max = *this;Date min = d;int flag = 1;if (max < min){max = d;min = *this;flag = -1;}int ret = 0;while (max != min){++min;++ret;}return ret*flag;}

👉源代码👈

Date.h

#pragma once
#include<iostream>
#include<assert.h>
#include<stdlib.h>
using namespace std;class Date
{friend istream& operator>>(istream& in, Date& d);friend ostream& operator<<(ostream& out,const Date& d);public:int GetMonthDay(int year, int month);Date(int year = 2023, int month = 2, int day = 3);//Date(const Date& d);bool operator==(const Date& d)const;bool operator!=(const Date& d)const;bool operator<(const Date& d)const;bool operator<=(const Date& d)const;bool operator>(const Date& d)const;bool operator>=(const Date& d)const;Date& operator=(const Date& d);Date& operator+=(int day);Date operator+(int day);Date& operator++();    // 前置++Date operator++(int);  // 后置++ ,int 参数只是和前置++ 区分Date& operator-=(int day);Date operator-(int day);int operator-(const Date& d)const;void operator<<(ostream& out);void Print()const;private:int _year;int _month;int _day;
};// 使用内联函数
inline ostream& operator<<(ostream& out, const Date& d)
{cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}inline istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}

Date.cpp

#include"Date.h"
int Date::GetMonthDay(int year, int month)
{assert(month > 0 && month < 13);int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0)){return 29;}else{return monthArray[month];}
}Date::Date(int year,int month, int day){assert(month > 0 && month < 13);_year = year;_month = month;_day = day;}bool Date::operator==(const Date& d)const{return _year == d._year&& _month == d._month&& _day == d._day;}bool Date::operator!=(const Date& d)const{return !(*this == d);}bool Date::operator<(const Date& d)const{if (_year < d._year)return true;else if (_year == d._year && _month < d._month)return true;else if (_year == d._year && (_month == d._month) && _day < d._day)return true;return false;}bool Date::operator<=(const Date& d)const{return *this < d || *this == d;}bool Date::operator>(const Date& d)const{return !(*this <= d);}bool Date::operator>=(const Date& d)const{return !(*this < d);}Date& Date::operator=(const Date& d){if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;}void Date::Print()const{cout << _year << "年" << _month << "月" << _day << "日" << endl;}Date& Date::operator+=(int day)
{if (day < 0){*this -= -day;return *this;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){_month = 1;_year++;}}return *this;
}// + 复用 += 比较好,这样可以减少拷贝构造
// 因为 + 无论如何都是要拷贝构造的,如果 += 复用+ ,那么+= 也要拷贝构造
Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}Date& Date::operator++()
{*this += 1;return *this;
}// 调用后置++ 的时候,编译器自动传一个 int 类型的参数
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}Date& Date::operator-=(int day)
{if (day < 0){*this += -day;return *this;}_day -= day;while (_day <= 0){_month -= 1;if (_month == 0){_year -= 1;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day)
{Date tmp = *this;tmp -= day;return tmp;
}int Date::operator-(const Date& d)const
{Date max = *this;Date min = d;int flag = 1;if (max < min){max = d;min = *this;flag = -1;}int ret = 0;while (max != min){++min;++ret;}return ret*flag;}void Date::operator<<(ostream& out)
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

相关文章:

【C++】运算符重载练习——Date 类

文章目录&#x1f449;日期类介绍&#x1f448;&#x1f449;日期类实现&#x1f448;&#x1f4d5; 成员变量&#x1f4d5; 构造函数&#x1f4d5; 对应月份天数&#x1f4d5; 赋值重载&#x1f4d5; 比较运算符重载&#x1f4d5; 计算 运算符重载&#x1f449;源代码&#x1…...

Redis学习(13)之Lua脚本【环境准备】

文章目录一 Lua入门环境准备1.1 Lua简介1.2 Linux 系统安装Lua1.2.1 Lua 下载1.2.2 Lua 安装1.3 Hello World1.3.1 命令行模式1.3.2 脚本文件模式1.3.3 两种脚本运行方式1.4 Win安装Lua1.4.1 LuaForWindows的安装1.4.2 SciTE修改字体大小1.4.3 SciTE中文乱码1.4.4 SciTE快捷键工…...

关于BLE的一些知识总结

数据包长度对于BLE4.0/4.1来说&#xff0c;一个数据包的有效载荷最大为20字节对于BLE4.2以上&#xff0c;数据包的有效载荷扩大为251字节传输速率在不考虑跳频间隔的情况下&#xff0c;最大传输速率为&#xff1a;1&#xff09;BLE4.0/4.1的理论吞吐率为39kb/s&#xff1b;2&am…...

Spring框架源码分析一

如何看源码&#xff08;方法论&#xff09;不要忽略源码中的注释使用翻译工具先梳理脉络&#xff0c;然后梳理细节即总分总&#xff0c;先总体过一遍&#xff0c;再看细节&#xff0c;再做一个总结大胆猜测&#xff08;8分靠猜&#xff09;&#xff0c;小心验证&#xff0c;再调…...

CSS常用内容总结(扫盲)

文章目录前言相关概念【了解】脚本语言什么是脚本语言脚本语言有什么特点常见的脚本语言什么是动态语言&#xff0c;什么是静态语言动态语言和静态语言两者之间有何区别CSSCSS是什么CSS的特点一、CSS代码怎么写基本语法规则引入方式内部样式内联样式表外部样式代码风格二、CSS的…...

Java启蒙之语言基础

目录 一.Java标识符和关键字 1.1Java标识符 1.2Java关键字 二.数据类型和变量的概述和关系 2.1Java变量 2.2Java的数据类型 2.2.1数据类型的分类的概述 2.2.2数据类型的转换 3.Java运算符 总结 &#x1f63d;个人主页&#xff1a;tq02的博客_CSDN博客-领域博主 &#…...

数据库系统--T-SQL数据查询功能-多表查询(超详细/设计/实验/作业/练习)

目录课程名&#xff1a;数据库系统内容/作用&#xff1a;设计/实验/作业/练习学习&#xff1a;T-SQL数据查询功能-多表查询一、前言二、环境与设备三、内容四、内容练习题目&#xff1a;对应题目答案&#xff1a;五、总结课程名&#xff1a;数据库系统 内容/作用&#xff1a;设…...

Spring Boot 3.0系列【14】核心特性篇之Configuration相关注解汇总介绍

有道无术,术尚可求,有术无道,止于术。 本系列Spring Boot版本3.0.3 源码地址:https://gitee.com/pearl-organization/study-spring-boot3 文章目录 前言@Configuration@ConfigurationProperties@EnableConfigurationProperties@ConfigurationPropertiesScan@Configuratio…...

[ubuntu][jetson]给jetson增加swap空间类似于给windows加虚拟内存

具体操作如下&#xff1a; #打开性能模式 sudo nvpmodel -m 0 && sudo jetson_clocks #增加swap空间&#xff0c;防止爆内存 swapoff -a sudo fallocate -l 15G /swapfile sudo chmod 600 /var/swapfile sudo mkswap /swapfile sudo swapon /swapfile…...

小黑子—Java从入门到入土过程:第二章

Java零基础入门2.0Java系列第二章1. 注释和关键字2. 字面量3. 变量3.1 基本用法3.2 使用方式3.3 注意事项4. 变量练习5. 计算机中的数据存储5.1 计算机的存储规则5.2 进制5.3 进制间转换二进制转十八进制转十十六进制转十十进制转其他进制6. 数据类型7. 定义变量的练习8. 标识符…...

ElasticSearch搜索详细讲解与操作

全文检索基础 全文检索流程 流程&#xff1a; #mermaid-svg-7Eg2qFEl06PIEAxZ {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-7Eg2qFEl06PIEAxZ .error-icon{fill:#552222;}#mermaid-svg-7Eg2qFEl06PIEAxZ .error…...

web实现太极八卦图、旋转动画、定位、角度、坐标、html、css、JavaScript、animation

文章目录前言1、html部分2、css部分3、JavaScript部分4、微信小程序演示前言 哈哈 1、html部分 <div class"great_ultimate_eight_diagrams_box"><div class"eight_diagrams_box"><div class"eight_diagrams"><div class&…...

【LeetCode】33. 搜索旋转排序数组、1290. 二进制链表转整数

作者&#xff1a;小卢 专栏&#xff1a;《Leetcode》 喜欢的话&#xff1a;世间因为少年的挺身而出&#xff0c;而更加瑰丽。 ——《人民日报》 目录 33. 搜索旋转排序数组 1290. 二进制链表转整数 33. 搜索旋转排序数组 33. 搜索旋转排序…...

IBM Semeru Windows 下的安装 JDK 17

要搞清楚下载那个版本&#xff0c;请参考文章&#xff1a;来聊聊 OpenJDK 和 JVM 虚拟机下载地址semeru 有认证版和非认证版&#xff0c;主要是因为和 OpenJ9 的关系和操作系统的关系而使用不同的许可证罢了&#xff0c;本质代码是一样的。在 Windows 下没有认证版&#xff0c;…...

Lambda表达式和steram流

目录 引言&#xff1a; 语法: Lambda 表达式实例&#xff1a; demo演示&#xff1a; Stream流&#xff1a; 引言&#xff1a; Lambda 表达式&#xff0c;也可称为闭包&#xff0c;它是推动 Java 8 发布的最重要新特性。 Lambda 允许把函数作为一个方法的参数&#xff08;函…...

面试必会-MySQL篇

1. Mysql查询语句的书写顺序Select [distinct ] <字段名称>from 表1 [ <join类型> join 表2 on <join条件> ]where <where条件>group by <字段>having <having条件>order by <排序字段>limit <起始偏移量,行数>2. Mysql查询语…...

Hadoop入门常见面试题与集群时间同步操作

目录 一&#xff0c;常用端口号 Hadoop3.x &#xff1a; Hadoop2.x&#xff1a; 二&#xff0c;常用配置文件&#xff1a; Hadoop3.x: Hadoop2.x: 集群时间同步&#xff1a; 时间服务器配置&#xff08;必须root用户&#xff09;&#xff1a; &#xff08;1&#xff09…...

JS 数组去重的方法

// 数组去重 const arr ["1", "1", "2", "3", "5", "3", "1", "5", "4"] console.log(this.deduplicate(arr)) // [1, 2, 3, 5, 4] // 数组对象去重 const arr [ { id: 1, nam…...

PMP项目管理项目沟通管理

目录1 项目沟通管理2 规划沟通管理3 管理沟通4 监督沟通1 项目沟通管理 项目沟通管理包括通过开发工件&#xff0c;以及执行用于有效交换信息的各种活动&#xff0c;来确保项目及其相关方的信息需求得以满足的各个过程。项目沟通管理由两个部分组成&#xff1a;第一部分是制定…...

2.JVM常识之 运行时数据区

1.JVM核心组成 2.JVM 运行时数据区&#xff08;jdk8&#xff09; 程序计数器&#xff1a;线程私有&#xff0c;当前线程所执行字节码的行号指示器 jvm栈&#xff1a;线程私有&#xff0c;Java 虚拟机栈为 JVM 执行 Java 方法服务 本地方法栈&#xff1a;线程私有&#xff0c;本…...

应用升级/灾备测试时使用guarantee 闪回点迅速回退

1.场景 应用要升级,当升级失败时,数据库回退到升级前. 要测试系统,测试完成后,数据库要回退到测试前。 相对于RMAN恢复需要很长时间&#xff0c; 数据库闪回只需要几分钟。 2.技术实现 数据库设置 2个db_recovery参数 创建guarantee闪回点&#xff0c;不需要开启数据库闪回。…...

微信小程序之bind和catch

这两个呢&#xff0c;都是绑定事件用的&#xff0c;具体使用有些小区别。 官方文档&#xff1a; 事件冒泡处理不同 bind&#xff1a;绑定的事件会向上冒泡&#xff0c;即触发当前组件的事件后&#xff0c;还会继续触发父组件的相同事件。例如&#xff0c;有一个子视图绑定了b…...

(十)学生端搭建

本次旨在将之前的已完成的部分功能进行拼装到学生端&#xff0c;同时完善学生端的构建。本次工作主要包括&#xff1a; 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...

.Net框架,除了EF还有很多很多......

文章目录 1. 引言2. Dapper2.1 概述与设计原理2.2 核心功能与代码示例基本查询多映射查询存储过程调用 2.3 性能优化原理2.4 适用场景 3. NHibernate3.1 概述与架构设计3.2 映射配置示例Fluent映射XML映射 3.3 查询示例HQL查询Criteria APILINQ提供程序 3.4 高级特性3.5 适用场…...

无法与IP建立连接,未能下载VSCode服务器

如题&#xff0c;在远程连接服务器的时候突然遇到了这个提示。 查阅了一圈&#xff0c;发现是VSCode版本自动更新惹的祸&#xff01;&#xff01;&#xff01; 在VSCode的帮助->关于这里发现前几天VSCode自动更新了&#xff0c;我的版本号变成了1.100.3 才导致了远程连接出…...

1.3 VSCode安装与环境配置

进入网址Visual Studio Code - Code Editing. Redefined下载.deb文件&#xff0c;然后打开终端&#xff0c;进入下载文件夹&#xff0c;键入命令 sudo dpkg -i code_1.100.3-1748872405_amd64.deb 在终端键入命令code即启动vscode 需要安装插件列表 1.Chinese简化 2.ros …...

相机从app启动流程

一、流程框架图 二、具体流程分析 1、得到cameralist和对应的静态信息 目录如下: 重点代码分析: 启动相机前,先要通过getCameraIdList获取camera的个数以及id,然后可以通过getCameraCharacteristics获取对应id camera的capabilities(静态信息)进行一些openCamera前的…...

Unity | AmplifyShaderEditor插件基础(第七集:平面波动shader)

目录 一、&#x1f44b;&#x1f3fb;前言 二、&#x1f608;sinx波动的基本原理 三、&#x1f608;波动起来 1.sinx节点介绍 2.vertexPosition 3.集成Vector3 a.节点Append b.连起来 4.波动起来 a.波动的原理 b.时间节点 c.sinx的处理 四、&#x1f30a;波动优化…...

【数据分析】R版IntelliGenes用于生物标志物发现的可解释机器学习

禁止商业或二改转载&#xff0c;仅供自学使用&#xff0c;侵权必究&#xff0c;如需截取部分内容请后台联系作者! 文章目录 介绍流程步骤1. 输入数据2. 特征选择3. 模型训练4. I-Genes 评分计算5. 输出结果 IntelliGenesR 安装包1. 特征选择2. 模型训练和评估3. I-Genes 评分计…...

html css js网页制作成品——HTML+CSS榴莲商城网页设计(4页)附源码

目录 一、&#x1f468;‍&#x1f393;网站题目 二、✍️网站描述 三、&#x1f4da;网站介绍 四、&#x1f310;网站效果 五、&#x1fa93; 代码实现 &#x1f9f1;HTML 六、&#x1f947; 如何让学习不再盲目 七、&#x1f381;更多干货 一、&#x1f468;‍&#x1f…...