【C++】日期类的实现

在这里插入图片描述

个人主页
创作不易,感谢大家的关注!

在这里插入图片描述
在这里插入图片描述

⭐一、前言

在实现日期类之前,我们首先要知道实现哪些内容才会去编写代码。因此我们一般会采用声明和定义分离的方式来实现。将声明统一放入Date.h的文件中,成员函数的定义统一放入Date.cpp的文件中。

🎄二、检查日期是否合法

我们需要检查月份不能小于1或者大于12,天数要根据年和月进行判断。

//检查日期是否有误
//由于声明和定义分开,因此需要指定一下命名空间
bool Date::CheckDate()
{
	if (_month < 1 || _month>12
		|| _day<1 || _day>GetMonthDay(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}

🎈三、获取某一年某一月份的天数

为了方便获取某年某月的的天数,我们会设定一个数组其大小为13以便数组下标能和月份相对应,而由于日期加减天数的运算符重载函数调用次数频繁且代码量少,因此我们可以定义到类的内部。
注:定义到类内部的成员默认都加了内联inline,而分离声明和定义是无法内联的。
在写代码之前,我们还需判断一下二月是否为闰年,从而影响天数的改变。

int GetMonthDay(int year, int month)
{
	assert(month > 0 && month <= 13);
	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;
	}
	else
	{
		return MonthDayArray[month];
	}
}

🏝️四、算数运算符重载

1.日期 += 天数

当我们将日期加完天数时,我们需要考虑月份和年份是否需要进位。因此我们需要实现一个循环,再循环内部不断进行进月或者进年,让天数不断减去本月的天数,直到小于当前月份天数时,循环结束。

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.日期 + 天数

由于日期+天数的规则是不能改变当前日期的值,因此在成员函数中定义了一个tmp进行拷贝,最后传值返回。这是因为tmp作为临时变量,出了作用域就会进行销毁。

//用const进行修饰防止左操作数被修改
Date Date::operator-(int day) const
{
	//拷贝构造函数
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

由于+=天数的代码和+天数的代码类似,因此这里就使用了复用操作,提升代码的效率。

3.日期 -= 天数

该功能和上述日期+=天数的功能实现逻辑类似。不过需要不断向月和年去借天数。

Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_month = 12;
			--_year;
		}
		else
		{
			_day += GetMonthDay(_year, _month);
		}
	}
	return *this;
}

4.日期 - 天数

这里仍然使用复用的操作。

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

5.日期 - 日期

由于我们不知道哪个对象的日期比较大,因此我们可以利用一个假设的思想,如果判断错误就纠正一下即可。

定义一个计数器,让较小的日期自增,直到和较大的日期相等时停止,他们之间的天数就是日期相差的天数,而天数有可能为正也可能为负,因此我们定义一个flag,默认左值为较大值,右值为较小值,若情况相反就将两者进行调换,并将flag置为-1。

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

🏠五、递增递减运算符重载

1.前置++和后置++

我们都知道前置和后置是有区别的,一个是先使用后++;而另一个则是先++后使用。但在写运算符重载时,我们并不能将两者很好的分开。因此C++规定:后置++的运算符重载需要加上一个int类型的参数,且不必写形参名。因此我们就可以很好的用代码进行实现。

//d1++
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

//++d1
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

2.前置–和后置–

逻辑思路和上述类似,这里就不过多进行赘述了,直接上代码。

//d1--
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

//--d1
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

🚆六、日期类的大小比较

==、>、>=、<、<=、!= 运算符重载

比较运算符的重载比较简单,我们只需实现一半的运算符重载即可,剩下的运算符利用取反逻辑操作符就可以轻松实现。

bool Date::operator==(const Date& d)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
bool Date::operator>(const Date& d)
{
	return !(*this <= d);
}

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

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;
		}
	}
	else
	{
		return false;
	}
}

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

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

💎七、流插入和流提取重载

