首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Date日期类的实现

Date日期类的实现

作者头像
用户11719958
发布2025-12-30 14:14:24
发布2025-12-30 14:14:24
1690
举报

前言:

实现日期类,主要是实现一些功能。判断日期的大小,是否相等。当然重点是在实现日期的加减等功能。主要用到的知识是运算符重载

1,类的定义

首先定义Date类,包含三个成员变量,以及几个默认成员函数

代码语言:javascript
复制
class Date
{
public:
	Date(int year=1,int month=1,int day=1)//实现的默认构造函数,全缺省
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)//拷贝构造
	{
		_year = d._year;
		_month = d._month;
		_day = d. _day;
	}
	Date& operator=(Date& d)//赋值运算符重载
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;

		return *this;
	}
    void print()
	{
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}
	//这里析构函数可以不写,因为我们没有要释放什么资源
private:
	int _year;
	int _month;
	int _day;
};

2,功能的实现

注:实现的时候声明和定义是分开的,所以前面需加上访问限定符。

2.1,判断两个日期是否相等

这里对"=="运算符进行重载即可,而且是重载在类内,在传参时第一个默认是隐含的this指针,所以只需写一个参数,为了防止参数被修改,在前面加上const修饰。

代码语言:javascript
复制
bool Date::operator==(const Date& d)
{
	return _year == d._year && _month == d._month && _day == d._day;
}
2.2,判断两个日期大小
代码语言:javascript
复制
bool Date::operator<(const Date& d)
{
	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;
			}
		}
	}
	return false;
}
2.3,更多的运算符重载

通过上面相等和小于的比较,我们就可以退出更多的

代码语言:javascript
复制
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 _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

这些都是对写过的函数的逻辑的复用。 

2.4,前置++和后置++重载

//c++规定 Date d1; //d1++ Date& operator++(int)//后置加加 { } //++d1 Date& operator++()//前置加加 { }

前置++

代码语言:javascript
复制
Date& Date::operator++()
{
	*this += 1;//直接调用+=重载
	return *this;
}

 后置++

代码语言:javascript
复制
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}
2.5,日期+=天数

从上图看,在进行运算的时候,整体思路是:先对天数加,在比较天数与对应月份的天数 ,如果小于就结束,如果大于就减去该月份的天数,同时月份加1,如果月份加到13,那么年加1,月份置为1。 因此我们需要获得指定月份的天数,我们可以将他封装为成员函数。

获取指定年月的天数:

代码语言:javascript
复制
int GetMonthDay(int year, int month)
	{
		//因为这里用到的地方多,直接将数组定义为静态变量
		static int MonthDay[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 MonthDay[month];
	}

代码实现:

代码语言:javascript
复制
Date& Date::operator+=(int day)
{
	_day += day;
	//超出天数
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
2.6,日期+天数

d1+100,d1不变;d1+=100,d1会改变

这里的逻辑和上面的逻辑相似,只是要求d1不变

代码语言:javascript
复制
//这里不能用引用返回,因为tmp为临时对象,函数结束后就销毁了
Date Date::operator+(int day) const//加上const防止this内容被修改
{
	Date tmp = *this;
	tmp += day;

	return tmp;
}

我们直接创建临时对象tmp,在调用+=重载,最后返回tmp,就可以实现

2.7,日期-=天数

思路:天数先减,如果小于0,节上个月的天数,在加上,月份减一,如果月份为0,年减一,月份置为12。

代码语言:javascript
复制
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day < 0)
	{
		_day += GetMonthDay(_year, _month);
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
	}
	return *this;
}
2.8,日期-天数

同理,原理相似

代码语言:javascript
复制
Date Date::operator-(int day)const
{
	Date tmp = *this;
	tmp -= day;

	return tmp;
}
2.9日期-日期,计算相差的天数 

思路:小的日期一直++,直到与大的日期相等,就可以孙处相差的天数

代码语言:javascript
复制
int Date::operator-(const Date& d) const
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;//调用赋值运算符重载
		min = *this;//调用赋值运算符重载
		flag = -1;
	}

	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}

	return n * flag;//返回有正负之分
}
2.10,日期的流插入和流提取运算符重载

