C++类和对象(二) - 日期类的实现

x33g5p2x  于2021-11-09 转载在 C/C++  
字(4.1k)|赞(0)|评价(0)|浏览(426)

前言:本文将介绍Date类的具体实现,通过日期类的简单实现帮助我们完整复习一遍类的“六大默认成员函数”。

1.日期类的定义

  1. class Date
  2. {
  3. public:
  4. // 获取某年某月的天数
  5. int GetMonthDay(int year, int month);
  6. // 全缺省的构造函数
  7. Date(int year=1988, int month=1, int day=1);
  8. // 拷贝构造函数
  9. Date(const Date& d);
  10. // 赋值运算符重载
  11. // d2 = d3 -> d2.operator=(&d2, d3)
  12. Date& operator=(const Date& d);
  13. // 日期+=天数
  14. Date& operator+=(int day);
  15. // 日期+天数
  16. Date operator+(int day);
  17. // 日期-天数
  18. Date operator-(int day);
  19. // 日期-=天数
  20. Date& operator-=(int day);
  21. // 前置++
  22. Date& operator++();
  23. // 后置++
  24. Date& operator++(int);
  25. // 后置--
  26. Date& operator--(int);
  27. // 前置--
  28. Date& operator--();
  29. // >运算符重载
  30. bool operator>(const Date& d);
  31. // ==运算符重载
  32. bool operator==(const Date& d);
  33. // >=运算符重载
  34. bool operator>=(const Date& d);
  35. // <运算符重载
  36. bool operator<(const Date& d);
  37. // <=运算符重载
  38. bool operator<=(const Date& d);
  39. // !=运算符重载
  40. bool operator!=(const Date& d);
  41. // 日期-日期 返回两个日期之间相隔的具体天数
  42. int operator-(const Date& d);
  43. //定义一个函数打印看看
  44. void print()
  45. {
  46. cout << _year << " " << _month << " " << _day << endl;
  47. }
  48. private:
  49. int _year;
  50. int _month;
  51. int _day;
  52. };

2.日期类成员函数具体实现

2.1准确获取某年某月有多少天

这里考虑下闰年的情况,然后用数组简单映射下就好了

  1. int Date::GetMonthDay(int year, int month)
  2. {
  3. //记录每月天数
  4. int Month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  5. //遇见闰年,二月天数置29
  6. if (month == 2 && ((year %4 == 0 && year % 100 != 0) || (year % 400 == 0))) return 29;
  7. return Month[month];
  8. }

2.2日期类构造函数

  1. Date::Date(int year, int month, int day)
  2. {
  3. _year = year;
  4. _month = month;
  5. _day = day;
  6. //检测下日期是否合法
  7. if (month < 1 || month>12 || day<0 || day>GetMonthDay(year,month)) cout << "非法日期" << endl;
  8. }

2.3日期类拷贝构造

  1. Date::Date(const Date& d)
  2. {
  3. _year = d._year;
  4. _month = d._month;
  5. _day = d._day;
  6. }

2.4赋值运算符重载

类体外定义是成员函数返回值类型+类名::函数名(形参列表)

  1. Date& Date:: operator=(const Date& d)
  2. {
  3. _year = d._year;
  4. _month = d._month;
  5. _day = d._day;
  6. return *this; //这个可不能少,少了遇上连续赋值语句a=b=c会出错。
  7. }

2.5+=运算符重载

考虑越界情况

  • 如果_day加了整数以后,<=该月最大天数,则不需要修改,直接返回该日期.
  • 如果_day加了整数以后,>该月最大天数,则_day减去新的月所拥有的最大天数,然后该月加1,.
  • 如果执行了第二步后,_day仍大于新的月所拥有天数,继续执行第二步,并且循环.
  1. Date& Date:: operator+=(int day)
  2. {
  3. //先把加的天数算进来
  4. _day += day;
  5. //考虑加了这个天数之后,_day是否合法,不合法就给他转一下
  6. while (_day > GetMonthDay(_year, _month))
  7. {
  8. _day -= GetMonthDay(_year, _month);
  9. _month++;
  10. if (_month == 13)
  11. {
  12. _month = 1;
  13. _year++;
  14. }
  15. }
  16. return *this;
  17. }

