日期类的实现(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、读取…

NodeJS:npm的使用

npm时nodejs的包安装工具 1.查看版本 $ npm -v 9.6.7 2.升级npm $ sudo npm install npm -g 3.安装nodejs模块 $ npm install <Module Name> 通过该方法将在当前目录下创建文件夹node_modules&#xff0c;并将模块安装到node_modules中 可以通过-g参数指定模块为全局安…

Zygote 进程你不知道的东西

一、概述 1.Zygote&#xff08;孵化&#xff09; 进程是所有 Android进程的父进程&#xff0c;包括SystemServer和各种应用进程都是通过Zygote进程fork出来的。Zygote进程相当于Android系统的根进程&#xff0c;系统启动后所有的进程都是通过这个进程fork出来的。这样做的好处…

【python】sklearn基础教程及示例

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

Python:模块导入

step1. 创建一个.py文件&#xff0c;里面装载你想导入的内容 step2. 用import导入 import导入的两种方法 1.整体 import file_name 2.局部 from file_name import function_name 整体的调用&#xff1a; file_name.function() 局部的&#xff1a; function_name() 这是导入的文…

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

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

Linux安装Python并运行一个项目

Linux安装Python并运行一个项目 1、下载Python Python版本&#xff1a;3.10.11 操作系统&#xff1a;Centos 8.2 下载地址&#xff1a;https://www.python.org/ftp/python/3.10.11/Python-3.10.11.tar.xz 将文件放在&#xff1a;/opt/python 下 2、安装Python 先安装一些…

数字陷波器的设计和仿真(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…

Vue学习(二)计算属性、监视属性、样式绑定

计算属性 定义&#xff1a;如果一个要用的数据&#xff0c;而是由已有的属性&#xff08;data中的属性&#xff09;计算得来&#xff0c;那么可以将其作为计算属性 原理&#xff1a;底层借助了Object.defineproperty方法提供的getter和setter。 计算属性都放在vue实例中的co…

达梦脱机备份报错[-7170]:bakres连接DMAP失败

达梦脱机备份报错[-7170]:bakres连接DMAP失败 [dmdbatest1 ~]$ DmServiceDMSVR01 stop Stopping DmServiceDMSVR01: [ OK ] [dmdbatest1 ~]$ [dmdbatest1 ~]$ dmrman dmrman V8 RMAN> backup database /dm8/data/DM01/dm.ini full; backu…

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…

洛谷P1064金明的预算方案题解

题目传送门 思路 购买附件是一定要购买主件&#xff0c;并且附件最多有两个&#xff0c;所以一个主件搭配附件的方法只有四种&#xff08;选第一个&#xff0c;选第二个&#xff0c;都选&#xff0c;不选&#xff09;。所以我们在dp时只考虑主件&#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都会…

基于机器学习的股票预测及股票推荐系统的设计与实现

基于机器学习的股票预测及股票推荐系统的设计与实现 Design and Implementation of a Machine Learning-based Stock Prediction and Stock Recommendation System 完整下载链接:基于机器学习的股票预测及股票推荐系统的设计与实现 文章目录 基于机器学习的股票预测及股票推荐…