最新【C++】日期类的实现_定义日期结构体和学生结构体c+(1),大数据开发开发面经分享
的意思是不能改变原来的值,所以要创建临时变量返回,并且不能用引用返回(出作用域后销毁)。由于无法分辨前置和后置,所以函数名修饰规则:后面加int参数为后置,不加为前置。前置++和后置++都完成了++,不同的地方在于返回值不同。我们发现我们写过+=,所以我们可以采用复用。要注意day为负数的情况。能传引用就传引用,提高效率(减少拷贝)。
·
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
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;
}
!=
bool Date::operator != (const Date& d)
{
return !(\*this == d);
}
3.12 日期-日期 返回天数
int Date::operator-(const Date& d)
{
Date max = \*this;
Date min = d;
int flag = 1;



**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**
ag = 1;
[外链图片转存中...(img-rAQLop0h-1715499368025)]
[外链图片转存中...(img-vOJr3crq-1715499368025)]
[外链图片转存中...(img-Ziwpr4Km-1715499368025)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**
更多推荐
所有评论(0)