日期类(Date)

本文详细介绍了如何设计并实现一个日期类,包括类的构建、成员函数定义、运算符重载等内容。通过具体实例展示了该类的功能,如日期加减、比较等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

日期类的实现在此分三个步骤实现:

一、构建类

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

public:
	Date(int year = 2017, int month = 9, int day = 23);

	~Date();

	Date& operator=(const Date& d);

	Date& operator++();//前置++
	Date operator++(int);//后置++
	Date& operator--();//前置--
	Date operator--(int);//后置--
	Date operator+(int days);//days天之后的日期
	Date operator-(int days);//days天之前的日期
	int operator-(const Date& d);//计算量日期之间的距离
	bool operator==(const Date& d);
	bool operator!=(const Date& d);
	bool operator>(const Date& d);
	bool operator<(const Date& d);
	int Is_runnian(int year);//判断闰年
	int Days(int year, int month);//求月的天数
	bool Is_hefadate(int year, int month, int day);//日期是否合法

private:
	int _year;
	int _month;
	int _day;
};

二、在类外定义构造函数,析构函数,友元函数

ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "/" << d._month << "/" << d._day << endl;

	return _cout;
}

//构造函数
Date::Date(int year, int month, int day)
:_year(year)
, _month(month)
, _day(day)
{
}

//析构函数
Date::~Date()
{
}

三、运算符的重载及相关操作

//赋值运算符重载
Date& Date::operator=(const Date& d)
{
	if (this == &d)
	{
		return *this;
	}
	else
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

//前置++
Date& Date::operator++()
{
	_day += 1;

	return *this;
}

//后置++
Date Date::operator++(int)
{
	Date temp = *this;

	_day += 1;

	return temp;
}

//前置--
Date& Date::operator--()
{
	_day -= 1;

	return *this;
}

//后置--
Date Date::operator--(int)
{
	Date temp = *this;

	_day -= 1;

	return temp;
}

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

bool Date::operator!=(const Date& d)
{
	if ((_year != d._year) || (_month != d._month) || (_day != d._day))
		return true;
	else
		return false;
}

bool Date::operator>(const Date& d)
{
	if ((_year > d._year) || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day))
		return true;
	else
		return false;
}

bool Date::operator<(const Date& d)
{
	if ((_year < d._year) || (_year == d._year && _month < d._month) || (_year == d._year && _month == d._month && _day < d._day))
		return true;
	else
		return false;
}

int Date::Is_runnian(int year)
{
	if ((year % 4 && year % 100) || (year % 400))
	{
		return 1;
	}
	return -1;
}

int Date::Days(int year, int month)
{
	int day;
	int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	day = days[month - 1];
	if (month == 2)
	{
		day += Is_runnian(year);
	}
	return day;
}

bool Date::Is_hefadate(int year,int month,int day)
{
	if ((year < 1) || (month < 1) || (month > 13) || (day < 1) || (day>Days(year, month)))
		return true;
	else
		return false;
}

//days天之后的日期
Date Date::operator+(int days)
{
	Date temp = *this;

	if (days < 0)
	{
		int day = -days;
		return temp - day;
	}
	else
	{
		temp._day += days;
		while (temp._day>Days(temp._day, temp._month))
		{
			temp._day -= Days(temp._day, temp._month);
			if (temp._month == 12)
			{
				temp._year++;
				temp._month = 1;
			}
			else
			{
				temp._month++;
			}
		}
		return temp;
	}
}

//days天之前的日期
Date Date::operator-(int days)
{
	Date temp = *this;

	if (days < 0)
	{
		int day = -days;
		return temp + day;
	}
	else
	{
		temp._day -= days;
		while (temp._day <= 0)
		{
			temp._day = temp._day + Days(temp._day, temp._month);
			if (temp._month== 1)
			{
				temp._year--;
				temp._month = 12;
			}
			else
			{
				temp._month--;
			}
		}
		return temp;
	}
}

//计算量日期之间的距离
int Date::operator-(const Date& d)
{
	int count = 0;
	Date max=*this;
	Date min=d;
	 if (max < min)
	{
	     max = d;
	     min = *this;
	}
     while ((min + count) < max)
    {
	   count++;
    }
   return count;
}

到这里,日期类的构建就结束了,不妨,我们来测试一下吧。

void test()
{
	Date d1(2017, 8, 25),d2(2017,7,5),d3;
	int count = 0;

	d1++;
	cout << d1;
	++d1;
	cout << d1;
	d1--;
	cout << d1;
	--d1;
	cout << d1;
	d3=d1 + 5;
	cout << d3;
	d3=d2 - 5;
	cout << d3;

	count=d1 - d2;
	cout << count<<endl;
}

int main()
{
	test();

	system("pause");
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值