【C++】日期类的实现

1、Date.h

c 复制代码
#pragma once
#include <iostream>
using namespace std;

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1);

	void Print();

	//Date& operator=(const Date& d); //赋值重载

	int GetMonthDay(int year, int month);


	bool operator<(const Date& d); //d1 就是 this,d2 就是 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);
	int operator-(const Date& d); //日期-日期


	//++d1 -> d1.operator++()
	Date& operator++();

	//d1++ -> d1.operator++(0)
	//【加一个int参数,进行占位,跟前置++构成函数重载进行区分】
	//【本质:后置++调用,编译器进行特殊处理】
	Date operator++(int i);

	Date& operator--();
	Date operator--(int i);

private:
	//内置类型
	int _year;
	int _month;
	int _day;
};

2、Date.cpp

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	//检查日期是否合法
	if (month < 1 || month>12 || day<1 || day>GetMonthDay(year, month))
	{
		cout << "非法日期:";
		//exit(-1);
	}
}

void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

int Date::GetMonthDay(int year, int month)
{
	//GetMonthDay会被重复调用,加一个static就不用每次调用都创建一个数组,节省了空间
	static int monthArray[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 monthArray[month];
}

bool Date::operator<(const Date& d) //d1 就是 this,d2 就是 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;
	}
}

//d1==d1
bool Date::operator==(const Date& d) //d1 就是 this,d2 就是 d
{
	return (_year == d._year && _month == d._month && _day == d._day);
}

//d1<=d2
bool Date::operator<=(const Date& d) //d1 就是 this,d2 就是 d
{
	return *this < d || *this == d; //*this就是d1,d就是d2
}

//d1>d2
bool Date::operator>(const Date& d)
{
	return !(*this <= d); //>就是<=取反
}

//d1>=d2
bool Date::operator>=(const Date& d)
{
	return !(*this < d); //>=就是<取反
}

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

Date& Date::operator+=(int day) //日期+天数
{
	//若+=负数,等于-=正数
	if (day < 0)
	{
		return *this -= (-day);
	}

	_day += day;

	while (_day > GetMonthDay(_year, _month))
	{
		//月进位
		_day -= GetMonthDay(_year, _month);
		++_month;

		//月满,年进位
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this; //this是指针,返回*this
}

Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;
	return tmp;

	//tmp._day += day;
	//while (tmp._day > GetMonthDay(tmp._year, tmp._month))
	//{
	//	//月进位
	//	tmp._day -= GetMonthDay(tmp._year, tmp._month);
	//	++_month;

	//	//月满,年进位
	//	if (tmp._month == 13)
	//	{
	//		++tmp._year;
	//		tmp._month = 1;
	//	}
	//}
	//return tmp;
}

//Date& Date::operator=(const Date& d)
//{
//	if (this != &d) //屏蔽d1=d1的操作
//	{
//		this->_year = d._year;
//		this->_month = d._month;
//		this->_day = d._day;
//	}
//	return *this;
//}

Date& Date::operator-=(int day)
{
	//若-=负数,等于+=正数
	if (day < 0)
	{
		return *this += (-day);
	}

	_day -= day;

	while (_day < 1)
	{
		_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 i)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

Date Date::operator--(int i)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

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

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

	int n = 0;
	while (min != max) //算相差的天数
	{
		++min;
		++n;
	}

	return n * flag;
}

3、Test.cpp

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"

void Test1()
{
	Date d1(2023, 7, 23);
	d1.Print();

	Date d2(2023, 8, 23);
	d2.Print();

	Date d3(2023, 13, 0);
	d3.Print();
}

void Test2()
{
	Date d1(2023, 7, 23);
	d1.Print();

	Date d2(2023, 8, 13);
	d2.Print();

	//拷贝构造,一个已经存在的对象去初始化另外一个要创建的对象
	Date d3(d2); //默认拷贝构造
	d3.Print();

	//赋值,两个已经存在的对象进行拷贝
	d1 = d2; //d1.operator=(d2);
	d1.Print();

	d1 = d2 = d3;
	d1.Print();
}

void Test3()
{
	Date d1(2023, 7, 27);
	d1 += 2000;
	d1.Print();

	Date d2(2023, 7, 27);

	Date ret = d2; //拷贝构造,不是赋值 
	/*Date ret;
	ret = d2 + 2000;*/ //这是赋值
	ret.Print();
}

void Test4()
{
	Date d1(2023, 7, 27);
	d1 -= 2000;
	d1.Print();

	d1 -= -200;
	d1.Print();
}

void Test5()
{
	Date d1(2023, 7, 27);
	Date ret1 = d1++;
	//Date ret1 = d1.operator++(0); //显式调用,此处的值传多少都无所谓
	ret1.Print();
	d1.Print();

	Date ret2 = ++d1;
	//Date ret2 = d1.operator++(); //显式调用
	ret2.Print();
	d1.Print();
}

void Test6()
{
	Date d1(2002, 12, 14);
	Date d2(2023, 7, 27);
	cout << d2 - d1 << endl;
}

int main()
{
	cout << "Test1:" << endl;
	Test1();

	cout << endl;

	cout << "Test2:" << endl;
	Test2();

	cout << endl;

	cout << "Test3:" << endl;
	Test3();

	cout << endl;

	cout << "Test4:" << endl;
	Test4();

	cout << endl;

	cout << "Test5:" << endl;
	Test5();

	cout << endl;

	cout << "Test6:" << endl;
	Test6();

	return 0;
}

4、运行结果

相关推荐
源代码•宸30 分钟前
小米2025届软件开发工程师(C/C++/Java)(编程题AK)
c语言·c++·经验分享·算法·动态规划
NuyoahC37 分钟前
算法笔记(四)——模拟
c++·笔记·模拟算法
猫武士水星40 分钟前
排水系统C++
开发语言·c++
国中之林2 小时前
【刷点笔试面试题试试水】不使用任何中间变量如何将a、b的值进行交换?
c++·学习·面试·笔试·刷题
玄【学生党】3 小时前
中国身份证号码校验
c++
碧海蓝天20223 小时前
C++中,如何使你设计的迭代器被标准算法库所支持。
开发语言·c++
single5943 小时前
【优选算法】(第十五篇)
java·数据结构·c++·vscode·算法·leetcode
小林熬夜学编程4 小时前
【Linux系统编程】第二十六弹---彻底掌握文件I/O:C/C++文件接口与Linux系统调用实践
java·linux·服务器·c语言·c++·算法
CrazyZ1264 小时前
c++primer第十三章 类继承
开发语言·c++·笔记·学习
萧鼎4 小时前
C++ 游戏开发
c++·c