• C++实现Date类


     Date.h

    1. #pragma once
    2. #include
    3. using std::cout;
    4. using std::endl;
    5. class Date {
    6. private:
    7. int _year = 1;
    8. int _month = 1;
    9. int _day = 1;
    10. public:
    11. //日期类无需显式定义拷贝构造函数、析构函数、赋值运算符重载
    12. //打印
    13. void Print();
    14. //有参构造函数
    15. Date(int year = 1, int month = 1, int day = 1);
    16. //==重载
    17. bool operator==(const Date& d);
    18. //!=重载
    19. bool operator!=(const Date& d);
    20. //<重载
    21. bool operator<(const Date& d);
    22. //<=重载
    23. bool operator<=(const Date& d);
    24. //>重载
    25. bool operator>(const Date& d);
    26. //>=重载
    27. bool operator>=(const Date& d);
    28. //日期+=天数重载
    29. Date& operator+=(int day);
    30. //日期+天数重载
    31. Date operator+(int day);
    32. //日期-=天数重载
    33. Date& operator-=(int day);
    34. //日期-天数重载
    35. Date operator-(int day);
    36. //日期前置++重载
    37. Date& operator++();
    38. //日期后置++重载
    39. Date operator++(int);
    40. //日期类-日期类重载
    41. int operator-(const Date& d);
    42. //某月的天数(函数直接定义在类中相当于内联函数)
    43. int Getmonthday(const Date& d)
    44. {
    45. static int monthday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    46. if (d._month == 2 && (((d._year % 4 == 0) && (d._year % 100 != 0)) || d._year % 400 == 0))
    47. {
    48. return 29;
    49. }
    50. return monthday[d._month];
    51. }
    52. };

    Date.cpp

    1. #include "Date.h"
    2. //打印
    3. void Date::Print()
    4. {
    5. cout << _year << "/" << _month << "/" << _day << endl;
    6. }
    7. //有参构造函数
    8. Date::Date(int year, int month, int day)
    9. {
    10. _year = year;
    11. _month = month;
    12. _day = day;
    13. }
    14. //==重载
    15. bool Date::operator==(const Date& d)
    16. {
    17. return this->_year == d._year
    18. && this->_month == d._month
    19. && this->_day == d._day;
    20. }
    21. //!=重载
    22. bool Date::operator!=(const Date& d)
    23. {
    24. return !(*this == d);
    25. }
    26. //<重载
    27. bool Date::operator<(const Date& d)
    28. {
    29. if (this->_year < d._year)
    30. {
    31. return true;
    32. }
    33. else if (this->_year == d._year)
    34. {
    35. if (this->_month < d._month)
    36. {
    37. return true;
    38. }
    39. else if (this->_month == d._month)
    40. {
    41. if (this->_day < this->_day)
    42. {
    43. return true;
    44. }
    45. }
    46. }
    47. return false;
    48. }
    49. //<=重载
    50. bool Date::operator<=(const Date& d)
    51. {
    52. return ((*this) < d) || (*(this) == d);
    53. }
    54. //>重载
    55. bool Date::operator>(const Date& d)
    56. {
    57. return !(*(this) <= d);
    58. }
    59. //>=重载
    60. bool Date::operator>=(const Date& d)
    61. {
    62. return !(*(this) < d);
    63. }
    64. //日期+=天数重载
    65. Date& Date::operator+=(int day)
    66. {
    67. _day += day;
    68. while (_day > Getmonthday(*this))
    69. {
    70. _day -= Getmonthday(*this);
    71. ++_month;
    72. if (_month > 12)
    73. {
    74. _month = 1;
    75. ++_year;
    76. }
    77. }
    78. return *this;
    79. }
    80. //日期+天数重载
    81. Date Date::operator+(int day)
    82. {
    83. Date tmp = *this;
    84. tmp += day;
    85. return tmp;
    86. }
    87. //日期-=天数重载
    88. Date& Date::operator-=(int day)
    89. {
    90. _day -= day;
    91. while (_day <= 0)
    92. {
    93. --_month;
    94. if (_month == 0)
    95. {
    96. _month = 12;
    97. --_year;
    98. }
    99. _day += Getmonthday(*this);
    100. }
    101. return *this;
    102. }
    103. //日期-天数重载
    104. Date Date::operator-(int day)
    105. {
    106. Date tmp = *this;
    107. tmp -= day;
    108. return tmp;
    109. }
    110. //日期前置++重载
    111. Date& Date::operator++()
    112. {
    113. *this += 1;
    114. return *this;
    115. }
    116. //日期后置++重载
    117. Date Date::operator++(int)
    118. {
    119. Date tmp = *this;
    120. *this += 1;
    121. return tmp;
    122. }
    123. //日期类-日期类重载
    124. int Date::operator-(const Date& d)
    125. {
    126. Date min = *this;
    127. Date max = d;
    128. int flag = -1;
    129. if (*this > d)
    130. {
    131. flag = 1;
    132. min = d;
    133. max = *this;
    134. }
    135. int n = 0;
    136. while (min != max)
    137. {
    138. min++;
    139. n++;
    140. }
    141. return n * flag;
    142. }
  • 相关阅读:
    leetcode 1002. 查找共用字符
    仓库物资管理系统(C#+SQL)
    js_对象的遍历与删除/for...in
    Ubuntu下安装vscode,并解决终端打不开vscode的问题
    独立站卖家需要做SEO
    Vue移动端动态表单生成组件
    (八)SpringCloud+Security+Oauth2--token增强个性化和格式化输出
    Spring MVC视图解析器简介说明
    农历版的FullCalendar插件
    【示波器专题】示波器探头的原理深入——无源探头
  • 原文地址:https://blog.csdn.net/2301_76197086/article/details/136308440