首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >揭秘:打造高效日期管理神器——日期类的深度实现

揭秘:打造高效日期管理神器——日期类的深度实现

作者头像
夜雨声烦1413
发布2026-01-12 15:36:55
发布2026-01-12 15:36:55
780
举报

hello,家人们,今天咱们来实现一个日期类,好啦,废话不多讲,开干.


在实现日期类之前,首先我们得确定日期类中有哪些函数需要实现滴,那么为了令这个日期类更加得系统化,我们实现一个声明与定义分离的日期类

1:Date.h

  1. 构造函数,当定义了一个对象时,我们需要构造函数帮助我们初始化对象.
  2. 检测用户提供的日期是否合法
  3. 由于闰年的2月是29天,平年的2月是28天且每个月的天数不一样,因此我们需要一个函数帮助我们提供每个月的天数.
代码语言:javascript
复制
#pragma once
#include <iostream> 
using namespace std;
#include <assert.h>
class Date
{
public:
	//构造函数
	Date(int year = 1, int month = 1, int day = 1);
	//获取某年某月的天数
	int Getmonthday(int year,int month)
	{
		assert(month > 0 && month < 13);
		int MonthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		//判断是否是闰年,4年一闰,百年不闰,四百年一闰
		if ((month == 2) && ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0))
			return 29;
		return MonthDays[month];
	}
	//打印日期
	void Print();
	//判断日期是合法
	bool CheckInvalid()
	{
		if (_year <= 0 || _month < 1 || _month > 12 || _day < 1 || _day > Getmonthday(_year, _month))
			return false;
		return true;
	}
	//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+(int day);
	Date& operator+=(int day);
	Date operator-(int day);
	Date& operator-=(int day);
	//前置++
	Date& operator++();
	//后置++
	Date operator++(int);
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);
	//日期 - 日期(日期-天数的函数重载形式)
	int operator-(const Date& d);
	//友元声明
	friend ostream& operator<<(ostream& out, const Date & d);
	friend istream& operator>>(istream& in, Date& d);
private:
	int _year;
	int _month;
	int _day;
};

4:上面三个函数后,接下来我们就得实现日期的等式比较了,分别是 >        >=        <        <=        ==        != 

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"

bool Date::operator>(const Date & d) const
{
	if (_year > d._year)
		return true;
	//年份相同,比较月份
	else if(_year == d._year)
	{
		if (_month > d._month)
			return true;
		//年份月份,均相同则比较天数
		else if(_month == d._month)
		{
			if (_day > d._day)
				return true;
		}
	}
	//其他情况均返回false;
	return false;
}

bool Date::operator>=(const Date& d)const
{
	////写法一
	//return this->operator>(d) || this->operator==(d);
	//写法二
	return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d)const
{
	//>=进行取反则是<
	return !this->operator>=(d);
}
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);
}

2:Date.cpp

2.1:代码1(构造函数)

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!CheckInvalid())
	{
		cout << "构造日期非法" << endl;
	}
}

void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

 2.2:代码2(比较函数)

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"

bool Date::operator>(const Date & d) const
{
	if (_year > d._year)
		return true;
	//年份相同,比较月份
	else if(_year == d._year)
	{
		if (_month > d._month)
			return true;
		//年份月份,均相同则比较天数
		else if(_month == d._month)
		{
			if (_day > d._day)
				return true;
		}
	}
	//其他情况均返回false;
	return false;
}

bool Date::operator>=(const Date& d)const
{
	////写法一
	//return this->operator>(d) || this->operator==(d);
	//写法二
	return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d)const
{
	//>=进行取反则是<
	return !this->operator>=(d);
}
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);
}

2.3:代码3(日期+天数)

代码语言:javascript
复制
Date  Date::operator+(int day)
{
	//拷贝构造:同类型一个存在的对象进行初始化另一个要创建的对象(因为要在不改变原日期的基础上进行操作)
	Date tmp = *this;
	tmp += day;
	return tmp;
}