2.6+运算符重载

复用!!!

  1. Date Date::operator+(int day)
  2. {
  3. //+和+=区别无非就是一个返回的是运算之后的结果,一个返回的是运算之前的结果
  4. Date tmp(*this);
  5. *this += day;
  6. return tmp;
  7. }

2.7-=运算符重载

考虑越界情况,与+=同理

  1. Date& Date::operator-=(int day)
  2. {
  3. _day -= day;
  4. while (_day < 1)
  5. {
  6. _month--;
  7. if (_month < 1)
  8. {
  9. _month = 12;
  10. _year--;
  11. }
  12. _day += GetMonthDay(_year, _month);
  13. }
  14. return *this;
  15. }

2.8+运算符重载

复用!!!

  1. Date Date::operator+(int day)
  2. {
  3. //+和+=区别无非就是一个返回的是运算之后的结果,一个返回的是运算之前的结果
  4. Date tmp(*this);
  5. *this += day;
  6. return tmp;
  7. }

2.9-运算符重载

复用!!!

  1. Date Date::operator-(int day)
  2. {
  3. Date tmp(*this);
  4. *this -= day;
  5. return tmp;
  6. }

2.10前置++运算符重载

复用!!!

  1. Date& Date::operator++()
  2. {
  3. *this += 1;
  4. return *this;
  5. }

2.11前置–运算符重载

复用!!!

  1. Date& Date::operator--()
  2. {
  3. *this -= 1;
  4. return *this;
  5. }

2.12后置-- 运算符重载

复用!!!

  1. Date& Date::operator--(int) //这个int用于区分前置还是后置
  2. {
  3. Date tmp(*this);
  4. *this -= 1;
  5. return tmp;
  6. }

2.13后置++运算符重载

复用!!!

  1. Date& Date::operator++(int) //这个int用于区分前置还是后置
  2. {
  3. Date tmp(*this);
  4. *this += 1;
  5. return tmp;
  6. }

2.14>运算符重载

  1. bool Date::operator>(const Date& d)
  2. {
  3. if (_year > d._year) return true;
  4. else if (_year == d._year && _month > d._month) return true;
  5. else if (_year == d._year && _month == d._month && _day > d._day) return true;
  6. return false;
  7. }

2.15==运算符重载

  1. bool Date::operator==(const Date& d)
  2. {
  3. if (_year == d._year && _month == d._month && _day == d._day) return true;
  4. return false;
  5. }

2.16>=运算符重载

复用!!!

  1. bool Date::operator>=(const Date& d)
  2. {
  3. if (*this > d || *this == d) return true;
  4. return false;
  5. }

2.17<运算符重载

复用!!!

  1. bool Date::operator<(const Date& d)
  2. {
  3. if (!(*this >= d)) return true;
  4. return false;
  5. }

2.18<=运算符重载

复用!!!

  1. bool Date::operator<=(const Date& d)
  2. {
  3. if (!(*this > d)) return true;
  4. return false;
  5. }

2.19!=运算符重载

复用!!!

  1. bool Date::operator!=(const Date& d)
  2. {
  3. return !(*this == d);
  4. }

2.20.计算两个日期之间的间隔天数,日期减去日期

直接从小的日期开始,通过while循环往上加,然后计数,直到等于大的日期为止。

计数结束时,所计的数字便是我们想要的间隔天数。

  1. int Date::operator-(const Date&d)
  2. {
  3. Date max(*this);
  4. Date min(d);
  5. int n = 1;
  6. if (max < min)
  7. {
  8. Date tmp(max);
  9. max = min;
  10. min = tmp;
  11. n = -1;
  12. }
  13. int count = 0;
  14. while (min<=max)
  15. {
  16. count++;
  17. min++;
  18. }
  19. return count*n;
  20. }

学以致用一波,精心为大家准备了几道日期类编程题,不妨练练手,完全相似的哦!!!

牛客:日期差值

牛客:计算一年的第几天

牛客:日期累加

牛客:打印日期

3.源码链接

点击跳转代码仓库

感谢您的阅读!!!如果内容对你有帮助的话,记得给我三连(点赞、收藏、关注)——做个手有余香的人。

相关文章