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;
}
相关推荐
老赵的博客5 分钟前
c++ 杂记
开发语言·c++
jimmy.hua8 分钟前
[C++刷怪笼]:set/map--优质且易操作的容器
开发语言·c++
tan180°20 分钟前
Boost搜索引擎 网络库与前端(4)
linux·网络·c++·搜索引擎
bkspiderx1 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
w2sfot1 小时前
Passing Arguments as an Object in JavaScript
开发语言·javascript·ecmascript
郝学胜-神的一滴1 小时前
避免使用非const全局变量:C++中的最佳实践 (C++ Core Guidelines)
开发语言·c++·程序人生
搞一搞汽车电子2 小时前
S32K3平台eMIOS 应用说明
开发语言·驱动开发·笔记·单片机·嵌入式硬件·汽车
总有刁民想爱朕ha2 小时前
车牌模拟生成器:Python3.8+Opencv代码实现与商业应用前景(C#、python 开发包SDK)
开发语言·python·数据挖掘
小菜全3 小时前
uniapp新增页面及跳转配置方法
开发语言·前端·javascript·vue.js·前端框架
人衣aoa3 小时前
Python编程基础(八) | 类
开发语言·python