日期类的实现(C++实现)

完整呈现

Date.h

#include <iostream>
using namespace std;
//日期类
class Date
{
public:int GetMonthDays(int year, int month) const;//构造函数Date(int year = 0, int month = 1, int day = 1);//拷贝构造Date(const Date& d);//打印void Print();//析构函数~Date();//运算符的重载bool operator==(const Date& d) const;// = 运算符的重载Date& operator=(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;//日期+=天数,自己要改变Date& operator+=(int day);//日期加日期+,自己不改变Date operator+(int day)const;   //Date operaotor+(Date* this, int day)//日期 -= 天数Date& operator-=(int day);//日期 - 天数Date operator-(int day)const;// 前置++  后置++ // 都不能加constDate& operator++();Date operator++(int);// 前置--  后置--Date& operator--();Date operator--(int);//日期 - 日期int operator-(const Date& d) const;
private:int _year;int _month;int _day;
};

Date.cpp

#include "Date.h"
int Date::GetMonthDays(int year, int month)const
{static int MonthDays[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;}return MonthDays[month];
}
//构造函数
Date::Date(int year, int month, int day)
{if (year >= 0&& month >= 1 && month <= 12&& day >= 1 && day <= GetMonthDays(year, month)){this->_year = year;this->_month = month;this->_day = day;}else{cout << "非法日期" << endl;}
}
//拷贝构造
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}
//打印
void Date::Print()
{cout << _year << "-" << _month << "-" << _day << endl;
}
//析构函数
Date::~Date()
{;
}//运算符的重载
bool Date:: operator==(const Date& d) const
{return this->_year == d._year&& _month == d._month&& _day == d._day;
}
// = 运算符的重载
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
{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;elsereturn false;
}
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;//else//	return false;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);
}
bool Date:: operator!=(const Date& d) const
{return !(*this == d);
}
//日期+=天数,自己要改变
Date& Date:: operator+=(int day)
{if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDays(_year, _month)){_day -= GetMonthDays(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this;
}//日期加日期+,自己不改变
Date Date:: operator+(int day)const   //Date operaotor+(Date* this, int day)
{/*Date ret(*this);ret._day += day;while (ret._day > GetMonthDays(ret._year, ret._month)){ret._day -= GetMonthDays(ret._year, ret._month);++ret._month;if (ret._month == 13){++ret._year;ret._month = 1;}}return ret;*/Date ret(*this);ret += day; // ret.operator+=(day)return ret;
}//日期 -= 天数
Date& Date:: operator-=(int day)
{if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){_month--;if (_month == 0){_month = 12;_year = 1;}_day += GetMonthDays(_year, _month);}return *this;
}
//日期 - 天数 自身不改变
Date Date:: operator-(int day) const
{//Date ret(*this);// Date ret = *this//ret._day -= day;//while (ret._day <= 0) //{//	--ret._month;//	if (ret._month == 0) //	{//		ret._month = 12;//		--ret._year;//	}//	ret._day += GetMonthDays(ret._year, ret._month);//}//return ret;Date ret = *this;ret -= day;return ret;
}// 前置++  后置++,默认前置++
Date& Date:: operator++()
{*this += 1;return *this;
}
Date Date:: operator++(int)  //为了构成函数重载,后置++
{Date tmp(*this);*this += 1;return tmp;
}
// 前置--  后置--
Date& Date:: operator--()
{*this -= 1;return *this;
}
Date Date:: operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}//日期 - 日期
int Date:: operator-(const Date& d) const   // const Date* this 
{int flag = 1;Date max = *this; // 拷贝构造Date min = d;if (*this < d){max = d;   // operator= min = *this;flag = -1;}int n = 0;while (max != min){++min;++n;}return n * flag;
}

Test.cpp

#include "Date.h"int main()
{Date d1(2024, 7, 16);Date d2(2024, 7, 15);d1.Print();d2.Print();cout << "-------------------" << endl;// 运算符的重载// == cout << (d1 == d2) << endl;// <cout << (d1 < d2) << endl;// <=cout << (d1 <= d2) << endl;// >cout << (d1 > d2) << endl;//日期加日期 + +=//Date d3(2024, 7, 15);//d3.Print();// d3.operator+(&d3,10);//d3 += 1000;//d3.Print();//Date d4(2024, 7, 15);//d4.Print();//d4 -= 100;//d4.Print();Date d3(2024, 7, 15);Date d4(2024, 7, 16);cout << d4 - d3 << endl;--d3;d3.Print();Date d5;d5.Print();return 0;
}

部分函数解读

日期 += 天数

1,当需要加的天数是正数的时候:

        先用天数加上需要加的天数,然后用天数和当前月比较,如果超出了当前月的天数,就减去当前月的天数然后进一位月份,如果超出了12月,年就进一位,当天数小于当前月的天数的时候,就不需要进位了。

        日期+天数也是同理,只不过多了一次拷贝构造一个临时对象,日期+天数保证自身不改变

 2, 当日期是负数的时候,相对于日期 - 天数,即: 日期 - (-day), 在实现日期 -= 天数的基础上直接调用日期 -= 天数

