• 运算符重载之日期类的实现


    接上一篇文章,废话不多说,直接上代码

    Date.h

    1. #pragma once
    2. #include
    3. using namespace std;
    4. #include
    5. class Date
    6. {
    7. //友元函数声明
    8. friend ostream& operator<<(ostream& out, const Date& d);
    9. friend istream& operator>>(istream& in, Date& d);
    10. public:
    11. Date(int year = 1900, int month = 1, int day = 1);
    12. void Print()const;
    13. //GetMonthDay函数频繁调用,直接定义类里面,默认是inline
    14. int GetMonthDay(int year, int month)
    15. {
    16. assert(month > 0 && month < 13);
    17. static int monthDayArray[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
    18. if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
    19. {
    20. return 29;
    21. }
    22. else
    23. {
    24. return monthDayArray[month];
    25. }
    26. }
    27. bool CheckDate();
    28. bool operator<(const Date& d)const;
    29. bool operator<=(const Date& d)const;
    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. Date& operator+=(int day);
    35. Date operator+(int day)const;
    36. Date& operator-=(int day);
    37. //日期-天数
    38. Date operator-(int day)const;
    39. //日期-日期
    40. int operator-(const Date& d)const;
    41. //前置++
    42. Date& operator++();
    43. //后置++,为了区分,构成重载,给后置++强行增加了一个int形参。这里不需要写形参名,因为接收值是多少不重要
    44. //该参数仅仅是为了跟前置++构成重载区分
    45. Date operator++(int);
    46. Date& operator--();
    47. Date operator--(int);
    48. private:
    49. int _year;
    50. int _month;
    51. int _day;
    52. };
    53. //重载函数
    54. ostream& operator<<(ostream& out, const Date& d);
    55. istream& operator>>(istream& in, Date& d);

    Date.cpp

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

    Test.cpp 

    1. #include"Date.h"
    2. void TestDate1()
    3. {
    4. Date d1(2024, 6, 26);
    5. Date d2 = d1 + 30000;
    6. d1.Print();
    7. d2.Print();
    8. Date d3(2024, 6, 26);
    9. Date d4 = d3 - 5000;
    10. d3.Print();
    11. d4.Print();
    12. Date d5(2024, 6, 26);
    13. d5 += -5000;
    14. d5.Print();
    15. }
    16. void TestDate2()
    17. {
    18. Date d1(2024, 6, 26);
    19. Date d2 = ++d1;
    20. d1.Print();
    21. d2.Print();
    22. Date d3 = d1++;
    23. d1.Print();
    24. d3.Print();
    25. }
    26. void TestDate3()
    27. {
    28. Date d1(2024, 6, 26);
    29. Date d2(2034, 6, 26);
    30. int n = d1 - d2;
    31. cout << n << endl;
    32. }
    33. void TestDate4()
    34. {
    35. const Date d1(2024, 6, 26);
    36. d1.Print();
    37. d1 + 100;
    38. Date d2(2024, 6, 26);
    39. d2.Print();
    40. d2 += 100;
    41. d1 < d2;
    42. d2 < d1;
    43. }
    44. int main()
    45. {
    46. void TestDate1();
    47. return 0;
    48. }

    点赞收藏加关注是博主不断更新优质好文的动力哦~

  • 相关阅读:
    Windows系统下MySQL8.0版详细安装及配置教程
    Windows 安装 MySQL 5.7详细步骤
    实验22:轻触开关实验
    Qt扫描-QMoive 理论总结
    计算机网络学习2
    力扣刷题笔记
    运行在容器中Postgres数据库数据损坏后如何恢复?
    18. 从零开始编写一个类nginx工具, 主动式健康检查源码实现
    Java 基础
    python安装第三方包
  • 原文地址:https://blog.csdn.net/W1118_/article/details/139993334