【C++】日期类的实现_定义日期结构体和学生结构体c++,2024年最新2024大数据开发高级进阶学习资料

28 阅读2分钟

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


三、接口实现

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


3.9 前置–和后置–

前置–:

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

后置–:

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


3.10 ==

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


3.11 比较符号

>

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

有了两个剩下的就可以用复用:

>=

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 || \*this == d;
}

!=

img img

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

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

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