Date& Date:: operator+=(int day)
{if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDays(_year, _month)){_day -= GetMonthDays(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this;
}

日期 -= 天数

        1,当天数是正数的时候,天数相减如果是正数,一定满足在当前月,如果是负数,则不满足当前月,需要向前一个月借,如果还是负数,继续借,直接借到0月,没有0月,则去借上一年的12月,直接月份为正数。则满足当前月的天数

        2, 负数和日期+=日期类似,如果日期 -= 负数 即相当于日期 += (-day), 在实现日期+=天数的基础上直接调用日期+=天数

Date& Date:: operator-=(int day)
{if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){_month--;if (_month == 0){_month = 12;_year = 1;}_day += GetMonthDays(_year, _month);}return *this;
}

最终总结与回顾

  1. 缺省参数:当声明有了缺省参数,定义的时候不需要再加缺省参数。
  2. const成员:const可以调用非const,const可以调用const,如果对象在函数内部不改变,则可以使用const修饰,如果在函数内部发生改变,则不能加const
  3. 引用返回:如果对象出了作用域还在,则可以使用引用返回,减少一次拷贝构造,如果对象出了作用域不在了,不能使用引用返回
  4. 日期类的实现,在有了类和对象的基础上实现并不复杂

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/51213.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Redis与MySQL数据一致性问题的策略模式及解决方案

目录 一、策略模式 1、旁路缓存模式&#xff08;Cache Aside Pattern&#xff09; 2、读写穿透&#xff08;Read-Through/Write-Through&#xff09; 3、异步缓存写入&#xff08;Write Behind&#xff09; 二、一致性解决方案 1、缓存延迟双删 2、删除重试机制 3、读取…

【python】sklearn基础教程及示例

【python】sklearn基础教程及示例 Scikit-learn&#xff08;简称sklearn&#xff09;是一个非常流行的Python机器学习库&#xff0c;提供了许多常用的机器学习算法和工具。以下是一个基础教程的概述&#xff1a; 1. 安装scikit-learn 首先&#xff0c;确保你已经安装了Python和…

Unity 资源 之 Pop It 3D 解压玩具与双人AI游戏 Unity 资源包分享

精彩呈现&#xff1a;Pop It 3D 解压玩具与双人AI游戏 Unity 资源包分享 一、Pop It 3D 解压玩具的魅力二、双人游戏的互动乐趣三、Unity 游戏资源包的优势四、如何获取资源包 亲爱的游戏爱好者们&#xff0c;今天为大家带来一款令人兴奋的游戏资源——Pop It 3D 解压玩具双人带…

数字陷波器的设计和仿真(Matlab+C)

目录 一、数字陷波器的模型 二、Matlab仿真 1. 示例1 2. 示例2 三、C语言仿真 1. 由系统函数计算差分方程 2. 示例代码 一、数字陷波器的模型 二、Matlab仿真 1. 示例1 clear clc f0=100;%滤掉的100Hz fs=1000;%大于两倍的信号最高频率 r=0.9; w0=2*pi*f0/fs;%转换到…

[图解]《分析模式》漫谈19-Midjourney、Sora

1 00:00:02,360 --> 00:00:03,360 今天的漫谈 2 00:00:03,370 --> 00:00:04,560 我们来说一下 3 00:00:04,570 --> 00:00:08,720 人工智能&#xff0c;还是前言 4 00:00:08,890 --> 00:00:11,840 这里有一句话 Kent Beck 5 00:00:12,630 --> 00:00:13,750 W…

Spring Boot配置文件的语法规则

主要介绍两种配置文件的语法和格式&#xff0c;properties和yml 目录 1.配置文件的作用 2.创建配置文件 3.properties语法 4.yml语法 5.配置文件格式 1.配置文件的作用 对于配置文件&#xff0c;也有独立的文件夹去存放&#xff0c;主要用来存放一些需要经过变动的数据&a…

python绘制方波信号

python绘制方波信号 1、效果 2、导入库 pip install numpy pip install matplotlib3、实现代码 # -*- coding: utf-8 -*-""" @contact: 微信 1257309054 @file: test.py @time: 2024/7/28 14:48 @author: LDC """ import numpy as np import …

IOS-04 Swift 中数组、集合、字典、区间、元组和可选类型

在 Swift 编程语言中&#xff0c;数据结构和类型的合理运用对于高效编程至关重要。接下来&#xff0c;我们将深入探讨数组、集合、字典、区间、元组和可选类型的相关知识。 一、数组&#xff08;Array&#xff09; &#xff08;一&#xff09;元素定义 可以通过多种方式定义数…

