我们前面已经基本学习完了C++的类和对象,并且我们也使用了日期类的部分来在类和对象部分举例,这篇文章将实现一个小的日期类:
首先我们先在VS中创建好三个文件:
Test.cpp用来测试我们的日期类是否正确。
对于一个日期类,在我们一样寻常生活,我们可以对一个日期 加 或 减整数天,比如对2004年2月1日加上3天,就得到了2004年2月4日,同理减法。但我们对一个日期举行乘除也就没意义了,以是我们在举行运算符重载就不需要乘除。
我们先来看看我们在头文件中都需要实现什么功能:
- #pragma once
- #include <iostream>
- #include <assert.h>
- using namespace std;
- class Date
- {
- friend ostream& operator<<(ostream& out, const Date& d);
- friend istream& operator>>(istream& in, Date& d);
- public:
- Date(int year = 2004, int month = 2, int day = 2);
- void Print();
- Date(const Date& d);
- ~Date()
- {
- _year = 0;
- _month = 0;
- _day = 0;
- }
- int GetMonthDay(int year, int month)
- {
- assert(month > 0 && month < 13);
- static int MonthArr[13] = { -1,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;
- }
- return MonthArr[month];
- }
- bool CheckDate();
- 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=(const Date& d);
- Date operator++();
- Date operator--();
- Date operator++(int);
- Date operator--(int);
- Date& operator+=(int day);
- Date operator+(int day)const;
- Date& operator-=(int day);
- Date operator-(int day)const;
- int operator-(const Date& d)const;
- private:
- int _year;
- int _month;
- int _day;
- };
复制代码 其实内容上大部分都是对一个日期举行 加 和 减 的操作,其次就是判断日期巨细操作等等。
对于Date的构造函数,我想应该很简单,这里直接展示代码:
- Date::Date(int year, int month, int day)
- :_year(year)
- ,_month(month)
- ,_day(day)
- {
- if (!CheckDate())
- {
- cout << "非法日期>:" << _year << "/" << _month << "/" << _day << endl;
- }
- }
复制代码 这里举行初始化列表,并且缺省参数规定给在函数声明时,因此这就是Date构造函数。这里尚有一个检查日期是否正当的函数,也比较简单,直接展示:
- bool Date::CheckDate()
- {
- if (_month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month))
- {
- return false;
- }
- return true;
- }
复制代码 接下来是拷贝构造函数,也是很简单:
- Date::Date(const Date& d)
- {
- _year = d._year;
- _month = d._month;
- _day = d._day;
- }
复制代码 对于GetMonthDay这个函数,我相信大家在学习C语言的时间,肯定写过这个函数,大家认真看一下肯定能懂,我们主要来解说重载:
- 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;
复制代码 对于这几个判断的运算符,我们其实写上2个就行了,由于写出了两个,其他的判断运算符基本上是复用,比如我们写了< 和 ==,我们通过返回取反即可,以<为例:
- bool Date::operator<(const Date& d)const
- {
- 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;
- }
复制代码 由于类的成员函数默认第一个参数是this,并且<左边为第一个参数,右边为第二个参数。假如this的_year都比d的_year小,那么<就返回true,假如两个年份相等了,再比较月、日等等,假如条件都不符合,最终返回false即可。这就是一个<运算符的重载。
我们再来看==的重载:
- bool Date::operator==(const Date& d)const
- {
- return _year == d._year && _month == d._month
- && _day == d._day;
- }
复制代码 假如两个参数的年 月 日均相等,返回true,假如有一个不相等,那么结果就是false,返回false。
当我们写完了这两个运算符的重载之后,后面就简单许多了。
例如我直接展示<= 和 > :
- bool Date::operator<=(const Date& d)const
- {
- return (*this < d) || (*this == d);
- }
- bool Date::operator>(const Date& d)const
- {
- return !(*this <= d);
- }
复制代码 假如符合 < 大概 == 的其中一个,那么<= 就建立;对于>来说我们只需返回<=的取反即可,其他的操作符重载类似,这里就不展示代码了。
我们再来看对于日期的+ - 相干运算符的重载:
- Date& operator=(const Date& d);
- Date operator++();
- Date operator--();
- Date operator++(int);
- Date operator--(int);
- Date& operator+=(int day);
- Date operator+(int day)const;
- Date& operator-=(int day);
- Date operator-(int day)const;
- int operator-(const Date& d)const;
复制代码 先来看第一个赋值运算符的重载。我们都知道,在C语言中,我们可以举行这段操作:
int a = b = c = 2;假设b,c已经声明为(int),由于他是从右向左,c = 2的结果是2,然后给b,b = 2
的结果是2,然后给a。因此在类的操作符重载的时间,我们也需要实现。
对于赋值运算符的重载:
- Date& Date::operator=(const Date& d)
- {
- _year = d._year;
- _month = d._month;
- _day = d._day;
- return *this;
- }
复制代码 第一个操作数在左边,即为我们的this,第二个操作数在右边,即为我们的d,代码也很简单,最终返回*this的引用,由于this生命周期并不在这个函数内里,因此我们不消担心空引用问题。
对于+、+=、++(-、-=、--)运算符重载,这里只展示+相干的,由于 - 的与 + 的类似,先来看+=:
- Date& Date::operator+=(int day)
- {
- if (day < 0)
- {
- return *this -= -day;
- }
- _day += day;
- while (_day > GetMonthDay(_year, _month))
- {
- _day -= GetMonthDay(_year, _month);
- _month++;
- if (_month == 13)
- {
- _year++;
- _month = 1;
- }
- }
- return *this;
- }
复制代码 我们都知道,每个月的天数不一样,并且一旦天数达到了上限,我们就要给月+1,一旦月份加到了13,我们就要给年+1,并且将月份修改为1,由于12月+1就到了来岁的1月。
假如这里我们 += 的是负数,我们就去调用 -= -day,例如要加上 -100天其实就是减100天。
假如不是负数,我们先直接给_day把天数加上,然后假如_day大于对应月的天数,我们就需要给_day减去该月的天数,然后_month++,假如_month都加到了13,此时修改为1即可。最后不要忘了返回*this。
看完了 += 后, 其实 + 的运算就十分简单了:
- Date Date::operator+(int day)const
- {
- Date tmp = *this;
- tmp += day;
- return tmp;
- }
复制代码 对于整形来说, c = a + b; a和b的值都不会变,因此我们在函数中创建一个tmp = *this,此时tmp的日期和*this的日期就同等了,然后举行 += 天数运算(我们刚刚写过),最后返回tmp即可;注意,这里不能返回tmp的引用,由于一旦出了作用域,tmp烧毁,那么我们得到的就是空引用。
最后我们来看 ++ 运算符的重载:
- //前置++
- Date Date::operator++()
- {
- *this += 1;
- return *this;
- }
- //后置++
- Date Date::operator++(int)
- {
- Date tmp = *this;
- *this += 1;
- return tmp;
- }
复制代码 这个也比较简单,我们之前已经讲过,假如要变成后置++,只需要加一个int参数即可。而 前置++ 与 后置++ 的唯一区别就是前置++用的时间已经加1了,后置是先使用没加之前的值,在加1,我相信同砚们能看懂这两段代码。
接下来我们来实现一下两个日期相减,得到两个日期之间相差的天数:
- int Date::operator-(const Date& d)const
- {
- int flag = -1;
- Date min = *this;
- Date max = d;
- if (min > max)
- {
- min = d;
- max = *this;
- flag = 1;
- }
- int n = 0;
- while (1)
- {
- if (min == max)
- break;
- ++min;
- n++;
- }
- return n*flag;
- }
复制代码 我相信很多同砚在写这个重载的时间,肯定都是一头雾水,想来想去也不知道该怎么计算,由于不仅有平年与闰年的区别,更复杂的是,每个月的天数也是不一样的。但是假如我们使用到之前的重载运算符,那么上面的代码你肯定能看懂。
由于左操作数是*this,右操作数是d,我们开始假设小的min日期为*this,大的max日期为d,假如假设建立,那么max - *this 将是一个负数,以是我们开始定义flag 为 -1;假如min > max。那么我们交换min 与 max,并把flag改为1。然后我们定义一个n来计数,假如min == max了,我们就break冲破循环,假如min != max,那么我们++min并且再 n++;直到min与max相等,那么最终的n值就是二者相差的天数,最后返回 n*flag即可。
日期类的实现总的来说还是挺简单的,只要我们认真去钻研,都可以自己来实现。以上内容假如有错误的地方,欢迎各位批评指正!!!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |