c++(日期类的实现)

1. 类设计 (date.h)

  • 构造函数:支持默认参数(2001年1月1日)和自定义日期初始化
  • 日期验证 :CheckDate() 方法验证日期合法性
  • 月份天数计算 :GetMonthDay() 静态方法,考虑闰年规则
  • 运算符重载 :
    • 比较运算符:<, >, <=, >=, ==, !=
    • 算术运算符:+, -, +=, -=
    • 自增运算符:前置 ++ 和后置 ++
    • 日期差计算:operator-(计算两个日期之间的天数差)

2. 实现细节 (date.cpp)

  • 日期合法性检查:验证月份范围(1-12)和日期是否超出当月天数
  • 比较运算符实现:按年、月、日逐级比较
  • 日期加减运算 :
    • 支持正负天数加减
    • 自动处理跨月、跨年边界
    • 使用 GetMonthDay() 处理不同月份天数
  • 日期差计算:通过循环递增较小日期直到相等,计算天数差

c++日期类的实现

date.h(所有函数的声明)

cpp 复制代码
#pragma once
#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<assert.h>
using namespace std;
class date
{
public:
	date(int year = 2001, int month = 1, int day = 1);
	void print()const;//加入const不会改变值
	bool CheckDate()const;//日期检查
	int GetMonthDay(int year, int month) const//得到天数
	{
		assert(month > 0 && month < 13);//assert检查月份是否合法

		static int monthDayArray[13] = { -1, 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 monthDayArray[month];
	}
	bool operator<(const date& d)const;//bool类型,传引用返回
	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)const;
	date operator-(int day)const;
	int operator-(const date&d)const;//两个日期相减
	date& operator++();//前置加加
	date operator++(int);//后置加加
	date operator--(int day);

	
private:
	int _year;
	int _month;
	int _day;

};

date.cpp

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 
#include"date.h"
#include<iostream>
bool date::CheckDate()const
{
	if (_month > 12 || _month < 1 || _day<1 || _day> GetMonthDay(_year, _month))
	{
		return false;
	}
	return true;
}
void date::print()const
{
	cout << _year << "/" << _month << "/" << _day << endl;
}
date::date(int year, int month,int day) //构造函数
{
	_year = year;
	_month = month;
	_day = day;
}
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 (_day < d._day)
		{
			return _day < d._day;
		}
	}
		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);

}
bool date::operator==(const date& d)const
{
	if (_year == d._year && _month == d._month && _day == d._day)
	{
		return true;
	}
	return false;
}
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> GetMonthDay(_year,_month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month==13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}

date date::operator+(int day)const
{
	date tmp;
	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(_year, _month);
	}

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


date date::operator++(int)//后置加加
{
	date tmp = *this;
	*this +=1;
	return tmp;
}

date& date::operator++()//前置加加
{
	*this +=1;
	return *this;

}
int date::operator-(const date& d)const
{
	date max = *this;
	date min = d;
	int flag = 0;
	if (*this<d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min!=max)
	{
		min++;
		n++;
	}
	return n;
}

test.cpp

cpp 复制代码
#include"date.h"
#include<iostream>
using namespace std;


void test1()//不要传参,默认含有this指针
{
	date d1(2026,7,24);//构造函数
	date d2 = d1 + 100;
	d1.print();
	d2.print();
}

void test2()//不要传参,默认含有this指针
{
	date d3(2026, 7, 24);//构造函数
	date d4 = d3-100;//报错,有歧义
	d3.print();
	d4.print();
}

void test3()//不要传参,默认含有this指针
{
	date d1(2026, 7, 24);//构造函数
	date d2(2025,7,24);
	cout << d1 - d2 << endl;
}

int main()
{
	test1();
	test2();
	test3();
 }
相关推荐
圆心角6 小时前
代码也有折旧率
前端·程序员
CoderYanger6 小时前
Java EE 进阶:14.案例综合练习-博客系统(第二弹):博客详情+令牌技术
服务器·前端·spring boot·后端·spring·java-ee·github
kyriewen10 小时前
我测了 5 种关页上报,两种老写法一条都没发出去
前端·javascript·程序员
郝学胜-神的一滴10 小时前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
旖旎夜光11 小时前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
不一样的少年_12 小时前
受够了当人肉传话筒:我把这 9 个 Agent 塞进了同一个聊天框
前端·agent·ai编程
狗头大军之江苏分军13 小时前
《潮水漫过十七岁》 加一个蛋,顺便说一下我解放了
前端·后端
Captaincc13 小时前
掘金AI用量迎来重大更新!全新AI用量
前端·后端
天若有情67314 小时前
开源|纯前端·条码&二维码在线生成器,支持拖拽缩放、复制生成码图片
前端·javascript·开源·html·二维码·条形码
青少儿编程课堂15 小时前
蓝桥杯真题解析:手链配色计数与波利亚定理(全国青少年信息素养大赛与电子学会软件编程考级)
c++·python·算法·bfs·信息学竞赛