C++经典日期类实现

学完C++类和对象以及四个默认成员函数,就可以来尝试自己实现一下日期类。完成日期类的各种功能,并加强自己的代码能力。

日期类实现

定义一个日期类

cpp 复制代码
// 文件:Date.h
#pragma once
#include <iostream>
using std::cout;
using std::endl;

//定义日期类
class Date
{
public:
	//直接写到类里面的函数默认inline修饰
	//所以短小的代码最好可以直接在类里面定义

	//构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//缺省构造
	Date()
	{}
	//析构函数、拷贝构造、赋值运算符重载编译器自己实现

	//打印日期类
	void Print() 
	{
		cout << "Date:" << _year << " " << _month << " " << _day << endl;
	}

private:
	//缺省赋值
	int _year = 0;
	int _month = 0;
	int _day = 0;
};

定义日期类的成员函数(方法)

cpp 复制代码
// 文件:Date.h
class Date
{
public:
    //...

	//逻辑运算符重载
	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);

	//日期++
	// 为了区分前置++和后置++
	// 后置++运算符重载函数中加入int类型占位
	Date& operator++();		//前置++
	Date& operator++(int);		//后置++
    //...
}

实现日期类的功能

cpp 复制代码
// 文件:Date.cpp
#include "Date.h"
//小于
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;
	}
	return false;
}
//等于
bool Date::operator==(const Date& d)
{
	if (_year == d._year && _month == d._month && _day == d._day)
		return true;
	return false;
}
//不等于
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}
//小于等于
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);
}

int GetMonthDays(int year, int month)
{
	int common[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;
	}
	else
	{
		return common[month];
	}
}

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

	//Date tmp = *this;
	//tmp._day += day;
	//while (_day > GetMonthDays(tmp._year, tmp._month))
	//{
	//	day -= GetMonthDays(tmp._year, tmp._month);
	//	tmp._month++;
	//	if (tmp._month == 13)
	//	{
	//		tmp._month = 1;
	//		tmp._year++;
	//	}
	//}
	//return tmp;
}

Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDays(_year, _month))
	{
		_day -= GetMonthDays(_year, _month);
		_month++;
		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}

Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day < 0)
	{
		_day += GetMonthDays(_year, _month - 1);
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 1;
		}
	}
	if (_day == 0)
	{
		_month--;
		_day = GetMonthDays(_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 += 1;
	return tmp;
}

测试验证

cpp 复制代码
// 文件:test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "Date.h"

int main()
{
	Date D1(2024, 3, 3);
	D1.Print();
	D1 -= 5;
	D1.Print();
	Date D2 = D1;
	D2.Print();
	D2++;
	D2.Print();
	return 0;
}
相关推荐
QiLinkOS25 分钟前
《打破“用爱发电”:一种基于 Gitee 与时间戳的开源权益分配机制探索》
c语言·数据结构·c++·科技·算法·gitee·开源
Irissgwe1 小时前
c++STL--string类
c++·stl·string
Irissgwe1 小时前
c++类型转换
c++·类型转换·explicit·static_cast·const_cast·dynamic_cast·rtti
智者知已应修善业1 小时前
【51单片机用T0定时器方式1,实现0.5S的时间间隔实现第一次一个灯亮、第二次二个灯亮,直到全部灯亮,然后重复整个过程】2023-12-29
c++·经验分享·笔记·算法·51单片机
在繁华处1 小时前
Java从零到熟练(九):并发编程基础
java·开发语言
木头程序员1 小时前
SSM框架学习笔记
java·开发语言·mysql·spring·maven
一起逃去看海吧1 小时前
dify-03
java·linux·开发语言
智者知已应修善业2 小时前
【51单片机4位静态数码管显示1234】2023-11-14
c++·经验分享·笔记·算法·51单片机
抓虾爪2 小时前
ST意法代理商粤科源兴丨LSM6DS3全系列现货库存,LSM6DS3TR-C当天可发
c++
妙为2 小时前
unreal engine5.7.4,创建ThirdPerson第三人称模版,类型是c++崩溃
c++·ue5·虚幻·unreal engine5