大数据最全【C++】日期类的实现_定义日期结构体和学生结构体c+,2024年最新为什么大公司一定要使用微服务

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

+ [3.6 日期-=天数](#36__184)
+ [3.7 日期-天数](#37__212)
+ [3.8 前置++和后置++](#38__222)
+ [3.9 前置--和后置--](#39__245)
+ [3.10 ==](#310__266)
+ [3.11 比较符号](#311__278)
+ [3.12 日期-日期 返回天数](#312___339)

一、日期类声明

// 日期类声明
class Date
{
public:
private:
	int _year;
	int _month;
	int _day;
};`

.h

#pragma once
#include<iostream>
// 只展开常用的防止命名冲突
using std::cin;
using std::cout;
using std::endl;


二、日期类接口声明

// 日期类声明
class Date
{
public:
	// 构造函数
	Date(int year = 0, int month = 1, int day = 1);
	// 打印年月日
	void Print();
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);
	// 日期+=天数
	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);
	// >=运算符重载
	inline 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;
};

日期类的拷贝构造,析构,复制重载用默认生成的就行。


三、接口实现

3.1 构造函数及检查日期合法

Date::Date(int year, int month, int day)
{
	//判断日期是否合法
	if (year >= 0
		&& month >= 1 && month <= 12
		&& day >= 0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "非法日期" << endl;
	}
}


注意缺省参数在声明和定义只能出现一处,GetMonthDay函数在后面讲解。


3.2 打印日期

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

3.3 获取某年某月的天数

inline int Date::GetMonthDay(int year, int month)
{
	// 0对应0
	int Day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = Day[month];
	// 特殊情况:闰年2月
	if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0))
	{
		day++;
	}
	return day;
}

注意两点:

1)创建数组要创建13个位置,因为要对应“0月”。
2)因为要多次调用,所以设置成内敛函数


3.4 日期+=天数

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

能传引用就传引用,提高效率(减少拷贝)。


3.5 日期+天数

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

+的意思是不能改变原来的值,所以要创建临时变量返回,并且不能用引用返回(出作用域后销毁)。
我们发现我们写过+=,所以我们可以采用复用。

Date Date::operator+(int day)
{
	Date tmp(\*this);//拷贝构造
	tmp += day;
	return tmp;
}


3.6 日期-=天数

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

要注意day为负数的情况。上面的+=也同理。


3.7 日期-天数

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

3.8 前置++和后置++

前置++和后置++都完成了++,不同的地方在于返回值不同。
由于无法分辨前置和后置,所以函数名修饰规则:后面加int参数为后置,不加为前置。
前置++:

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

后置++:

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

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

return tmp;
}



[外链图片转存中...(img-jVxqrACA-1715424354238)]
[外链图片转存中...(img-DDFZdDxD-1715424354239)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值