C++日期类

在编写日期类时,需要注意以下几点:
1.类的定义,实例化的方法
2.使用构造函数,析构函数
3.类成员的访问控制的应用
4.C++中运算符的重载
5.面向对象的的程序设计方法
日期类的主要内容为:
实现加若干天,减若干天的操作,需要设计构造函数和析构函数。编写一个main函数
实现对日期类的测试,先使用特定的日期对类对象进行初始化,然后对该对象进行测试,
测试中有闰年平年二月份的天数,以及每个月的天数是否合法等边缘数据。

#include<iostream>
#include<assert.h>
using namespace std;
class DATE //定义一个日期类
{
private:
	int _year;
	int _month;
	int _day;
public:
	DATE(int year =2017, int month = 3, int day = 15)

		: _year(year)
		, _month(month)
		, _day(day)
	{
		assert(Isinvalid());
		cout << " DATE(int year = 1995, int month = 4, int day =28)" << endl;
	}
	void Display()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	int GetMonthday( int year,int month)//定义一个每个月份天数的函数
	{
		char Month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};//初始化月份天数数组
		if (month!= 2)//判断二月份天数
		{
			return Month[month];
		}
		else
		{
			if (((year % 4 == 0) && (year % 100 !=0)) || (year % 400 == 0))//平年返回28天,闰年返回29天
			{
				return 29;
			}
			else
			{
				return 28;
			}
		}
	}
	bool Isinvalid()//判断日期的年月日是否合法
	{
		if ((_month > 0) && (_month<13) && (_day>0) && (_day<=GetMonthday(_year,_month)) && (_year>0))
			return true;
		else
			return false;
	}
	DATE(const DATE& d)//构造拷贝函数
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	DATE operator+(int day)//运算符重载+号,判断初始化的日期加上需要加的天数
	{
		if (day < 0)

		{
			return *this - (-day);
		}
		DATE tmp = *this;
		tmp._day += day;
		
		
		while (tmp.Isinvalid()==false)
			{
				tmp._day -= GetMonthday(tmp._year, tmp._month);
				if (tmp._month == 12)//当月份等于12时,年份需要进一,月份置为一月
				{
					tmp._year++;
					tmp._month = 1;
				}
				else
				{
					tmp._month++;
				}
			//mp._day -= GetMonthday(tmp._year, tmp._month);
			}
		return tmp;
	}
	DATE& operator++()// 直接引用,节省内存开销
	{
		return *this =*this+ 1;
	}
	DATE operator++(int day)
	{
		DATE tmp = *this;
		*this = *this + 1;
		return tmp;
	}
	DATE operator-(int day)
	{
		if (day < 0)
			return *this + (-day);
		
			DATE tmp = *this;
			tmp._day -= day;
			while (tmp.Isinvalid()==false)
			{
				
				if (tmp._month == 1)
				{
					tmp._year--;
					tmp._month = 12;
				}
				else
				{
					tmp._month--;
				}
				tmp._day += GetMonthday(_year, _month);
			}
			
			return tmp;
	}
	DATE& operator--()
	{
	
	return *this = *this - 1;
	}
	DATE operator--(int day)
	{
		DATE tmp = *this;
		*this= *this - 1;
		return tmp;
	}
	bool 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 operator==(const DATE&d)
	{
		if( (_year == d._year) && (_month == d._month) && (_day==d._day))
			return true;
		else
			return false;
	}
	bool operator>=(const DATE&d)
	{
		if ((*this > d) || (*this == d))
			return true;
		else
			return false;
	}
	bool operator<(const DATE&d)
	{

			return !(*this >= d);
	}
	bool operator!=(const DATE&d)
	{
		
			return !(*this == d);
	}
	bool operator<=(const DATE&d)
	{
	
			return !(*this>d);
	}
};
int main()
{
	DATE d1(2017,2,29);
	d1.Display();
	DATE d2;
	d2 = d1 - 100;
	d2.Display();
	d2 = d1 + 100;
	d2.Display();
	system("pause");
	
	
 }
当输入非法日期时,程序崩溃

输入日期合法时,程序成功运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值