1. 类设计 (date.h)
- 构造函数:支持默认参数(2001年1月1日)和自定义日期初始化
- 日期验证 :
CheckDate() 方法验证日期合法性
- 月份天数计算 :
GetMonthDay() 静态方法,考虑闰年规则
- 运算符重载 :
- 比较运算符:
<, >, <=, >=, ==, !=
- 算术运算符:
+, -, +=, -=
- 自增运算符:前置
++ 和后置 ++
- 日期差计算:
operator-(计算两个日期之间的天数差)
2. 实现细节 (date.cpp)
- 日期合法性检查:验证月份范围(1-12)和日期是否超出当月天数
- 比较运算符实现:按年、月、日逐级比较
- 日期加减运算 :
- 支持正负天数加减
- 自动处理跨月、跨年边界
- 使用
GetMonthDay() 处理不同月份天数
- 日期差计算:通过循环递增较小日期直到相等,计算天数差
c++日期类的实现
date.h(所有函数的声明)
#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
#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
#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();
}