赞
踩
目录
在学习了类的6个默认成员函数后,我们现在动手实现一个日期类,以便巩固所学知识。
- #include<iostream>
- using namespace std;
- class Date
- {
- public:
- // 获取某年某月的天数
- int GetMonthDay(int year, int month);
- // 全缺省的构造函数
- Date(int year = 1, int month = 1,int day = 1);
- // 拷贝构造函数
- // d2(d1)
- Date(const Date& d);
- // 赋值运算符重载
- // d2 = d3 -> d2.operator=(&d2, d3)
- Date& operator=(const Date& d);
- // 析构函数
- ~Date();
- //打印函数
- void Print()const;
-
- //<运算符重载
- 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);
-
- // 日期+=天数
- Date& operator+=(int day);
- // 日期+天数
- Date operator+(int day);
- // 日期-=天数
- Date& operator-=(int day);
- // 日期-天数
- Date operator-(int day);
-
-
- // 前置++
- Date& operator++();
- // 后置++
- Date operator++(int);
- // 前置--
- Date& operator--();
- // 后置--
- Date operator--(int);
-
- //日期-日期 返回天数
- int operator-(const Date& d);
-
- private:
- int _year;
- int _month;
- int _day;
- };

需要注意的地方有两点:
1️⃣为什么要用const static定义数组?
1.static:这意味着monthArray数组是一个静态局部变量。静态局部变量只在程序运行时初始化一次,并且它的生命周期将持续到程序结束,而不是每次函数调用时重新创建。在这个上下文中,这意味着无论这个GetMonthDay函数被调用多少次,monthArray数组都只会被初始化一次,提高了效率并节省了内存。
2.const 这表示monthArray数组是常量,其内容在定义后不可更改。由于数组存储的是每个月的天数,这是一个固定不变的数据(除了二月可能因闰年而变化),所以声明为const可以防止意外修改,并允许编译器进行一些优化。
2️⃣if判断条件要先写month == 2,判断是否是2月,接着才去判断是否是闰年。因为如果一个月不是二月,就没必要判断是否为闰年了。
图解:
- int Date::GetMonthDay(int year, int month)
- {
- const static int monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
- if ((month == 2) // 0 1 2 3 4 5 6 7 8 9 10 11 12
- && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
- {
- return 29;
- }
- return monthArray[month];
- }
综合起来,const static int monthArray[13]
表示一个存储每月天数的只读数组,在整个程序执行期间保持不变,而且在整个类内部可访问。这样设计既保证了数据的安全性,也提高了程序的运行效率。
注意:当函数的声明和定义分离时,我们给初始值的时候要在.h(声明)去给,不能放到.cpp(定义)去给
- 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;
- exit(-1);
- }
- }
- Date::Date(const Date& d)
- {
- _year = d._year;
- _month = d._month;
- _day = d._day;
- }
- void Date::Print()const
- {
- cout << _year << "年" << _month << "月" << _day << "日" << endl;
- }
所以这个函数的作用:打印日期对象的信息,但并不改变日期对象的状态。
- 若_day加了整数以后,<=该月的最大天数,则不需要修改,直接返回该日期
- 若_day加了整数以后,>=该月的最大天数,则用和之后的_day减去该月的最大天数,之后++_month
- 如果_month==13,则需要++_year,把_month置为1。如果_month未满13,则重复第二步过程,直到不满足循环条件为止。
- Date& Date::operator+=(int day)
- {
- if (day < 0)
- {
- return *this -= (-day);
- }
- //_day是哪年哪月哪天的天数,day是要加的天数
- _day += day;
- while (_day > GetMonthDay(_year,_month))
- {
- //加完之后的日期天数,减了该月应当有的天数,之后++_month
- _day -= GetMonthDay(_year, _month);
- ++_month;
- if (_month == 13)
- {
- ++_year;
- _month = 1;
- }
- }
- return *this;
- }

注意:当需要加的天数为负数时,则需要复用-=运算符重载。
+和+=的区别在于,是否会改变操作数原本的值。
图解:
还有一点需要注意的是,日期+=天数的返回值是引用&返回,由上面的图解可以知道,+=操作符会改变操作数原来的值,所以+=运算符重载函数返回时,*this对象(传进来的初始值)出了函数作用域还在,没有被销毁,使用引用提高效率。
但是+运算符不一样,日期+天数不会改变原操作数的值,所以this指针指向的对象出了函数作用域就被销毁了,不能使用传引用返回。这时使用拷贝构造创建一个tmp对象,直接改变拷贝的那个对象tmp并返回,以传值返回的方式返回。
图解:
- Date Date::operator+(int day)
- {
- Date tmp(*this);// 拷贝构造tmp,用于返回
- tmp += day; // 复用operator+=
- return tmp;
- }
还有一点需要注意的是:为什么要用+复用+=,而不是+=复用+:
对于-=运算符重载函数,进来先用初始值减掉要求的天数,若减去后得到的天数>0,则直接返回该日期。若减去后的天数<=0,则表明该日期不合法,需要调整,逻辑如下:
1.当_day<0时,--_month
2.若_month此时为0,则需要向年借位,--_year,并且重新把_month置为12,并将减去后得到的天数与该月应有的天数相加,除非_day>0,则需要像下图一样反复借位。
若_month不为0,只需要将减去后得到的天数与该月应有的天数相加,直到_day>0,否则循环上述条件。
3.最后需要返回*this,跟+=运算符同理,this指向的对象出了作用域还在,使用传引用返回。
- Date& Date::operator-=(int day)
- {
- if (day < 0)
- {
- return *this += -(day);
- }
- _day -= day;
- while (_day <= 0)
- {
- --_month;
- if (_month == 0)
- {
- --_year;
- _month = 12;
- }
- _day += GetMonthDay(_year,_month);
- }
- return *this;
- }

