日期类的实现

头文件

#include<iostream>

using namespace std;

class Date
{
public:
	int GetMonthDay(int year, int month)
	{
		static int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		{
			return 29;
		}
		return MonthDay[month];
	}
	//全缺省的构造函数
	Date(int year = 1, int month = 1, int day = 1);
	//拷贝构造函数
	Date(const Date& d);
	//赋值运算符重载
	Date& operator=(const Date& d);
	//日期+=天数
	Date& operator+=(int day);
	//日期+天数
	Date operator+(int day);
	//日期-=天数
	Date& operator-=(int day);
	//日期-天数
	Date operator-(int day);
	//前置++
	Date& operator++();
	//后置++
	Date operator++(int);
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);
	// >运算符重载
	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);
	// 日期-日期 返回天数
	int operator-(const Date& d);
	//打印
	void Print();
	//析构函数
	~Date();
private:
	int _year;
	int _month;
	int _day;
};

源文件

#include"Date.h"

//全缺省的构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
//拷贝构造函数
Date::Date(const Date& d)
{
	_day = d._day;
	_month = d._month;
	_year = d._year;
}
//赋值运算符重载
Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}
//日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
//日期+天数
Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}
//日期-=天数
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
//日期-天数
Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}
//前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//后置++
Date Date::operator++(int)
{
	Date tmp = *this;
	++(*this);
	return tmp;
}
//前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
//后置--
Date Date::operator--(int)
{
	Date tmp = *this;
	--(*this);
	return tmp;
}
// >运算符重载
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 _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
// >=运算符重载
bool Date::operator>=(const Date& d)
{
	return *this == d || *this > d;
}
// <运算符重载
bool Date::operator<(const Date& d)
{
	return !(*this >= d);
}
// <=运算符重载
bool Date::operator <= (const Date& d)
{
	return !(*this > d);
}
// !=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	int count = 0;
	Date max = *this;
	Date min = d;
	if (max < min)
	{
		max = d;
		min = *this;
	}
	while (max != min)
	{
		min++;
		count++;
	}
	if (*this < d)
	{
		count = -count;
	}
	return count;
}
void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
Date::~Date()
{
	;
}
相关推荐
漫漫进阶路38 分钟前
VS C++ 配置OPENCV环境
开发语言·c++·opencv
BinaryBardC2 小时前
Swift语言的网络编程
开发语言·后端·golang
code_shenbing2 小时前
基于 WPF 平台使用纯 C# 制作流体动画
开发语言·c#·wpf
邓熙榆2 小时前
Haskell语言的正则表达式
开发语言·后端·golang
ac-er88883 小时前
Yii框架中的队列:如何实现异步操作
android·开发语言·php
马船长3 小时前
青少年CTF练习平台 PHP的后门
开发语言·php
hefaxiang4 小时前
【C++】函数重载
开发语言·c++·算法
花生树什么树4 小时前
下载Visual Studio Community 2019
c++·visual studio·vs2019·community
exp_add35 小时前
Codeforces Round 1000 (Div. 2) A-C
c++·算法
落幕5 小时前
C语言-构造数据类型
c语言·开发语言