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;
}
相关推荐
位东风20 分钟前
【c++学习记录】状态模式,实现一个登陆功能
c++·学习·状态模式
雷羿 LexChien2 小时前
C++内存泄漏排查
开发语言·c++
嘉小华3 小时前
CMake 完全指南:第一章 - 构建的烦恼 - 为什么需要CMake?
c++
Feliz Da Vida3 小时前
[代码学习] c++ 通过H矩阵快速生成图像对应的mask
c++·学习
无聊的小坏坏4 小时前
单调栈通关指南:从力扣 84 到力扣 42
c++·算法·leetcode
YOLO大师5 小时前
华为OD机试 2025B卷 - 小明减肥(C++&Python&JAVA&JS&C语言)
c++·python·华为od·华为od机试·华为od2025b卷·华为机试2025b卷·华为od机试2025b卷
看到我,请让我去学习6 小时前
OpenCV编程- (图像基础处理:噪声、滤波、直方图与边缘检测)
c语言·c++·人工智能·opencv·计算机视觉
xiaolang_8616_wjl15 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源
夜月yeyue15 小时前
设计模式分析
linux·c++·stm32·单片机·嵌入式硬件
无小道15 小时前
c++-引用(包括完美转发,移动构造,万能引用)
c语言·开发语言·汇编·c++