Date& Date::operator+=(int day)
{
	//先增加天数
	_day += day;
	//判断是否大于本年本月的天数
	while(_day > Getmonthday(this->_year,this->_month))
	{
		_day -= Getmonthday(this->_year, this->_month);
		_month++;
		if(_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

2.4:代码4(日期 - 天数)

代码语言:javascript
复制
Date& Date::operator-=(int day)
{
	//先减天数
	_day -= day;
	//进行轮回,开始减月份
	while (_day < 0)
	{
		_month--;
		if(_month == 0)
		{
			_year--;
			_month = 12;
		}
		//每减了一次月份后,再获取当月的天数
		_day += Getmonthday(this->_year, this->_day);
	}
	return *this;
}

Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

2.5:代码5(前后置++与前后置--)

代码语言:javascript
复制
//前置++
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;
}

2.6:代码6(日期-日期)

代码语言:javascript
复制
//日期 - 日期(日期-天数的函数重载形式)
int Date::operator-(const Date & d)
{
	//找到小的那个日期
	int flag = 1;
	Date max = *this;
	Date min = d;

	if(*this < d)
	{
		max = d;
		min = *this;
	}
	

	//标记加的天数
	int days = 0;
	//然后小的日期不断++,直到与max相等
	while(min != max)
	{
		min++;
		days++;
	}
	return days * flag;
}

3:Test.cpp

3.1:代码1(测试构造函数)

代码语言:javascript
复制
#define  _CRT_SECURE_NO_WARNINGS
#include "Date.h"

int main()
{
	Date d1(2012, 3, 8);
	d1.Print();
	Date d2(2013, 13, 22);
	return 0;
}

3.2:代码2(测试比较函数)

代码语言:javascript
复制
#define  _CRT_SECURE_NO_WARNINGS
#include "Date.h"

int main()
{
	Date d1(2012, 3, 8);
	Date d2(2013, 11, 3);
	Date d3(2024, 9, 10);
	Date d4(2024, 9, 10);

	cout << (d1 < d2) << endl;
	cout << (d2 > d1) << endl;
	cout << endl;
	cout << (d3 >= d4) << endl;
	cout << (d3 == d4) << endl;
	cout << (d3 != d4) << endl;
	return 0;
}

3.3:代码3(测试日期+天数)

代码语言:javascript
复制
#define  _CRT_SECURE_NO_WARNINGS
#include "Date.h"

void TestAdd()
{
	Date d1(2022, 3, 8);
	d1.Print();
	Date d2(2023, 7, 6);
	d2 += 100;
	Date d3 = d1 + 15;
	d2.Print();
	d3.Print();
}

int main()
{
	TestAdd();
}

3.4:代码4(测试日期-天数)

代码语言:javascript
复制
#define  _CRT_SECURE_NO_WARNINGS
#include "Date.h"

void TestSub()
{
	Date d1(2022, 3, 8);
	Date d2(2023, 7, 6);
	d1 -= 7;
	d1.Print();
	Date d3 =  d2 - 16;
	d3.Print();
}

int main()
{
	//TestAdd();
	TestSub();
}

3.5:代码5(测试前后置++与前后置--)

代码语言:javascript
复制
#define  _CRT_SECURE_NO_WARNINGS
#include "Date.h"
void Test_Set_front_and_back()
{
	Date d1;
	Date d2(2023, 12, 29);
	//前置++与--是先自变再使用
	//后置++与--是先使用再自变
	d1 = d2++;
	d1.Print();
	d2.Print();
	cout << endl;
	Date d3 = ++d2;
	d3.Print();
	d2.Print();
	cout << endl;
	Date d4 = d2;
	d4.Print();
	d2.Print();
	cout << endl;
	Date d5 = d4--;
	d4.Print();
	d5.Print();
	cout << endl;
	Date d6 = --d4;
	d4.Print();
	d6.Print();
}

int main()
{
	Test_Set_front_and_back();
}

4:总代码

4.1:Date.h

代码语言:javascript
复制
#pragma once
#include <iostream> 
using namespace std;
#include <assert.h>
class Date
{
public:
	//构造函数
	Date(int year = 1, int month = 1, int day = 1);
	//获取某年某月的天数
	int Getmonthday(int year, int month)
	{
		assert(month > 0 && month < 13);
		int MonthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		//判断是否是闰年,4年一闰,百年不闰,四百年一闰
		if ((month == 2) && ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0))
			return 29;
		return MonthDays[month];
	}
	//打印日期
	void Print();
	//判断日期是合法
	bool CheckInvalid()
	{
		if (_year <= 0 || _month < 1 || _month > 12 || _day < 1 || _day > Getmonthday(_year, _month))
			return false;
		return true;
	}
	//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+(int day);

	Date& operator+=(int day);

	Date operator-(int day);
	Date& operator-=(int day);
	//前置++
	Date& operator++();
	//后置++
	Date operator++(int);
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);

	//日期 - 日期(日期-天数的函数重载形式)
	int operator-(const Date& d);
	//友元声明
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
private:
	int _year;
	int _month;
	int _day;
};

