c++实现一个日期类

本文档详细介绍了如何使用C++定义一个日期类,包括全缺省构造函数、获取月份天数的内联函数以及各种运算符(如+、-、==、!=、++、--等)的重载实现。此外,还涵盖了日期加减天数的逻辑处理和日期合法性检查。
摘要由CSDN通过智能技术生成

定义一个日期类

🔰要实现一个简单的日期类,那么我们将需要实现的功能定义在类里面,成员变量可以定义为私有来管理,目的是防止别的程序对其随意改动。接下来我们实现一个常见的日期类功能,熟悉运算符重载的使用。

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

using std::cout;
using std::cin;
using std::endl;

class Date
{
public:
	//全缺省的构造函数
	Date(int year = 0, int month = 1, int day = 1);

	void Print()const;
	析构、拷贝构造、赋值重载可以不用写,默认生成的就已经够用了

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

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

	//++d -> d.operator++(&d)
	Date& operator++();//前置++
	//++d -> d.operator++(&d,0)
	Date operator++(int);//int参数不需要给实参,因为没有用,它的作用是为了跟前置++构成函数重载

	//--d -> d.operator--(&d)
	Date& operator--();
	//d-- -> d.operator--(&d,0)
	Date operator--(int);

	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);
private:
	int _year;
	int _month;
	int _day;
};

获取某一月份的天数

//此函数将可能在后续频繁调用,所以我们将会被频繁调用的小函数定义为内联函数,减小程序运行时栈帧的开销
inline int GetMonthDay(int year, int month)
{
	//数组储存平年每个月的天数,将数组定义为static静态成员函数储存在静态区,避免每次我们调用此函数都需要生成一个新的数组
	static int dayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

	//获取平年每个月的天数
	int days = dayArray[month];

	//判断接收的日期是否为闰年的2月,如果是,就处理一下此日期
	if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		days = 29;
	}

	//返回对应月份的天数
	return days;
}

全缺省的构造函数

🚀我们需要自己实现一个构造函数对日期进行初始化并检查日期的合法性,保证传入的日期合法。如果输入的日期非法,那么将提示一下。

//在类定义时,我们将这个函数定义为一个全缺省的函数,但是在定义和声明时不能同时都给
//初始值,所以我们在函数声明时给了全缺省,定义时就必须把初始值去掉
Date::Date(int year, int month, int day)
{
	//检查日期的合法性
	if (year >= 0 && month > 0 && month < 13 && day>0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		//严格来说,这里使用抛异常更好
		cout << "illegal date" << ": ";
		cout << year << "年" << month << "月" << day << "日" << endl;
	}
}

日期+=天数

//使用+=,那么当这个函数调用结束时,+=之后的变量属于类,不会销毁,所以使用传引用返回
Date& Date::operator+=(int day)
{
	//如果用户传的day小于0,相当于日期往前走。那么就可以复用-=函数
	if (day < 0)
	{
		*this -= -day;
	}
	else
	{
		//先将天数直接加day上
		_day += day;

		//天数不合法,不断进位,让日期(天数)合法
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;

			if (_month > 12)
			{
				++_year;
				_month = 1;
			}
		}
	}
	return *this;
}

日期+天数

🚀实现日期+天数的时候,我们可以复用之前写的+=代码,这样不仅能够简化代码,也可以少一些判断。例如:当用户传入一个负数day时,就不需要在进行特殊处理,因为在+=函数中已经处理过了,只管调用就可以了。

Date Date::operator+(int day)const
{
	//若传入的day是负数,那么在复用operator+=的时候已经处理过了

	Date ret(*this);
	//复用operator+=
	ret += day;
	return ret;
}

日期-=天数

🔳实现思想与+=相似。

Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		//复用operator+=
		*this += -day;
	}
	else
	{
		//先在_day中减去需要向前减的天数
		_day -= day;

		//当天数为负数时,说明天数不合法,那么将对其进行调整
		while (_day <= 0)
		{
			--_month;
			if (_month == 0)
			{
				--_year;
				_month = 12;
			}
			_day += GetMonthDay(_year, _month);
		}
	}
	return *this;
}

日期 - 天数

🔹复用operator-=函数。

Date Date::operator-(int day)const
{
	Date ret(*this);
	ret -= day;//ret.operator-=(&ret,day)
	return ret;
}

++day

//前置++
Date& Date::operator++()
{
	*this += 1;//先+=1
	return *this;//返回++之后的
}

day++

//后置++
Date Date::operator++(int)
{
	Date tmp(*this);//拷贝一份临时数据
	*this += 1;
	return tmp;//返回++之前的数据
}

- - day

//前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;//返回--之后的
}

day- -

//后置--
Date Date::operator--(int)
{
	Date tmp(*this);//拷贝一份临时数据
	*this -= 1;
	return tmp;//返回--之前的
}

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

✅这里我们只需要实现(== 、>)或者(<、==)就可以了,其它的运算符重载都可以复用这两个运算符重载。

//d1>d2 -> d1.operator(&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;
}

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

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

日期 - 日期

//int operator-(Date*this ,const Date& d)
int Date::operator-(const Date& d) const
{
	//默认传入的第一个参数大于第二个参数
	Date max = *this;
	Date min = d;
	int flag = 1;

	//若第一个参数比第二个参数小,那么调整一下
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;//第一个参数小,那么相减天数为负。最终结果由flag控制
	}
	int n = 0;//定义它们之间相差的天数
	while (min != max)
	{
		++min;
		++n;
	}
	return flag * n;//返回最终结果
}

一个简单的日期类已经实现完毕。如果错误或者建议,望各位大佬指点一下!😜

评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风&57

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值