C++实现Date类

Date.h

cpp 复制代码
#pragma once

#include <iostream>
using std::cout;
using std::endl;

class Date {
private:
	int _year = 1;
	int _month = 1;
	int _day = 1;

public:
	//日期类无需显式定义拷贝构造函数、析构函数、赋值运算符重载

	//打印
	void Print();
	//有参构造函数
	Date(int year = 1, int month = 1, int day = 1);
	//==重载
	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 day);
	//日期+天数重载
	Date operator+(int day);
	//日期-=天数重载
	Date& operator-=(int day);
	//日期-天数重载
	Date operator-(int day);
	//日期前置++重载
	Date& operator++();
	//日期后置++重载
	Date operator++(int);
	//日期类-日期类重载
	int operator-(const Date& d);

	//某月的天数(函数直接定义在类中相当于内联函数)
	int Getmonthday(const Date& d)
	{
		static int monthday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (d._month == 2 && (((d._year % 4 == 0) && (d._year % 100 != 0)) || d._year % 400 == 0))
		{
			return 29;
		}
		return monthday[d._month];
	}
	
};

Date.cpp

cpp 复制代码
#include "Date.h"
//打印
void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

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

//==重载
bool Date::operator==(const Date& d)
{
	return this->_year == d._year
		&& this->_month == d._month
		&& this->_day == d._day;
}

//!=重载
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

//<重载
bool Date::operator<(const Date& d)
{
	if (this->_year < d._year)
	{
		return true;
	}
	else if (this->_year == d._year)
	{
		if (this->_month < d._month)
		{
			return true;
		}
		else if (this->_month == d._month)
		{
			if (this->_day < this->_day)
			{
				return true;
			}
		}
	}
	return false;
}

//<=重载
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);
}

//日期+=天数重载
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > Getmonthday(*this))
	{
		_day -= Getmonthday(*this);
		++_month;
		if (_month > 12)
		{
			_month = 1;
			++_year;
		}
	}
	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)
		{
			_month = 12;
			--_year;
		}
		_day += Getmonthday(*this);
	}
	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 += 1;
	return tmp;
}

//日期类-日期类重载
int Date::operator-(const Date& d)
{
	Date min = *this;
	Date max = d;
	int flag = -1;

	if (*this > d)
	{
		flag = 1;
		min = d;
		max = *this;
	}

	int n = 0;
	while (min != max)
	{
		min++;
		n++;
	}
	return n * flag;
}
相关推荐
YH_DevJourney7 分钟前
Linux-C/C++《C/9、信号:基础》(基本概念、信号分类、信号传递等)
linux·c语言·c++
FL162386312942 分钟前
[C++]使用纯opencv部署yolov12目标检测onnx模型
c++·opencv·yolo
JenKinJia1 小时前
Windows10配置C++版本的Kafka,并进行发布和订阅测试
开发语言·c++
wen__xvn1 小时前
每日一题洛谷P1914 小书童——凯撒密码c++
数据结构·c++·算法
云中飞鸿2 小时前
MFC中CString的Format、与XML中的XML_SETTEXT格式化注意
xml·c++·mfc
小小小白的编程日记3 小时前
List的基本功能(1)
数据结构·c++·算法·stl·list
努力可抵万难3 小时前
C++11新特性
开发语言·c++
ox00803 小时前
C++ 设计模式-策略模式
c++·设计模式·策略模式
egoist20233 小时前
【C++指南】一文总结C++类和对象【上】
开发语言·c++·类和对象·内存对齐·热榜·this指针·c++ 11
月上柳梢头&3 小时前
[C++ ]使用std::string作为函数参数时的注意事项
开发语言·c++·算法