当前位置:   article > 正文

C++日期类的实现

C++日期类的实现

要实现一个日期类我们先考虑一下他有什么成员?

必须有的:年、月、日

需要显示写构造函数和析构函数吗?

Date类的成员类型都是内置类型,编译器不一定处理

我们可以显示的写一个全缺省的构造函数

  1. Date::Date(int year, int month, int day)
  2. {
  3. _year = year;
  4. _month = month;
  5. _day = day;
  6. }

成员都是内置类型,那么也就是没有资源需要清理也就不需要显示的写析构函数

接下来我们再来考虑下日期类一般会有什么方法?

1.拷贝构造函数

需要显示的写吗?

不需要,编译器会自动调用默认的拷贝构造

如果要显示的写拷贝构造函数该怎么实现?

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

拷贝构造函数名就是类名,只有一个参数,形参是类类型的引用

2.获取某个月的具体天数

  1. int Date::GetMonthDay(int year, int month)
  2. {
  3. static int MonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
  4. if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
  5. {
  6. return 29;
  7. }
  8. else
  9. {
  10. return MonthDay[month];
  11. }
  12. }

将数组的长度设置为13,这样数组的下标刚好就与月份能对应得上

别忘了,闰年的二月有29天

3.赋值运算符重载

编译器有默认的,可以不用显示写

那显示的如何实现?要注意什么?

  1. Date& Date::operator=(const Date& d)
  2. {
  3. //检查自赋值
  4. if (this == &d)
  5. {
  6. return *this;
  7. }
  8. _year = d._year;
  9. _month = d._month;
  10. _day = d._day;
  11. return *this;
  12. }

返回值类型:类类型的引用

形参:只有一个,形参类型也是类类型的引用

函数体中返回的是*this,this指针指向当前对象(当前日期),存放当前对象的地址,所以要返回当前对象就是返回this指针的解引用

4.各种运算符重载,不一一说明

想看详细一点的可以阅读:http://t.csdnimg.cn/ZE13g

里面一个核心的思想就是复用

5.直接输入、输出日期

  1. friend ostream& operator<<(ostream& os, Date& d)
  2. {
  3. os << d._year << "/" << d._month << "/" << d._day << endl;
  4. return os;
  5. }
  6. friend istream& operator>>(istream& is, Date& d)
  7. {
  8. is >> d._year >> d._month >> d._day;
  9. return is;
  10. }

需要注意的是他们不是日期类的成员函数,而是日期类的友元函数,返回值类型前面加了关键字friend来修饰,注意友元函数虽然可以访问类的所有成员,但是由于他本身不是类的成员函数也就没有了隐藏的this指针,所以需要传两个参数.

具体代码

头文件

  1. #pragma once
  2. #include<iostream>
  3. using namespace std;
  4. class Date
  5. {
  6. public:
  7. Date(int year = 0, int month = 0, int day = 0);
  8. // 拷贝构造函数
  9. // d2(d1)
  10. Date(const Date& d);
  11. // 获取某年某月的天数
  12. int GetMonthDay(int year, int month);
  13. // 赋值运算符重载
  14. // d2 = d3 -> d2.operator=(&d2, d3)
  15. Date& operator=(const Date& d);
  16. // 日期+=天数
  17. Date& operator+=(int day);
  18. // 日期+天数
  19. Date operator+(int day);
  20. // 日期-天数
  21. Date operator-(int day);
  22. // 日期-=天数
  23. Date& operator-=(int day);
  24. // 前置++
  25. Date& operator++();
  26. // 后置++
  27. Date operator++(int);
  28. // 后置--
  29. Date operator--(int);
  30. // 前置--
  31. Date& operator--();
  32. // >运算符重载
  33. bool operator>(const Date& d);
  34. // ==运算符重载
  35. bool operator==(const Date& d);
  36. // >=运算符重载
  37. bool operator >= (const Date& d);
  38. // <运算符重载
  39. bool operator < (const Date& d);
  40. // <=运算符重载
  41. bool operator <= (const Date& d);
  42. // !=运算符重载
  43. bool operator != (const Date& d);
  44. // 日期-日期 返回天数
  45. int operator-(const Date& d);
  46. void print()const;
  47. // 析构函数(日期类无需清理资源,析构函数不必显示写)
  48. //~Date()
  49. //{
  50. //cout << "~Date()" << endl;
  51. //}
  52. friend ostream& operator<<(ostream& os, Date& d)
  53. {
  54. os << d._year << "/" << d._month << "/" << d._day << endl;
  55. return os;
  56. }
  57. friend istream& operator>>(istream& is, Date& d)
  58. {
  59. is >> d._year >> d._month >> d._day;
  60. return is;
  61. }
  62. private:
  63. int _year, _month, _day;
  64. };

