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();
 }
相关推荐
恋猫de小郭1 小时前
Android R8 为什么可以让 Kotlin 协程提速 2 倍?
android·前端·flutter
han_hanker1 小时前
SQL语法 , BETWEEN ... AND ...,比较运算符
前端·javascript·sql
PedroQue991 小时前
Vite插件v1.1.0发布:自动导入全面重构
前端·vite
FogLetter1 小时前
三栏布局的七十二变:从Flex到Grid,我学会了响应式设计的"降龙十八掌"
前端·面试
答案answer1 小时前
做了几年 Three.js,我为什么决定自己开发一款3D低代码编辑器
前端·three.js
Revolution611 小时前
首屏变慢后,应该先查资源下载、脚本执行还是接口请求
前端·性能优化·前端工程化
大爱编程♡1 小时前
C++基础-类和对象
c++·算法
shawn03261 小时前
《疯狂动物城 2》背后的 Hybrid BVH:迪士尼如何让复杂场景跑进交互式 GPU 光追
前端·计算机图形学
圣光SG2 小时前
web操作题练习(五)
前端