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;
}
相关推荐
糯米导航3 分钟前
Java毕业设计:WML信息查询与后端信息发布系统开发
java·开发语言·课程设计
南岩亦凛汀13 分钟前
在Linux下使用wxWidgets进行跨平台GUI开发
c++·跨平台·gui·开源框架·工程实战教程
MessiGo31 分钟前
Javascript 编程基础(5)面向对象 | 5.1、构造函数实例化对象
开发语言·javascript·原型模式
大霞上仙34 分钟前
nonlocal 与global关键字
开发语言·python
曦月逸霜36 分钟前
第34次CCF-CSP认证真题解析(目标300分做法)
数据结构·c++·算法
galaxy_strive39 分钟前
绘制饼图详细过程
开发语言·c++·qt
黑客老李1 小时前
JavaSec | SpringAOP 链学习分析
java·运维·服务器·开发语言·学习·apache·memcached
开开心心就好2 小时前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
特立独行的猫a2 小时前
Nuxt.js 中的路由配置详解
开发语言·前端·javascript·路由·nuxt·nuxtjs
勤奋的知更鸟2 小时前
Java编程之原型模式
java·开发语言·原型模式