函数的具体实现

  1. #include"Date.h"
  2. Date::Date(int year, int month, int day)
  3. {
  4. _year = year;
  5. _month = month;
  6. _day = day;
  7. }
  8. Date::Date(const Date& d)
  9. {
  10. _year = d._year;
  11. _month = d._month;
  12. _day = d._day;
  13. }
  14. // 赋值运算符重载
  15. // d2 = d3 -> d2.operator=(&d2, d3)
  16. Date& Date::operator=(const Date& d)
  17. {
  18. //检查自赋值
  19. if (this == &d)
  20. {
  21. return *this;
  22. }
  23. _year = d._year;
  24. _month = d._month;
  25. _day = d._day;
  26. return *this;
  27. }
  28. int Date::GetMonthDay(int year, int month)
  29. {
  30. static int MonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
  31. if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
  32. {
  33. return 29;
  34. }
  35. else
  36. {
  37. return MonthDay[month];
  38. }
  39. }
  40. void Date::print()const
  41. {
  42. cout << _year << "年" << _month << "月" << _day << "日" << endl;
  43. }
  44. // >运算符重载
  45. bool Date::operator>(const Date& d)
  46. {
  47. if (_year > d._year)
  48. {
  49. return true;
  50. }
  51. else if (_year == d._year)
  52. {
  53. if (_month > d._month)
  54. {
  55. return true;
  56. }
  57. else if (_month == d._month)
  58. {
  59. if (_day > d._day)
  60. {
  61. return true;
  62. }
  63. }
  64. }
  65. return false;
  66. }
  67. // ==运算符重载
  68. bool Date::operator==(const Date& d)
  69. {
  70. return _year == d._year && _month == d._month && _day == d._day;
  71. }
  72. // >=运算符重载
  73. bool Date::operator >= (const Date& d)
  74. {
  75. return (*this > d || *this == d);
  76. }
  77. // <运算符重载
  78. bool Date::operator < (const Date& d)
  79. {
  80. return !(*this >= d);
  81. }
  82. // <=运算符重载
  83. bool Date::operator <= (const Date& d)
  84. {
  85. return !(*this > d);
  86. }
  87. // !=运算符重载
  88. bool Date::operator != (const Date& d)
  89. {
  90. return !(*this == d);
  91. }
  92. // 日期+=天数
  93. Date& Date::operator+=(int day)
  94. {
  95. _day += day;
  96. while (_day > GetMonthDay(_year, _month))
  97. {
  98. _month++;
  99. if (_month == 13)
  100. {
  101. _year++;
  102. _month = 1;
  103. }
  104. _day -= GetMonthDay(_year, _month);
  105. }
  106. return *this;
  107. }
  108. // 日期+天数
  109. Date Date::operator+(int day)
  110. {
  111. Date tmp;
  112. tmp += day;
  113. return tmp;
  114. }
  115. // 日期-天数
  116. Date Date::operator-(int day)
  117. {
  118. Date tmp;
  119. tmp -= day;
  120. return tmp;
  121. }
  122. // 日期-=天数
  123. Date& Date::operator-=(int day)
  124. {
  125. _day -= day;
  126. while (_day <= 0)
  127. {
  128. _month--;
  129. if (_month == 0)
  130. {
  131. _month = 12;
  132. _year--;
  133. }
  134. _day += GetMonthDay(_year, _month);
  135. }
  136. return *this;
  137. }
  138. // 前置++
  139. Date& Date::operator++()
  140. {
  141. *this += 1;
  142. return *this;
  143. }
  144. // 后置++
  145. Date Date::operator++(int)
  146. {
  147. Date tmp(*this);
  148. *this += 1;
  149. return tmp;
  150. }
  151. // 后置--
  152. Date Date::operator--(int)
  153. {
  154. Date tmp(*this);
  155. *this -= 1;
  156. return tmp;
  157. }
  158. // 前置--
  159. Date& Date::operator--()
  160. {
  161. *this -= 1;
  162. return *this;
  163. }
  164. // 日期-日期 返回天数
  165. int Date::operator-(const Date& d)
  166. {
  167. Date max(*this);
  168. Date min(d);
  169. int flag = 1;
  170. int count = 0;
  171. if (*this < d)
  172. {
  173. flag = -1;
  174. max = d;
  175. min = *this;
  176. }
  177. while (min < max)
  178. {
  179. min++;
  180. count++;
  181. }
  182. return count * flag;
  183. }

测试

  1. #include"Date.h"
  2. void Test1()
  3. {
  4. Date d1(2023, 10, 5), d2(2024, 6, 10);
  5. cout << (d1 == d2) << endl;
  6. cout << (d1 != d2) << endl;
  7. cout << (d1 <= d2) << endl;
  8. cout << (d1 >= d2) << endl;
  9. cout << (d1 < d2) << endl;
  10. cout << (d1 > d2) << endl;
  11. }
  12. void Test2()
  13. {
  14. Date d1(2024, 6, 10), d2(2024, 6, 05);
  15. /*Date d3 = d1--;
  16. Date d4 = d2++;
  17. d3.print();
  18. d4.print();
  19. Date d5 = --d3;
  20. Date d6 = ++d4;
  21. d5.print();
  22. d6.print();*/
  23. int num = d1 - d2;
  24. cout << num << endl;
  25. cout << d1;
  26. cin >> d2;
  27. }
  28. int main()
  29. {
  30. Test2();
  31. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/742882
推荐阅读
相关标签
  

闽ICP备14008679号