C++实现日期类

 

题目:C++实现日期类

Date.h

#pragma once

#include <iostream>
using namespace std;

//日期类
class Date
{
private:
	int _year;
	int _month;
	int _day;
	static int _count;//用来统计该类一共创建了多少个对象(要在类外初始化)
public:
	//构造函数
	Date(int year = 2018, int month = 8, int day = 8)
		: _year(year)
		, _month(month)
		, _day(day)
	{}

	//拷贝构造函数
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	//赋值运算符重载
	//为了支持连续赋值,返回Date&,而且出了作用域对象还在
	//返回引用高效
	Date& operator=(const Date& d)
	{
		if (this != &d)//防止自己给自己赋值
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}

	//析构函数
	~Date()
	{
		//cout << "~Date()" << endl;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	//取地址操作符重载(一般不用我们自己写)

	//6个关系操作符的重载(注意只需要写==,>其它的可以复用)
	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);//小于等于
	bool operator!=(const Date& d);//不等于

	//+=   -=操作符重载(原日期改变)
	Date& operator+=(int n);//日期+天数=日期
	Date& operator-=(int n);//日期-天数=日期

	//+   -操作符重载(原日期不变)
	Date operator+(int n);//日期+天数=日期
	Date operator-(int n);//日期-天数=日期

	//自增、自减
	Date& operator++();//前置++
	Date& operator--();//前置--
	Date operator++(int);//后置++
	Date operator--(int);//后置--

	//日期-日期=天数
	int operator-(const Date& d);

};

Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
#include <iostream>
using namespace std;

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)
	{
		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;
	}
	else
	{
		return false;
	}
}
bool Date::operator<(const Date& d)//小于
{
	return !(*this > d || *this == d);
}
bool Date::operator>=(const Date& d)//大于等于
{
	return (*this > d || *this == d);
}
bool Date::operator<=(const Date& d)//小于等于
{
	return !(*this < d || *this == d);
}
bool Date::operator!=(const Date& d)//不等于
{
	return !(*this == d);
}

bool IsLeapYear(int year)//判断一年是否闰年
{
	if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//给定年份、月份,求出这一月的天数
int GetYMDays(int year, int month)
{
	int monthdays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if (month == 2 && IsLeapYear(year))//当这一月是2月并且这一年闰年是
	{
		return 29;
	}
	else
	{
		return monthdays[month];
	}
}

Date& Date::operator+=(int n)//日期+天数=日期(原日期发送变化)
{
	if (n < 0)
	{
		return *this -= (-n);
	}
	_day += n;
	while (_day > GetYMDays(_year, _month))
	{
		//加了一个整数(天数),导致天数>当年当月天数
		//说明这一年这一月应该满了,则将这一月的天数从总天数中减掉,
		//合成这一个月,再将月份加1,将剩下的天数往下一个月放...
		_day -= GetYMDays(_year, _month);
		_month++;
		if (_month > 12)//但是当加完月份,发现不合法了,那说明这一年满了
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Date& Date::operator-=(int n)//日期-天数=日期(原日期发生变化)
{
	if (n < 0)
	{
		return *this += (-n);
	}
	_day -= n;
	while (_day <= 0)//天数不合法
	{
		_month--;//因为要向上一个月借天数
		if (_month == 0)//这时候应该向上一年借
		{
			_year--;
			_month = 12;
		}
		//加上从算好的这一年这一月借的天数,看看
		//够不够,不够继续向上一个月借
		_day += GetYMDays(_year, _month);
	}
	return *this;
}

//+   -操作符重载(原日期不变)
Date Date::operator+(int n)//日期+天数=日期
{
	Date ret(*this);//用原来的拷贝一份

	ret += n;//复用加等操作符

	return ret;
}
Date Date::operator-(int n)//日期-天数=日期
{
	Date ret(*this);//用原来的拷贝一份

	ret -= n;//复用减等操作符

	return ret;
}

//自增、自减
Date& Date::operator++()//前置++
{
	*this += 1;//复用加等
	return *this;
}
Date& Date::operator--()//前置--
{
	*this -= 1;
	return *this;
}
Date Date::operator++(int)//后置++
{
	Date ret(*this);//拷贝构造一个新的ret
	*this += 1;//加等1了
	return ret;//但是返回的是之前没有加的
}
Date Date::operator--(int)//后置--
{
	Date ret(*this);
	*this -= 1;
	return ret;
}
//注意前置返回的是引用,因为*this作用域还存在,
//后置返回的是局部对象,出了作用域不存在,返回值类型

int Date::operator-(const Date& d)
{
	int flag = 1;
	int count = 0;
	
	Date max(*this);
	Date min(d);

	if (*this < d)//保证max是那个大的
	{
		max = d;
		min = *this;
		flag = -1;
	}

	//小的追上大的
	while (min != max)
	{
		min++;
		count++;
	}
	return count*flag;
}

int Date::_count=0;//初始化静态变量
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值