我们想要实现输入的两个日期进行运算,这里就需要对<<>>进行运算符重载。

流插入<<的运算符重载,我们知道c++中cout可以实现对任意类型的打印,这是因为编译器对他进行了运算符重载。

由上图可知,cout是ostream类类型。

注意:在实现时 ,我们需要将函数写为全局函数,因为如果写为成员函数,第一个参数默认是隐含的this指针,会导致参数不匹配。但是定义为全局函数,我们就不能访问私有成员了,我们可以再将函数声明为友元函数,就可以访问了。

代码语言:javascript
复制
//用out接收cout,cout<<d1;调用该函数
ostream& operator<<(ostream& out,Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;//给返回是为了支持cout<<d1<<d2;这种情况
}

流提取>>的重载,与cout不同的是,cin是istream类型的对象。

代码语言:javascript
复制
istream& operator<<(istream& in, Date& d)
{
	cout << "请输入年月日:";
	in >> d._year >> d._month >> d._day;
	return in;//同样,为了支持cin>>d1>>d2;
}

3,代码总体

Date.h文件

代码语言:javascript
复制
#include <iostream>
#include <assert.h>
using namespace std;

class Date
{
    //声明为友元函数
	friend ostream& operator<<(ostream& cout, Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	int GetMonthDay(int year, int month);
	Date(int year=1,int month=1,int day=1)//实现的默认构造函数,全缺省
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)//拷贝构造
	{
		_year = d._year;
		_month = d._month;
		_day = d. _day;
	}
	void print()
	{
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}
	//这里析构函数可以不写,因为我们没有要释放什么资源
	
	Date operator+(int day)const;
	Date& operator+=(int day);
	Date& operator-=(int day);
	Date operator-(int day)const;
	bool 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;
	bool operator!=(const Date& d) const;
	int operator-(const Date& d)const;
	Date& operator++();
	Date operator++(int);
	
	
private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& out, Date& d);
istream& operator>>(istream& in, Date& d);

Date.cpp文件

代码语言:javascript
复制
#include"Date.h"
int Date::GetMonthDay(int year, int month)
{
	assert(month > 0 && month < 13);
	//因为这里用到的地方多,直接将数组定义为静态变量
	static int MonthDay[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 MonthDay[month];
}

//d1==d2
bool Date::operator==(const Date& d)
{
	return _year == d._year && _month == d._month && _day == d._day;
}

//d1<d2
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;
			}
		}
	}
	return false;
}

//d1+100
//这里不能用引用返回,因为tmp为临时对象,函数结束后就销毁了
Date Date::operator+(int day) const
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}
//d1+=100
Date& Date::operator+=(int day)
{
	_day += day;
	//超出天数
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
//d1-=100
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day < 0)
	{
		_day += GetMonthDay(_year, _month);
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
	}
	return *this;
}
//d1-100
Date Date::operator-(int day)const
{
	Date tmp = *this;
	tmp -= day;

	return tmp;
}
// d1 <= d2
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
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

// d1 - d2
int Date::operator-(const Date& d) const
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;//调用赋值运算符重载
		min = *this;//调用赋值运算符重载
		flag = -1;
	}

	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}

	return n * flag;//返回有正负之分
}
//++d1
Date& Date::operator++()
{
	*this += 1;//直接调用+=重载
	return *this;
}
//d1++
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}
//用out接收cout,cout<<d1;调用该函数
ostream& operator<<(ostream& out,Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;//给返回是为了支持cout<<d1<<d2;这种情况
}
//cin>>d1
istream& operator>>(istream& in, Date& d)
{
	cout << "请输入年月日:";
	in >> d._year >> d._month >> d._day;
	return in;//同样,为了支持cin>>d1>>d2;
}

4,功能展示

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1,类的定义
  • 2,功能的实现
    • 2.1,判断两个日期是否相等
    • 2.2,判断两个日期大小
    • 2.3,更多的运算符重载
    • 2.4,前置++和后置++重载
    • 2.5,日期+=天数
    • 2.6,日期+天数
    • 2.7,日期-=天数
    • 2.8,日期-天数
    • 2.9日期-日期,计算相差的天数 
    • 2.10,日期的流插入和流提取运算符重载
  • 3,代码总体
  • 4,功能展示
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档