注意:当需要减的天数为负数时,转而调用+=运算符重载函数。
和+运算符的重载类似,-运算符的重载可以直接复用上面的-=运算符重载函数的实现。
- Date Date::operator-(int day)
- {
- Date tmp(*this);// 拷贝构造tmp,用于返回
- tmp -= day;// 复用operator-=
- return tmp;
- }
注意:-=运算符的重载函数采用的是引用返回,而-运算符的重载函数的返回值是传值返回,因为 -运算符重载函数中的tmp对象出了函数作用域被销毁了,所以不能使用引用返回。
直接复用+=运算符的重载函数
- Date& Date::operator++()
- {
- *this += 1;// 复用operator+=
- return *this;
- }
我们可以看出前置++和后置++的运算符重载是及其相似的,为了区分,给个int作为占位符也同样作为区分的标准,有参的是后置++,无参的为前置++。
- Date Date::operator++(int)
- {
- Date tmp(*this);// 拷贝构造tmp,用于返回
- *this += 1;// 复用operator+=
- return tmp;
- }
注意:后置++需要返回+1之前的值,调用拷贝构造创建的tmp对象保存++之前的初始值,接着再让this指向的对象+1,返回tmp(对象+1之前的初始值),由此我们可知返回时应用传值返回,不能用传引用返回(出了作用域tmp就销毁了)。
无参的--为前置--,传引用返回,直接复用前面的-=运算符的重载函数。
- Date& Date::operator--()
- {
- *this -= 1;// 复用operator-=
- return *this;
- }
有参的--为后置--,传值返回,直接复用前面的 -=运算符的重载函数。
- Date Date::operator--(int)
- {
- Date tmp(*this);// 拷贝构造tmp,用于返回
- *this -= 1;// 复用operator-=
- return tmp;
- }
只需要实现<和==的运算符重载函数,其他的比较函数直接复用即可(赋值=运算符重载除外)。
先判断_year是否小于d._year,条件为true,则判断_month是否小于d._month,对于_day同理。
这其中的继续条件是在年相等或者月相等的条件下,继而比较日是否小于,若不满足条件则返回false。
- bool Date::operator<(const Date&d)
- {
- if (_year < d._year)
- {
- return true;
- }
- if (_year == d._year && _month < d._month)
- {
- return true;
- }
- if (_year == d._year && _month == d._month && _day < d._day)
- {
- return true;
- }
- else
- {
- return false;
- }
- }

若年月日均相等则条件为真,其中一个不等就为false。
- bool Date::operator==(const Date& d)
- {
- return _year == d._year
- && _month == d._month
- &&_day == d._day;
- }
这里复用了==运算符的重载函数,首先计算 *this == d
,即当前对象与传入对象 d
是否相等,然后对结果取反(!
),如果两者原本相等,则结果为假(false),表示不相等;如果不相等,则结果为真(true),同样表示不相等。
- bool Date::operator!=(const Date& d)
- {
- return !(*this == d);
- }
复用了<和==的运算符重载函数,条件为true只需满足其中一个即可
- 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& Date::operator=(const Date& d)
- {
- if (*this != d)
- {
- _year = d._year;
- _month = d._month;
- _day = d._day;
- }
- return *this;
- }
找到两个日期中的较大和较小者,然后通过不断递增较小的日期直到与较大的日期相等,计算出两者之间的天数差,并根据初始比较结果确定正负号。
- int Date::operator-(const Date& d)
- {
- Date max = *this;// 假设第一个日期较大
- Date min = d;// 假设第二个日期较小
- int flag = 1;// 此时结果应该为正值
-
- if (*this < d)
- {// 假设错误,纠正
- max = d;
- min = *this;
- flag = -1;//改为负值
- }
- int n = 0;
- while (min!= max)
- {
- ++min;//较小的日期++
- ++n;//相差的总天数
- }
- return n * flag;
- }

逻辑:
首先创建了两个临时变量
max
和min
,将当前对象*this
的值赋给max
,传入参数d
的值赋给min
。初始化一个整型变量flag
为1,这个标志位将在最后的结果中决定返回值的正负,表示是从max
日期到min
日期的方向。判断当前对象
*this
是否小于传入参数d
。如果是,则交换max
和min
的值,并将flag
设置为 -1,这意味着我们要计算的是从较小的日期(min
)到较大的日期(max
)之间的天数差。初始化一个计数器
n
为0,用于记录相差的天数。使用一个循环来递增
min
直到它等于max
。每次循环中,min
会增加一天,并且计数器n
也会增加1,这样就统计出了从min
到max
所经过的天数。循环结束后,返回
n * flag
。由于之前已经通过flag
标记了方向,所以无论开始时是哪一天在前,都会得到正确的天数差。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/216099
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。