日期类的实现C++(笔记)

打印 上一主题 下一主题

主题 990|帖子 990|积分 2970

我们前面已经基本学习完了C++的类和对象,并且我们也使用了日期类的部分来在类和对象部分举例,这篇文章将实现一个小的日期类:
首先我们先在VS中创建好三个文件:

Test.cpp用来测试我们的日期类是否正确。
对于一个日期类,在我们一样寻常生活,我们可以对一个日期 加 或 减整数天,比如对2004年2月1日加上3天,就得到了2004年2月4日,同理减法。但我们对一个日期举行乘除也就没意义了,以是我们在举行运算符重载就不需要乘除。
我们先来看看我们在头文件中都需要实现什么功能:
  1. #pragma once
  2. #include <iostream>
  3. #include <assert.h>
  4. using namespace std;
  5. class Date
  6. {
  7.         friend ostream& operator<<(ostream& out, const Date& d);
  8.         friend istream& operator>>(istream& in, Date& d);
  9. public:
  10.         Date(int year = 2004, int month = 2, int day = 2);
  11.         void Print();
  12.         Date(const Date& d);
  13.         ~Date()
  14.         {
  15.                 _year = 0;
  16.                 _month = 0;
  17.                 _day = 0;
  18.         }
  19.         int GetMonthDay(int year, int month)
  20.         {
  21.                 assert(month > 0 && month < 13);
  22.                 static int MonthArr[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
  23.                 if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
  24.                 {
  25.                         return 29;
  26.                 }
  27.                 return MonthArr[month];
  28.         }
  29.         bool CheckDate();
  30.         bool operator==(const Date& d)const;
  31.         bool operator!=(const Date& d)const;
  32.         bool operator<(const Date& d)const;
  33.         bool operator<=(const Date& d)const;
  34.         bool operator>(const Date& d)const;
  35.         bool operator>=(const Date& d)const;
  36.         Date& operator=(const Date& d);
  37.         Date operator++();
  38.         Date operator--();
  39.         Date operator++(int);
  40.         Date operator--(int);
  41.         Date& operator+=(int day);
  42.         Date operator+(int day)const;
  43.         Date& operator-=(int day);
  44.         Date operator-(int day)const;
  45.         int operator-(const Date& d)const;
  46. private:
  47.         int _year;
  48.         int _month;
  49.         int _day;
  50. };
复制代码
其实内容上大部分都是对一个日期举行 加 和 减 的操作,其次就是判断日期巨细操作等等。
 对于Date的构造函数,我想应该很简单,这里直接展示代码:
  1. Date::Date(int year, int month, int day)
  2.         :_year(year)
  3.         ,_month(month)
  4.         ,_day(day)
  5. {
  6.         if (!CheckDate())
  7.         {
  8.                 cout << "非法日期>:" << _year << "/" << _month << "/" << _day << endl;
  9.         }
  10. }
复制代码
这里举行初始化列表,并且缺省参数规定给在函数声明时,因此这就是Date构造函数。这里尚有一个检查日期是否正当的函数,也比较简单,直接展示:
  1. bool Date::CheckDate()
  2. {
  3.         if (_month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month))
  4.         {
  5.                 return false;
  6.         }
  7.         return true;
  8. }
复制代码
 接下来是拷贝构造函数,也是很简单:
  1. Date::Date(const Date& d)
  2. {
  3.         _year = d._year;
  4.         _month = d._month;
  5.         _day = d._day;
  6. }
复制代码
 对于GetMonthDay这个函数,我相信大家在学习C语言的时间,肯定写过这个函数,大家认真看一下肯定能懂,我们主要来解说重载:
  1.     bool operator==(const Date& d)const;
  2.         bool operator!=(const Date& d)const;
  3.         bool operator<(const Date& d)const;
  4.         bool operator<=(const Date& d)const;
  5.         bool operator>(const Date& d)const;
  6.         bool operator>=(const Date& d)const;
复制代码
对于这几个判断的运算符,我们其实写上2个就行了,由于写出了两个,其他的判断运算符基本上是复用,比如我们写了< 和 ==,我们通过返回取反即可,以<为例:
  1. bool Date::operator<(const Date& d)const
  2. {
  3.         if (_year < d._year)
  4.         {
  5.                 return true;
  6.         }
  7.         else if (_year == d._year)
  8.         {
  9.                 if (_month < d._month)
  10.                 {
  11.                         return true;
  12.                 }
  13.                 else if (_month == d._month)
  14.                 {
  15.                         if (_day < d._day)
  16.                         {
  17.                                 return true;
  18.                         }
  19.                 }
  20.         }
  21.         return false;
  22. }
复制代码
由于类的成员函数默认第一个参数是this,并且<左边为第一个参数,右边为第二个参数。假如this的_year都比d的_year小,那么<就返回true,假如两个年份相等了,再比较月、日等等,假如条件都不符合,最终返回false即可。这就是一个<运算符的重载。
我们再来看==的重载:
  1. bool Date::operator==(const Date& d)const
  2. {
  3.         return _year == d._year && _month == d._month
  4.                 && _day == d._day;
  5. }