4.2:Date.cpp

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!CheckInvalid())
	{
		cout << "构造日期非法" << endl;
	}
}

void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

bool Date::operator>(const Date& d) const
{
	if (_year > d._year)
		return true;
	//年份相同,比较月份
	else if (_year == d._year)
	{
		if (_month > d._month)
			return true;
		//年份月份,均相同则比较天数
		else if (_month == d._month)
		{
			if (_day > d._day)
				return true;
		}
	}
	//其他情况均返回false;
	return false;
}

bool Date::operator>=(const Date& d)const
{
	////写法一
	//return this->operator>(d) || this->operator==(d);
	//写法二
	return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d)const
{
	//>=进行取反则是<
	return !this->operator>=(d);
}
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);
}

Date& Date::operator+=(int day)
{
	//先增加天数
	_day += day;
	//判断是否大于本年本月的天数
	while (_day > Getmonthday(this->_year, this->_month))
	{
		_day -= Getmonthday(this->_year, this->_month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Date  Date::operator+(int day)
{
	//拷贝构造:同类型一个存在的对象进行初始化另一个要创建的对象
	Date tmp = *this;
	tmp += day;
	return tmp;
}


Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

Date& Date::operator-=(int day)
{
	//先减天数
	_day -= day;
	//进行轮回,开始减月份
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		//每减了一次月份后,再获取当月的天数
		_day += Getmonthday(this->_year, this->_month);
	}
	return *this;
}



//前置++
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)
{
	//找到小的那个日期
	int flag = 1;
	Date max = *this;
	Date min = d;

	if (*this < d)
	{
		max = d;
		min = *this;
	}


	//标记加的天数
	int days = 0;
	//然后小的日期不断++,直到与max相等
	while (min != max)
	{
		min++;
		days++;
	}
	return days * flag;
}

4.3:Test.cpp

代码语言:javascript
复制
#define  _CRT_SECURE_NO_WARNINGS
#include "Date.h"

void TestAdd()
{
	Date d1(2022, 3, 8);
	d1.Print();
	Date d2(2023, 7, 6);
	d2 += 100;
	Date d3 = d1 + 15;
	d2.Print();
	d3.Print();
}

void TestSub()
{
	Date d1(2022, 3, 8);
	Date d2(2023, 7, 6);
	d1 -= 7;
	d1.Print();
	Date d3 =  d2 - 16;
	d3.Print();
}

void Test_Set_front_and_back()
{
	Date d1;
	Date d2(2023, 12, 29);
	//前置++与--是先自变再使用
	//后置++与--是先使用再自变
	d1 = d2++;
	d1.Print();
	d2.Print();
	cout << endl;
	Date d3 = ++d2;
	d3.Print();
	d2.Print();
	cout << endl;
	Date d4 = d2;
	d4.Print();
	d2.Print();
	cout << endl;
	Date d5 = d4--;
	d4.Print();
	d5.Print();
	cout << endl;
	Date d6 = --d4;
	d4.Print();
	d6.Print();
}

void TestDateSubDate()
{
	Date d1(2022, 3, 8);
	Date d2(2022, 4, 7);
	Date d3(2024, 3, 8);
	cout << d2 - d1 << endl;
	cout << d3 - d1 << endl;
}
int main()
{
	//TestAdd();
	//TestSub();
	//Test_Set_front_and_back();
	TestDateSubDate();
}

好啦,家人们,关于日期类实现这块的相关细节知识,博主就讲到这里了,如果uu们觉得博主讲的不错的话,请动动你们滴滴的小手给博主点个赞,你们滴鼓励将成为博主源源不断滴动力.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-01-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1:Date.h
  • 2:Date.cpp
    • 2.1:代码1(构造函数)
    •  2.2:代码2(比较函数)
    • 2.3:代码3(日期+天数)
    • 2.4:代码4(日期 - 天数)
    • 2.5:代码5(前后置++与前后置--)
    • 2.6:代码6(日期-日期)
  • 3:Test.cpp
    • 3.1:代码1(测试构造函数)
    • 3.2:代码2(测试比较函数)
    • 3.3:代码3(测试日期+天数)
    • 3.4:代码4(测试日期-天数)
    • 3.5:代码5(测试前后置++与前后置--)
  • 4:总代码
    • 4.1:Date.h
    • 4.2:Date.cpp
    • 4.3:Test.cpp
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档