目录
首先,日期类不能没有年月日,因此需要年月日参数,我们int一下这三个参数,我们不希望外界直接访问这三个参数,因此设置为private,同时我们需要自己写一个拷贝构造来对参数进行初始化。这里使用了全缺省,同时为了更加贴合实战项目的要求,我们将声明写到头文件Date.h里,将定义写到Date.cpp里,来进行声明和定义分离。
同时为了保证我们传入的参数是合法的,我们在拷贝构造函数里可以添加判断,先写出一个 GetMonthDay 类函数来获取这一年的这一月有多少天
Date.h的声明
- class Date
- {
- public:
- Date(int year = 1, int month = 1, int day = 1);
- int GetMonthDay(int year,int month);
- private:
- int _year;
- int _month;
- int _day;
- };
我们在Date.cpp写了定义。构造函数赋值并判断是否合法 GetMonthDay 能返回这一年的这个月有几天
- Date::Date(int year, int month, int day)
- {
- _year = year;
- _month = month;
- _day = day;
- if (month < 1 || month>12 || day<1 || day>GetMonthDay(year, month))
- {
- cout << "非法日期" << endl;
- }
- }
-
- int Date::GetMonthDay(int year, int month)
- {
- static int MonthDay[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
- if (month == 2 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
- {
- return 29;
- }
- return MonthDay[month];
- }
注意:缺省参数一定要写在声明的地方,定义的地方不能写缺省参数。
我们想要一个能简便的对比两个日期类对象的大小,也就是两个日期比较大小,由于是自定义类型不能直接比较,因此我们要进行函数重载,在Date.h文件类里面添加上如下定义
bool operator<(const Date& d);
同时在Date.cpp添加上函数实现,注意一定要在函数名前加上 Date:: 来表示是那个作用域下的,代码部分就是简单的作比较,先比较年,再比较月,最后比较天是最符合逻辑的。
- bool Date::operator<(const Date& d)
- {
- 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 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);
定义:
- bool Date::operator<(const Date& d)
- {
- 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==(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);
- }
我们写个日期类打印函数测试一下
Date.h 里面声明
void Print();
Date.cpp里面定义
- void Date::Print()
- {
- cout << _year << "/" << _month << "/" << _day << endl;
- }
已经可以完成Date类对象的比较操作了

我们还想做到运算这个日期加上天数是什么日期。
例如今天是2023年10月10号,我想要知道300天后是几几年多少月多少号,就可以重载加法运算符来帮助我们计算。
首先要直接将要加的天数先加到当前类对象的_day,算出到了这个月的多少天,在判断这个天数有没有越阶,如果天数不符合,就将天数减去这个月的天数,同时月份++,如果月份也溢出了,到了13月,就将月份置为1,年再加加。注意返回值为 *this 因为是+=,返回的就是类对象本身,同时由于函数结束后, *this 依然存在,因此可以穿&(引用返回)。
同时,day有可能为负数,那就相当于减day天,因此我们还要加个判断 day大于等于0走上面的代码,小于0可以复用上-=。我们只需把-=完善一下即可
- if (day >= 0)
- {
- this->_day += day;
- while (_day > GetMonthDay(_year,_month))
- {
- _day -= GetMonthDay(_year, _month);
- _month++;
- if (_month == 13)
- {
- _month = 1;
- _year++;
- }
- }
- }
- else
- {
- *this -= (-day);
- }
- return *this;
-=和+=类似的,唯一的区别是要区分出去找哪个月借,这里是先将_month-- 之后,在去让_day+=月的天数,因为你当前day是负数后,你需要找前一个月去借天数,直到大于0才对。
- Date& Date::operator-=(int day)
- {
- if (day >= 0)
- {
- this->_day -= day;
- while (_day <= 0)
- {
- _month--;
- _day += GetMonthDay(_year, _month);
- if (_month == 0)
- {
- _month = 12;
- _year--;
- }
- }
- }
- else
- {
- *this += (-day);
- }
- return *this;
- }
日期类的+=和-=我们已经处理好了,还有+和-没有重载,我们也可以直接服用,拷贝构造一个tmp类对象,复用+=和-=,再return这个临时对象即可。
注意:返回值不能加&(引用),因为tmp的生命周期是在这个函数内部,出了函数就销毁, 因此不能加&
- 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.h文件填上就好,后续就不多赘述了,在最后面也会附上代码。
运算符重载还有++和--
这里会产生一个问题,按照之前的思路来重载++运算符,你无法区分这个++是前置还是后置,C++祖师爷添加了一个参数来区分前置还是后置,没有参数的是前置++,有int参数的是后置++,这个int可以不给值,他只是一个占位符,来表示后置++。
同理 -- 也是一样的。
这里我们也可以复用之前的+=来帮我们处理,前置++返回的是++后的值,直接返回 *this,因此可以用&返回。
后置++返回的是++前的值, 因此需要拷贝构造一份tmp,返回tmp即可,tmp出了函数就销毁了不能用&返回。
注意:对于类对象来说,前置++没有产生拷贝构造,而后置++产生了拷贝构造,因此如果只是单条语句用前置++效率会更高
- Date& Date::operator++()
- {
- *this += 1;
- return *this;
- }
- Date Date::operator++(int)
- {
- Date tmp(*this);
- *this += 1;
- return tmp;
- }
- Date& Date::operator--()
- {
- *this -= 1;
- return *this;
- }
- Date Date::operator--(int)
- {
- Date tmp(*this);
- *this -= 1;
- return tmp;
- }
日期减日期,可以找到这两个日期之前相差多少天,是日期加天数的变形。
这里采用了一个很简单的方法,就是先找到两个日期小的那个,再将小的日期一步一步加到大的日期,加了多少次,就有多少天。
flag既可以判断谁大,又可以作为符号,因为是相减,有可能是小的日期减去大的日期,这样结果是一个负数。
- int Date::operator-(const Date& d)
- {
- int flag = 1;
- Date max = *this;
- Date min = d;
- if (max < min)
- {
- flag = -1;
- max = d;
- min = *this;
- }
- int n = 0;
- while (max != min)
- {
- ++min;
- ++n;
- }
- return n * flag;
- }
再运行一下检查


跟网上的日期计算器作对比,没问题~。
对于一个参数有类对象的函数,如果我们不需要在函数里面进行修改操作,那么我们可以给类对象添加上const修饰,使其无法修改,这样操作会更加安全,但是这有会引发一些问题,例如类对象调用不了类的成员函数

为什么会发生上图这种情况?
因为这里的d是一个const修饰的类对象,而 Print() 函数是一个普通的成员函数,没有被const修饰,这就会引发权限放大的问题,由const转为非const,是不能够实现的,即使我们在 Print() 函数例没有进行任何修改。
那么似乎我们只需要给类对象的成员函数加上 const 修饰一下即可,但是我们又发现函数实现并没有传任何参数,只有一个默认传的this指针,而this指针我们既不需要传给函数,又不能够穿给函数,这似乎是一个悖论,那么我们该如何让这个指针加上const呢?

C++祖师爷想了一个很好的方法,在 Print() 后面添加上const来代表传递的this指针为const,如下

这个时候我们回过头来看,就会发现代码不再报错了,也可以正常调用了。

再验证一下普通类对象能不嫩调用 const函数,可以发现也是没问题的。

同理,如果函数是只读函数(内部不涉及修改成员的都是只读函数),就可以加const。如下
- bool operator<(const Date& d) const;
- bool operator==(const Date& d) const;
- bool operator<=(const Date& d) const;
- bool operator>(const Date& d) const;
- bool operator>=(const Date& d) const;
- bool operator!=(const Date& d) const;
- Date& operator+=(int day);
- Date& operator-=(int day);
- Date operator+(int day) const;
- Date operator-(int day) const;
- Date& operator++();
- Date operator++(int);
- Date& operator--();
- Date operator--(int);
- //日期相减
- int operator-(const Date& d) const;
- void Print() const;
- int GetMonthDay(int year,int month) const;
const对象不可以调用非const成员函数(权限的扩大)
非const对象可以调用const成员函数 (权限的缩小)
权限可以平移,可以缩小,但是不能放大
对于类对象来说,再后面加了const函数和不加const函数是构成重载的,因为他们默认给到的this指针类型不同。因此有时候会需要我们写两个函数,一个是const的一个是非const的,如下

不过这里我们可以不需要重载,没涉及到那么复杂的情景,加了const就足够了。
我们想让日期类对象和普通的内置类型一样可以用 cin和 cout 来处理,就得重载>>和 << 这两个操作符,如果我们将重载 >> 函数放在类的里面是不可行的,如下
- ostream& Date::operator<<(ostream& out)
- {
- out << _year << "/" << _month << "/" << _day << endl;
- return out;
- }

我们明明重载了,为啥会报链接错误呢?因为this指针默认在第一个参数。而out是第二个参数,这和我们的代码 cout << d1; 不匹配,要交换位置才能运行,因此我们不能够这样写。

这里我们采取的方法是写出全局函数,用友元的形式去访问类的私有变量。 将 类对象放在第二个参数的位置就能符合我们的要求。同时也重载一下 >> 流输入,只需要关注流输入的类对象d不要加const修饰就行,因为我们要修改这个对象,因此不能加const。
- friend ostream& operator<<(ostream& out, const Date& d);
- friend istream& operator>>(istream& in, Date& d);
- ostream& operator<<(ostream& out, const Date& d)
- {
- out << d._year << "/" << d._month << "/" << d._day << endl;
- return out;
- }
-
- istream& operator>>(istream& in, Date& d)
- {
- in >> d._year >> d._month >> d._day;
- return in;
- }
如此一来,代码就大功告成了

附上全部代码
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 = 1, int month = 1, int day = 1);
- bool operator<(const Date& d) const;
- bool operator==(const Date& d) const;
- bool operator<=(const Date& d) const;
- bool operator>(const Date& d) const;
- bool operator>=(const Date& d) const;
- bool operator!=(const Date& d) const;
- Date& operator+=(int day);
- Date& operator-=(int day);
- Date operator+(int day) const;
- Date operator-(int day) const;
- Date& operator++();
- Date operator++(int);
- Date& operator--();
- Date operator--(int);
- int operator-(const Date& d) const;
-
- void Print() const;
- int GetMonthDay(int year,int month) const;
- private:
- int _year;
- int _month;
- int _day;
- };
-
- ostream& operator<<(ostream& out, const Date& d);
- istream& operator>>(istream& in, Date& d);
Date.cpp
- #include"Date.h"
- Date::Date(int year, int month, int day)
- {
- _year = year;
- _month = month;
- _day = day;
- if (month < 1 || month>12 || day<1 || day>GetMonthDay(year, month))
- {
- cout << "非法日期" << endl;
- }
- }
- bool Date::operator<(const 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==(const Date& d) const
- {
- return _year == d._year && _month == d._month && _day == d._day;
- }
- bool Date::operator<=(const Date& d) const
- {
- return *this < d || *this == d;
- }
-
- bool Date::operator>(const Date& d) const
- {
- return !(*this <= d);
- }
- bool Date::operator>=(const Date& d) const
- {
- return !(*this < d);
- }
- bool Date::operator!=(const Date& d) const
- {
- return !(*this == d);
- }
- Date& Date::operator+=(int day)
- {
- if (day >= 0)
- {
- this->_day += day;
- while (_day > GetMonthDay(_year,_month))
- {
- _day -= GetMonthDay(_year, _month);
- _month++;
- if (_month == 13)
- {
- _month = 1;
- _year++;
- }
- }
- }
- else
- {
- *this -= (-day);
- }
- return *this;
- }
- Date& Date::operator-=(int day)
- {
- if (day >= 0)
- {
- this->_day -= day;
- while (_day <= 0)
- {
- _month--;
- _day += GetMonthDay(_year, _month);
- if (_month == 0)
- {
- _month = 12;
- _year--;
- }
- }
- }
- else
- {
- *this += (-day);
- }
- return *this;
- }
- Date Date::operator+(int day) const
- {
- Date tmp(*this);
- /*if (day >= 0)
- {
- tmp._day += day;
- while (tmp._day > GetMonthDay(tmp._year,tmp._month))
- {
- tmp._day -= GetMonth(tmp._month);
- tmp._month++;
- if (tmp._month == 13)
- {
- tmp._month = 1;
- tmp._year++;
- }
- }
- }
- else
- {
- *this - (-day);
- }*/
- tmp += day;
- return tmp;
- }
- Date Date::operator-(int day) const
- {
- Date tmp(*this);
- /*if (day >= 0)
- {
- tmp._day -= day;
- while (tmp._day < 0)
- {
- tmp._day += GetMonthDay(tmp._year,tmp._month);
- tmp._month--;
- if (tmp._month == 0)
- {
- tmp._month = 12;
- tmp._year--;
- }
- }
- }
- else
- {
- *this + (-day);
- }*/
- tmp -= day;
- return tmp;
- }
-
-
- Date& Date::operator++()
- {
- *this += 1;
- return *this;
- }
-
- Date Date::operator++(int)
- {
- Date tmp(*this);
- *this += 1;
- return tmp;
- }
-
- Date& Date::operator--()
- {
- *this -= 1;
- return *this;
- }
- Date Date::operator--(int)
- {
- Date tmp(*this);
- *this -= 1;
- return tmp;
- }
-
- int Date::operator-(const Date& d) const
- {
- int flag = 1;
- Date max = *this;
- Date min = d;
- if (max < min)
- {
- flag = -1;
- max = d;
- min = *this;
- }
- int n = 0;
- while (max != min)
- {
- ++min;
- ++n;
- }
- return n * flag;
- }
-
- void Date::Print() const
- {
- cout << _year << "/" << _month << "/" << _day << endl;
- }
- int Date::GetMonthDay(int year, int month) const
- {
- static int MonthDay[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
- if (month == 2 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
- {
- return 29;
- }
- return MonthDay[month];
- }
-
- ostream& operator<<(ostream& out, const Date& d)
- {
- out << d._year << "/" << d._month << "/" << d._day << endl;
- return out;
- }
-
- istream& operator>>(istream& in, Date& d)
- {
- in >> d._year >> d._month >> d._day;
- return in;
- }
Test.cpp
- #include"Date.h"
- int main()
- {
- Date d1(2023, 10, 10);
- Date d2(2000, 2, 16);
- cout << d1 << d2;
- cin >> d1 >> d2;
- cout << d1 << d2;
- }