复制代码
 假如两个参数的年 月 日均相等,返回true,假如有一个不相等,那么结果就是false,返回false。
当我们写完了这两个运算符的重载之后,后面就简单许多了。
例如我直接展示<= 和 > : 
  1. bool Date::operator<=(const Date& d)const
  2. {
  3.         return (*this < d) || (*this == d);
  4. }
  5. bool Date::operator>(const Date& d)const
  6. {
  7.         return !(*this <= d);
  8. }
复制代码
假如符合 < 大概 == 的其中一个,那么<= 就建立;对于>来说我们只需返回<=的取反即可,其他的操作符重载类似,这里就不展示代码了。
我们再来看对于日期的+ - 相干运算符的重载:
  1.     Date& operator=(const Date& d);
  2.         Date operator++();
  3.         Date operator--();
  4.         Date operator++(int);
  5.         Date operator--(int);
  6.         Date& operator+=(int day);
  7.         Date operator+(int day)const;
  8.         Date& operator-=(int day);
  9.         Date operator-(int day)const;
  10.         int operator-(const Date& d)const;
复制代码
先来看第一个赋值运算符的重载。我们都知道,在C语言中,我们可以举行这段操作:
int a = b = c = 2;假设b,c已经声明为(int),由于他是从右向左,c = 2的结果是2,然后给b,b = 2
的结果是2,然后给a。因此在类的操作符重载的时间,我们也需要实现。
对于赋值运算符的重载:
  1. Date& Date::operator=(const Date& d)
  2. {
  3.         _year = d._year;
  4.         _month = d._month;
  5.         _day = d._day;
  6.         return *this;
  7. }
复制代码
 第一个操作数在左边,即为我们的this,第二个操作数在右边,即为我们的d,代码也很简单,最终返回*this的引用,由于this生命周期并不在这个函数内里,因此我们不消担心空引用问题。
对于+、+=、++(-、-=、--)运算符重载,这里只展示+相干的,由于 - 的与 + 的类似,先来看+=:
  1. Date& Date::operator+=(int day)
  2. {
  3.         if (day < 0)
  4.         {
  5.                 return *this -= -day;
  6.         }
  7.         _day += day;
  8.         while (_day > GetMonthDay(_year, _month))
  9.         {
  10.                 _day -= GetMonthDay(_year, _month);
  11.                 _month++;
  12.                 if (_month == 13)
  13.                 {
  14.                         _year++;
  15.                         _month = 1;
  16.                 }
  17.         }
  18.         return *this;
  19. }
复制代码
 我们都知道,每个月的天数不一样,并且一旦天数达到了上限,我们就要给月+1,一旦月份加到了13,我们就要给年+1,并且将月份修改为1,由于12月+1就到了来岁的1月。
假如这里我们 += 的是负数,我们就去调用 -= -day,例如要加上 -100天其实就是减100天。
假如不是负数,我们先直接给_day把天数加上,然后假如_day大于对应月的天数,我们就需要给_day减去该月的天数,然后_month++,假如_month都加到了13,此时修改为1即可。最后不要忘了返回*this。
看完了 += 后, 其实 + 的运算就十分简单了:
  1. Date Date::operator+(int day)const
  2. {
  3.         Date tmp = *this;
  4.         tmp += day;
  5.         return tmp;
  6. }
复制代码
对于整形来说, c = a + b; a和b的值都不会变,因此我们在函数中创建一个tmp = *this,此时tmp的日期和*this的日期就同等了,然后举行 += 天数运算(我们刚刚写过),最后返回tmp即可;注意,这里不能返回tmp的引用,由于一旦出了作用域,tmp烧毁,那么我们得到的就是空引用。
最后我们来看 ++ 运算符的重载:
  1. //前置++
  2. Date Date::operator++()
  3. {
  4.         *this += 1;
  5.         return *this;
  6. }
  7. //后置++
  8. Date Date::operator++(int)
  9. {
  10.         Date tmp = *this;
  11.         *this += 1;
  12.         return tmp;
  13. }
复制代码
这个也比较简单,我们之前已经讲过,假如要变成后置++,只需要加一个int参数即可。而 前置++ 与 后置++ 的唯一区别就是前置++用的时间已经加1了,后置是先使用没加之前的值,在加1,我相信同砚们能看懂这两段代码。
接下来我们来实现一下两个日期相减,得到两个日期之间相差的天数:
  1. int Date::operator-(const Date& d)const
  2. {
  3.         int flag = -1;
  4.         Date min = *this;
  5.         Date max = d;
  6.         if (min > max)
  7.         {
  8.                 min = d;
  9.                 max = *this;
  10.                 flag = 1;
  11.         }
  12.         int n = 0;
  13.         while (1)
  14.         {
  15.                 if (min == max)
  16.                         break;
  17.                 ++min;
  18.                 n++;
  19.         }
  20.         return n*flag;
  21. }
复制代码
我相信很多同砚在写这个重载的时间,肯定都是一头雾水,想来想去也不知道该怎么计算,由于不仅有平年与闰年的区别,更复杂的是,每个月的天数也是不一样的。但是假如我们使用到之前的重载运算符,那么上面的代码你肯定能看懂。
由于左操作数是*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企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

乌市泽哥

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表