甘肃省住房建设厅户网站/网站维护工作内容
文章目录
- 👉日期类介绍👈
- 👉日期类实现👈
- 📕 成员变量
- 📕 构造函数
- 📕 对应月份天数
- 📕 赋值重载
- 📕 比较运算符重载
- 📕 计算 运算符重载
- 👉源代码👈
- 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 类
文章目录👉日期类介绍👈👉日期类实现👈📕 成员变量📕 构造函数📕 对应月份天数📕 赋值重载📕 比较运算符重载📕 计算 运算符重载👉源代码…...

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来说,一个数据包的有效载荷最大为20字节对于BLE4.2以上,数据包的有效载荷扩大为251字节传输速率在不考虑跳频间隔的情况下,最大传输速率为:1)BLE4.0/4.1的理论吞吐率为39kb/s;2&am…...

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

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

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

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

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加虚拟内存
具体操作如下: #打开性能模式 sudo nvpmodel -m 0 && sudo jetson_clocks #增加swap空间,防止爆内存 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搜索详细讲解与操作
全文检索基础 全文检索流程 流程: #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. 二进制链表转整数
作者:小卢 专栏:《Leetcode》 喜欢的话:世间因为少年的挺身而出,而更加瑰丽。 ——《人民日报》 目录 33. 搜索旋转排序数组 1290. 二进制链表转整数 33. 搜索旋转排序数组 33. 搜索旋转排序…...

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

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

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

Hadoop入门常见面试题与集群时间同步操作
目录 一,常用端口号 Hadoop3.x : Hadoop2.x: 二,常用配置文件: Hadoop3.x: Hadoop2.x: 集群时间同步: 时间服务器配置(必须root用户): (1)…...

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 项目沟通管理 项目沟通管理包括通过开发工件,以及执行用于有效交换信息的各种活动,来确保项目及其相关方的信息需求得以满足的各个过程。项目沟通管理由两个部分组成:第一部分是制定…...

2.JVM常识之 运行时数据区
1.JVM核心组成 2.JVM 运行时数据区(jdk8) 程序计数器:线程私有,当前线程所执行字节码的行号指示器 jvm栈:线程私有,Java 虚拟机栈为 JVM 执行 Java 方法服务 本地方法栈:线程私有,本…...

你的游戏帐号是如何被盗的
据报道,2022上半年,中国游戏市场用户规模达到了5.54亿人,游戏市场销售收入1163.1亿元,相较去年均为同比增长的情况。如此庞大的市场规模,黑色产业链是绕不开的话题。 但相较于游戏中大家常见的玩家与玩家、玩家与官方…...

C++11异步编程
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言1、std::future和std::shared_future1.1 std:future1.2 std::shared_future2、std::async3、std::promise4、std::packaged_task前言 C11提供了异步操作相关的类…...

20230310----重返学习-DOM元素的操作-时间对象-定时器
day-024-twenty-four-20230310-DOM元素的操作-时间对象-定时器 复习 获取元素 id document.getElementById() 类名 document.getElementsByClassName() 标签名 document.getElementsByTagName() name属性 document.getElementsByName() 选择器 document.querySelector()docum…...

江苏专转本转本人后悔排行榜
江苏专转本转本人后悔排行榜 一、复习的太迟: 后悔指数:五颗星。 复习越到最后,时间一天天变少,要复习的内容还有很多,很多人都后悔没有早早开始,总想着多给我两月一定会考上的。 担心时间不够用,那就努力利…...

【算法时间复杂度】学习记录
最近开算法课,开几篇文章记录一下算法的学习过程。 关于算法的重要性 学习计算机当程序员的话,在编程过程中是绕不开算法这个大矿山的,需要我们慢慢挖掘宝藏。 算法(Algorithm)是指用来操作数据、解决程序问题的一组…...

汽车车机芯片Linux系统内核编译问题总结
谈到车机,很多人会想到华为问界上装的大屏车机,号称车机的天花板,基于鸿蒙OS的,而今天谈到的车机芯片用的是linux内核Kernel,对于它的编译,很多人一时会觉得头大,的确如果工具不是很齐全,就会遇到这样那样的问题,但是过程都会有错误提示,按照错误提示基本可以解决,而…...

Android13 音量曲线调整
Android13 音量曲线调整 Android13 上配置文件的路径: /vendor/sprd/modules/audio/engineconfigurable_apm/工程目录/system/etc/audio_engine_config/audio_policy_engine_stream_volumes.xml /vendor/sprd/modules/audio/engineconfigurable_apm/工程目录/sys…...

OpenHarmony通过MQTT连接 “改版后的华为IoT平台”
一、前言 本篇文章我们使用的是BearPi-HM_Nano开发板:小熊派的主板+E53_IA1扩展板 源码用的是D6_iot_cloud_oc,点击下载BearPi-HM_Nano全量源码 那么为什么要写这篇呢? 前段时间看到OpenHarmony群里,经常有小伙伴问接入华为IoT平台的问题,他们无法正常连接到华为IoT平台等…...

SQS (Simple Queue Service)简介
mazon Simple Queue Service (SQS)是一种完全托管的消息队列服务,可以让你分离和扩展微服务、分布式系统和无服务应用程序。 在讲解SQS之前,首先让我们了解一下什么是消息队列。 消息队列 还是举一个电商的例子,一个用户在电商网站下单后付…...

高速PCB设计指南系列(三)
第一篇 高密度(HD)电路的设计 本文介绍,许多人把芯片规模的BGA封装看作是由便携式电子产品所需的空间限制的一个可行的解决方案,它同时满足这些产品更高功能与性能的要求。为便携式产品的高密度电路设计应该为装配工艺…...