Hello 算法:动画图解、一键运行的数据结构与算法教程

Hello 算法 《Hello 算法》是一份开源、免费的数据结构与算法入门教程&#xff0c;特别适合新手。全书采用动画图解&#xff0c;内容清晰易懂&#xff0c;学习曲线平滑&#xff0c;引导初学者探索数据结构与算法的知识地图。源代码可以一键运行&#xff0c;帮助读者通过练习提…

C#中的同步编程和异步编程

1. 简单描述一下同步编程和异步编程 同步编程&#xff1a;按照代码的顺序一行一行执行&#xff0c;如果某个操作需要等待&#xff08;比如读取文件、网络请求、数据库操作等&#xff09;&#xff0c;那么当前的线程就会停下来&#xff0c;一直到这个操作完成了之后&#xff0c…

git学习(一)

一、代码仓库的初始化 1、先在本地操作&#xff0c;不涉及到远程服务器&#xff0c;创建目录mkdir git demo 想要本地创建的目录成为一个远程仓库就需要初始化git init git init 后会发生什么&#xff1f; 2、watch -n 1 -d tind每隔1s打印当前文件目录并且刷新 左边命令 wa…

搞DDR,你是可以看看我的这篇笔记(三)

关于DDR PHY这个部分,是数模混合器件,工作涉及到了很多信号完整性,眼图,模拟等相关的东西我就没讲了。因为确实不太熟悉,只能站在架构、功能、使用上去聊聊。 上一篇我们看了这个图片,简化就是下面这个样子: 其实这个也不太合适~~~ 这样舒服多了,一般DDRC和DDRPHTY都会…

被工信部认可的开源软件治理解决方案

近日&#xff0c;工信部网络安全产业发展中心正式发布了“2023年信息技术应用创新解决方案”&#xff0c;开源网安凭借“基于SCA技术开源软件治理解决方案”顺利入选&#xff0c;成为经工信部认可的优秀解决方案&#xff0c;这是开源网安连续两届荣获此荣誉。 工业和信息化部网…

17.延迟队列

介绍 延迟队列&#xff0c;队列内部是有序的&#xff0c;延迟队列中的元素是希望在指定时间到了以后或之前取出和处理。 死信队列中&#xff0c;消息TTL过期的情况其实就是延迟队列。 使用场景 1.订单在十分钟内未支付则自动取消。 2.新创建的店铺&#xff0c;如果十天内没…

【Redis系列】RedisTemplate的使用与注意事项

目录 一.什么是RedisTemplate 二.如何使用RedisTemplate RedisTemplate的API 序列化 三.StringRedisTemplate 一.什么是RedisTemplate RedisTemplate 是一个工具类&#xff0c;由 Spring 官方提供的方便操作 Redis 数据库的一个工具类&#xff0c;来源于 org.springframe…

【LLM】-10-部署llama-3-chinese-8b-instruct-v3 大模型

目录 1、模型下载 2、下载项目代码 3、启动模型 4、模型调用 4.1、completion接口 4.2、聊天&#xff08;chat completion&#xff09; 4.3、多轮对话 4.4、文本嵌入向量 5、Java代码实现调用 由于在【LLM】-09-搭建问答系统-对输入Prompt检查-CSDN博客 关于提示词注入…

涉密移动载体智能柜管控系统DW-S404|国产自主可控

东识移动载体管控系统载体柜系统采用RFID识别技术&#xff0c;结合智能载体管理软件&#xff0c;实现了文件载体权限管理、定位管理、智能存取、智能盘点、在线监控等功能&#xff0c;同时对文件载体进行规范化、智能化、自动化管理。工作人员通过授权进行文件、载体、卷宗等存…

C# 简单的单元测试

文章目录 前言参考文档新建控制台项目新建测试项目添加引用添加测试方法测试结果(有错误)测试结果&#xff0c;通过正规的方法抛出异常 总结 前言 听说复杂的项目最好都要单元测试一下。我这里也试试单元测试这个功能。到时候调试起来也方便。 参考文档 C# 单元测试&#xf…

这一文,关于 Java 泛型的点点滴滴 一(泛型基础、类型擦除)

作为一个 Java 程序员&#xff0c;用到泛型最多的&#xff0c;我估计应该就是这一行代码&#xff1a; List<String> list new ArrayList<>();这也是所有 Java 程序员的泛型之路开始的地方啊。 不过本文讲泛型&#xff0c;先不从这里开始讲&#xff0c;而是再往前…

富唯智能转运机器人:高效、智能、未来的选择

在现代工业中&#xff0c;高效的物流和物料处理是提升生产效率的关键。富唯智能转运机器人&#xff0c;以其卓越的技术和智能化的设计&#xff0c;为各行业提供了完美的解决方案。 产品概述 富唯智能转运机器人搭载ICD系列核心控制器&#xff0c;拥有多种移载平台&#xff0c…