C++—实现Date类

对于类的六个默认函数,
由于是Date类,
以下代码主要实现的是构造、拷贝、与运算符重载

#include <iostream>
using namespace std;

class Date
{
public:
	//构造函数
	Date(int year = 1900, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day){
		//若输入不规范,则改为默认日期
		if (getday(year, month, day) == false){
			cout << "输入有误,已改为默认日期" << endl;
			_year = 1900;
			_month = 1;
			_day = 1;
		}

	}
	//拷贝函数
	Date(const Date& d){
			_year = d._year;
			_month = d._month;
			_day = d._day;
	}
	//判断日期是否规范
	bool getday(int year, int month, int day){
		int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		//若年份为闰年则 二月改为29天
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
			days[1] = 29;
		}
		//日期的限制条件
		if (year <= 0 || month <= 0 || month > 12 || day >= days[month-1] || day <=0 ){
			return false;
		}
		return true;
	}
	//下面都是赋值运算符重载
	Date& operator=(const Date& d){
		//若不是拷贝自身则实现赋值
		if (this != &d){
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	Date operator+(int days){
		int day[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		//进入循环,当增加天数为零时终止循环,这里不考虑增加天数为负数时的情况
		while (days > 0){
			//每次循环都要重新判断一下是否为闰年
			if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0){
				day[1] = 29;
			}
			int d = days + _day;
			//若增加天数后,日期大于本月天数的最大值,则月份加一
			if (d > day[_month - 1]){
				++_month;
				//若月份数大于十二,则年数加一,月份变为一月
				if (_month > 12){
					++_year;
					_month = 1;
				}
				_day = d - day[_month - 1];
				days = _day;
			}
			else{
				_day = d;
				days = 0;
			}
		}
		return *this;
	}
	//此函数与operator+原理相近,只需要改变条件即可
	Date operator-(int days){
		int day[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		while (days > 0 ){
			if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0){
				day[1] = 29;
			}
			int d = _day - days;
			if (d <= 0){
				--_month;
				if (_month <= 0){
					--_year;
					_month = 12;
				}
				_day = day[_month - 1];
				days = days - _day;
			}
			else{
				_day = d;
				days = 0;
			}
		}
		return *this;
	}
	//+=、-=则可直接调用实现
	Date operator+=(int days){
		return (*this = (*this + days));
	}
	Date operator-=(int days){
		return (*this = (*this - days));
	}
	//int operator-(const Date& d);
	
	//前置++,与后置++都可直接调用
	Date& operator++(){
		return *this = *this + 1;
	}
	Date operator++(int){
		Date ret(*this);
		*this = *this + 1;
		return ret;
	}
	Date& operator--(){
		return *this = *this - 1;
	}
	Date operator--(int){
		Date ret(*this);
		*this = *this - 1;
		return ret;
	}
	bool operator>(const Date& d) const {
		//当年份大于时直接返回1
		if (_year > d._year){
			return true;
		}
		//当年份相等,月份数大于时直接返回1
		else if (_year == d._year && _month > d._month){
			return true;
		}
		//当年份与月份都相等时,就比较天数,大于则返回1
		else if (_year == d._year && _month == d._month&&_day > d._day){
			return true;
		}
		//除了上述条件则返回0
		return false;
	}
	//>= 则只需参照 > 加上相等的情况即可
	bool operator>=(const Date& d) const {
		if (_year >= d._year){
			return true;
		}
		else if (_year == d._year && _month >= d._month){
			return true;
		}
		else if (_year == d._year && _month == d._month&&_day >= d._day){
			return true;
		}
		return false;
	}
	//同理,将>的情况取反
	bool operator<(const Date& d) const {
		if (_year < d._year){
			return true;
		}
		else if (_year == d._year && _month < d._month){
			return true;
		}
		else if (_year == d._year && _month == d._month&&_day < d._day){
			return true;
		}
		return false;
	}
	//同理
	bool operator<=(const Date& d) const {
		if (_year <= d._year){
			return true;
		}
		else if (_year == d._year && _month <= d._month){
			return true;
		}
		else if (_year == d._year && _month == d._month&&_day <= d._day){
			return true;
		}
		return false;
	}
	bool operator==(const Date& d) const {
		return _year == d._year&&
			_month == d._month&&
			_day == d._day;
	}
	bool operator!=(const Date& d) const {
		return !operator==(d);
	}
	void display(){
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值