流插入和流提取不适用于在类内部进行实现,因为我们无法改变调用类内部定义的成员函数时,第一个传过去的永远是this指针。因此我们需要将流插入和流提取的函数重载在类的外部。

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日";
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请依次输入年月日:>";
		in >> d._year >> d._month >> d._day;
		if (!d.CheckDate())
		{
			cout << "输入日期非法:";
			d.Print();
			cout << "请重新输入!!!" << endl;
		}
		else
		{
			break;
		}
	}
	return in;
}

这里的返回一个对象引用类型是为了贴合内置类型使用规则。

⏱️八、友元函数

友元函数,是指某些虽然不是类成员却能够访问类的所有成员的函数。 因此我们就可以在类外直接访问私有成员了。 这种访问权限的赋予是通过在类定义中使用friend这个关键字来进行实现的,只需要在类的内部添加上类外定义的函数的声明,并在声明前加上关键字friend即可。

class Date
{
	//友元函数声明
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	// ...
	
private:
	// ....
};

🚀九、总结

1.Date.h

#pragma once

#include<iostream>
#include<assert.h>

using namespace std;

class Date
{
	//友元函数声明
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

public:
	bool CheckDate();
	Date(int year = 1000, int month = 1, int day = 1);
	void Print();
	//定义在类里面的函数默认是inline
	int GetMonthDay(int year, int month)
	{
		assert(month > 0 && month <= 13);
		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;
		}
		else
		{
			return MonthDayArray[month];
		}
	}
	bool operator<(const Date& d) const;
	bool operator<=(const Date& d);
	bool operator>(const Date& d);
	bool operator>=(const Date& d);
	bool operator==(const Date& d);
	bool operator!=(const Date& d);

	Date operator+(int day);
	Date& operator+=(int day);
	Date operator-(int day) ;
	Date& operator-=(int day);

	//d1++
	Date operator++(int);
	//++d1
	Date& operator++();
	//d1--
	Date operator--(int);
	//--d1
	Date& operator--();

	//d1 - d2
	int operator-(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

2.Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include"Date.h"

//检查日期是否有误
bool Date::CheckDate()
{
	if (_month < 1 || _month>12
		|| _day<1 || _day>GetMonthDay(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}

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;
		}
	}
	else
	{
		return false;
	}
}

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

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

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

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

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

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

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;
}

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)
		{
			_month = 12;
			--_year;
		}
		else
		{
			_day += GetMonthDay(_year, _month);
		}
	}
	return *this;
}

//d1++
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

//++d1
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

//d1--
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

//--d1
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

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

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日";
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请依次输入年月日:>";
		in >> d._year >> d._month >> d._day;
		if (!d.CheckDate())
		{
			cout << "输入日期非法:";
			d.Print();
			cout << "请重新输入!!!" << endl;
		}
		else
		{
			break;
		}
	}
	return in;
}

3.Test.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include "Date.h"

//测试+ - +=
void Test1()
{
	Date d1(2024, 7, 14);
	Date d2 = d1 + 300;
	d1.Print();
	d2.Print();
	Date d3(2024, 7, 14);
	Date d4 = d3 - 2000;
	d3.Print();
	d4.Print();
	Date d5(2024, 7, 14);
	d5 += 3000;
	d5.Print();
}

// 测试 d1++,++d1,d1--,--d1
void Test2()
{
	Date d1(2024, 7, 14);
	Date d2 = ++d1;
	d1.Print();
	d2.Print();

	Date d3 = d1++;
	d1.Print();
	d3.Print();

	Date d4 = --d1;
	d1.Print();
	d4.Print();

	Date d5 = d1--;
	d1.Print();
	d5.Print();
}

//测试两个日期相减 d1-d2
void Test3()
{
	Date d1(2024, 7, 14);
	Date d2(2024, 7, 5);
	int n = d1 - d2;
	cout << n << endl;
	n = d2 - d1;
}

int main()
{
	//Test1();
	//Test2();
	Test3();
	return 0;
}
  • 69
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 91
    评论
评论 91
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值