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;
}
相关推荐
Y1nhl11 分钟前
搜广推校招面经八十一
开发语言·人工智能·pytorch·深度学习·机器学习·推荐算法·搜索算法
Algorithm157612 分钟前
谈谈接口和抽象类有什么区别?
java·开发语言
Starry_hello world13 分钟前
C++ 快速幂算法
c++·算法·有问必答
yu41062114 分钟前
Rust 语言使用场景分析
开发语言·后端·rust
良艺呐^O^15 分钟前
uniapp实现app自动更新
开发语言·javascript·uni-app
264玫瑰资源库2 小时前
问道数码兽 怀旧剧情回合手游源码搭建教程(反查重优化版)
java·开发语言·前端·游戏
普if加的帕3 小时前
java Springboot使用扣子Coze实现实时音频对话智能客服
java·开发语言·人工智能·spring boot·实时音视频·智能客服
2301_807611493 小时前
77. 组合
c++·算法·leetcode·深度优先·回溯
安冬的码畜日常3 小时前
【AI 加持下的 Python 编程实战 2_10】DIY 拓展:从扫雷小游戏开发再探问题分解与 AI 代码调试能力(中)
开发语言·前端·人工智能·ai·扫雷游戏·ai辅助编程·辅助编程
朝阳5814 小时前
Rust项目GPG签名配置指南
开发语言·后端·rust