既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
int GetMonthDay(int year, int month);
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d);
// 析构函数
~Date();
// 日期+=天数
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;
};
## 3. 具体代码实现
头文件
//date.h文件
#pragma once
#include
using namespace std;
class Date
{
//友员声明
//流输出
friend ostream& operator<<(ostream& out, const Date&d);
//流输入
friend istream& operator>>(istream& in, Date&d);
public:
Date(int year = 1970, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{}
// 拷贝构造,这里不显式写法也够用
//Date(const Date&d)
//{
// _year = d._year;
// _month = d._month;
// _day = d._day;
//}
//析构函数,这里不写也够用
//~Date()
//{
// \_year = 0;
// \_month = 0;
// \_day = 0;
//}
int GetMonthDay(int year, int month)
{
static int MonthDayArry[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 MonthDayArry[month];
}
}
bool operator==(Date&d)const;
bool operator!=(Date&d)const;
bool operator>(Date&d)const;
bool operator>=(Date&d)const;
bool operator<(Date&d)const;
bool operator<=(Date&d)const;
Date& operator+=(int day);
Date& operator-=(int day);
Date operator+(int day)const;
Date operator-(int day)const;
int operator-(Date&d)const;
//前置++
Date& operator++();
//后置++
Date operator++(int);
//前置--
Date& operator--();
//后置--
Date operator--(int);
Date& operator=(const Date& d);
private:
int _year;
int _month;
int _day;
};
功能函数实现文件
#include"Date.h"
bool Date::operator==(Date&d)const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
bool Date::operator!=(Date&d)const
{
return !(*this == d);
}
bool Date::operator>(Date&d)const
{
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;
}
else
{
return false;
}
}
bool Date::operator>=(Date&d)const
{
return *this == d
|| *this > d;
}
bool Date::operator<(Date&d)const
{
return !(*this>=d);
}
bool Date::operator<=(Date&d)const
{
return !(*this >d);
}
Date& Date::operator+=(int day)
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
_day -= GetMonthDay(_year, _month);
}
return *this;
}
Date& Date::operator-=(int day)
{
_day -= day;
while (_day <= 0)
{
_month–;
if (_month == 0)
{
_year–;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator+(int day)const
{
Date tem(*this);
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
W-1715731498926)]
[外链图片转存中…(img-iQdF4VOh-1715731498927)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新