日期类深度解析

本文详细介绍了C++中的日期类模块,包括其构造函数、拷贝构造函数、赋值运算符、析构函数以及日期与天数、日期与日期的运算符重载,如+/-运算、比较操作等。源码示例展示了如何实现这些功能以支持日期计算和比较。
摘要由CSDN通过智能技术生成

目录

日期类模块:

基本内置模块:

日期与天数操作模块:

日期与日期操作模块:

日期类模块的实现:

基本内置模块:

构造函数:

拷贝构造函数:

赋值运算符重载:

析构函数:

函数打印:

日期与天数操作符:

获取某年某月天数:

日期+=天数:

日期+天数:

日期-=天数:

日期-天数:

前置++:

后置++:

前置--:

后置--:

日期与日期操作符:

>运算符重载

==运算符重载

>=运算符重载

<运算符重载

<=运算符重载

!=运算符重载

日期-日期 返回天数

源码:

Date.h:

Date.cpp:


日期类模块:

基本内置模块:

	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	// 拷贝构造函数
	Date(const Date& d);
	// 赋值运算符重载
	Date& operator=(const Date& d);
	 析构函数
	~Date();
	//函数打印
	void Print();

日期与天数操作模块:

	// 日期+=天数
	Date& operator+=(int day);
	// 日期+天数
	Date operator+(int day);
	// 日期-天数
	Date operator-(int day);
	// 日期-=天数
	Date& operator-=(int day);


	// 前置++
	Date& operator++();
	// 后置++
	Date operator++(int);
	// 后置--
	Date operator--(int);
	// 前置--
	Date& operator--();

日期与日期操作模块:

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

	// 日期-日期 返回天数
	int operator-(const Date& d);

日期类模块的实现:

将定义和声明分开书写,增加代码可读性,.cpp文件中采用Date::链接.h中函数的定义

基本内置模块:

构造函数:

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

拷贝构造函数:

Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

赋值运算符重载:

对于赋值运算符重载的设计,有以下几个点需要注意:

  • 函数返回值类型为Date,目的是为了解决连续赋值的情况。eg:d1 = d2 = d3
  • Date&和return *this的搭配可以提高运行效率,如果没有引用操作符的使用,则函数参数this在调用时会调用类的构造函数和析构函数,从而降低运行效率,
Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

析构函数:

Date::~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}

函数打印:

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

日期与天数操作符:

获取某年某月天数:

创建数组封装各个月份的天数值,由于数组从下标0开始存储,为了方便月份的操作,将数组设置为长度为13的数组,将下标0内的元素设置为-1,剩下下标元素与各个月份对应的天数值对应

int Date::GetMonthDay(int year, int month)
{
	int day[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 || ((year / 100 != 0 && year / 4 == 0) || (year / 400 == 0)))
	{
		return 29;
	}
	else
	{
		return day[month];
	}
}

日期+=天数:

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

日期+天数:

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

日期-=天数:

Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		_day += GetMonthDay(_year, _month);
		if (_month < 1)
		{
			_year--;
			_month = 12;
		}
	}
	return *this;
}

日期-天数:

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

前置++:

前置++:返回+1之后的结果
注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

后置++:

前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载
C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器
自动传递
注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存
一份,然后给this+1,而temp是临时对象,因此只能以值的方式返回,不能返回引用
Date Date::operator++(int)
{
	Date tmp = *this;
	tmp += 1;
	return tmp;
}

前置--:

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

后置--:

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

日期与日期操作符:

虽然一共要实现>、==、>=、<、<=、!=六种日期与日期操作符,但实际上可以将其简化,仅仅完成>和==的完整源码,剩下来的操作符作这两种操作符的组合调用即可

>运算符重载

bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
	}
	if (_year == d._year)
	{
		if (_month == d._month)
		{
			if (_day > d._day)
			{
				return true;
			}
		}
	}
	return false;
}

==运算符重载

bool Date::operator==(const Date& d)
{
	return _year == d._year &&
		_month == d._month &&
		_day == d._day;
}

>=运算符重载

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);
}

!=运算符重载

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

日期-日期 返回天数

设置一个计数器n,对于其中较小的日期进行累加操作,直到满足跳出循环条件,n的值即为两者相差天数

int Date::operator-(const Date& d)
{
	int n = 0;
	Date max = d;
	Date min = *this;
	if (*this > d)
	{
		max = *this;
		min = d;
	}
	while (max > min)
	{
		n++;
		min +=1;
	}
	return n;
}

源码:

Date.h:

#pragma once
#include <iostream>
using namespace std;
class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);
	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	// 拷贝构造函数
	Date(const Date& d);
	// 赋值运算符重载
	Date& operator=(const Date& d);
	 析构函数
	~Date();
	//函数打印
	void Print();

	// 日期+=天数
	Date& operator+=(int day);
	// 日期+天数
	Date operator+(int day);
	// 日期-天数
	Date operator-(int day);
	// 日期-=天数
	Date& operator-=(int day);


	// 前置++
	Date& operator++();
	// 后置++
	Date operator++(int);
	// 后置--
	Date operator--(int);
	// 前置--
	Date& operator--();

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

	// 日期-日期 返回天数
	int operator-(const Date& d);

private:
	int _year;
	int _month;
	int _day;
};

Date.cpp:

#include "Date.h"
// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
	int day[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 || (year / 100 != 0 && year / 4 == 0) && (year / 400 == 0))
	{
		return 29;
	}
	else
	{
		return day[month];
	}
}
// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
// 拷贝构造函数
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
// 赋值运算符重载
Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}
// 析构函数
Date::~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}
//函数打印
void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

// 日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;
	while(_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
// 日期+天数
Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}
// 日期-天数
Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		_day += GetMonthDay(_year, _month);
		if (_month < 1)
		{
			_year--;
			_month = 12;
		}
	}
	return *this;
}

//前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
// 后置++
Date Date::operator++(int)
{
	Date tmp = *this;
	tmp += 1;
	return tmp;
}
// 后置--
Date Date::operator--(int)
{
	Date tmp = *this;
	tmp -= 1;
	return tmp;
}
// 前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

// >运算符重载
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
	}
	if (_year == d._year)
	{
		if (_month == d._month)
		{
			if (_day > d._day)
			{
				return true;
			}
		}
	}
	return false;
}
// ==运算符重载
bool Date::operator==(const Date& d)
{
	return _year == d._year &&
		_month == d._month &&
		_day == d._day;
}
// >=运算符重载
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);
}
// !=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}

// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	int n = 0;
	Date max = d;
	Date min = *this;
	if (*this > d)
	{
		max = *this;
		min = d;
	}
	while (max > min)
	{
		n++;
		min +=1;
	}
	return n;
}

  • 13
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值