• 【C++】:日期类实现


    朋友们、伙计们,我们又见面了,本期来给大家解读一下有关C++的基础知识点,如果看完之后对你有一定的启发,那么请留下你的三连,祝大家心想事成!

    C 语 言 专 栏:C语言:从入门到精通

    数据结构专栏:数据结构

    个  人  主  页 :stackY、

    C + + 专 栏   :C++

    Linux 专 栏  :Linux

    目录

    前言:

    1. 基本构造

    2. 基础运算符重载

    3. 进阶运算符重载

    3.1 日期+天数

    3.2 日期-天数

    3.3 日期+、-天数的优化版本

    3.4 前置++、后置++、前置--、后置--

    3.5 日期与日期相差天数

    4. 流提取、流插入运算符重载

    4.1 流插入

    完整的优化代码:

    4.2 流提取

    5. 完整代码


    前言:

    承接上篇的类和对象本期我们来实现一下日期类,顺便再来回顾一下构造函数和运算符的重载,关于基本的运算符重载就不做过多的解释,之前没提到过的会在这里重点解释。

    1. 基本构造

    日期类的实现我们采用分文件来进行实现:

    在头文件 Date.h中实现日期类的基本框架

    在源文件 Date.cpp中实现框架逻辑

    在源文件 Test.cpp中实现测试逻辑

    日期类的基本创建只需要写出构造函数和打印函数即可,因为日期类中没有资源的创建与释放,所以编译器自动生成的析构函数拷贝构造以及&运算符重载足够使用。

    头文件 :Date.h

    1. //日期类
    2. class Date
    3. {
    4. public:
    5. //不涉及内部数据的修改的函数可以加上const修饰
    6. //拷贝构造函数(全缺省)
    7. Date(int year = 1949, int month = 10, int day = 1);
    8. //打印函数
    9. void Print() const;
    10. private:
    11. int _year; //年
    12. int _month; //月
    13. int _day; //日
    14. };

    源文件:Date.cpp

    1. //拷贝构造函数(全缺省)
    2. Date::Date(int year, int month, int day)
    3. {
    4. _year = year;
    5. _month = month;
    6. _day = day;
    7. }
    8. //打印函数
    9. void Date::Print() const
    10. {
    11. cout << _year << "/" << _month << "/" << _day << endl;
    12. }

    2. 基础运算符重载

    基础运算符的重载包括==、!=、<、<=、>、>=,这几个运算符重载比较简单,在这里就不做解析。

    头文件:Date.h

    1. //日期类
    2. class Date
    3. {
    4. public:
    5. //不涉及内部数据的修改的函数可以加上const修饰
    6. //拷贝构造函数(全缺省)
    7. Date(int year = 1949, int month = 10, int day = 1);
    8. //打印函数
    9. void Print() const;
    10. //==运算符重载
    11. bool operator==(const Date& d) const;
    12. //!=运算符重载
    13. bool operator!=(const Date& d) const;
    14. //<运算符重载
    15. bool operator<(const Date& d) const;
    16. //<=运算符重载
    17. bool operator<=(const Date& d) const;
    18. //>运算符重载
    19. bool operator>(const Date& d) const;
    20. //>=运算符重载
    21. bool operator>=(const Date& d) const;
    22. private:
    23. int _year; //年
    24. int _month; //月
    25. int _day; //日
    26. };

    源文件 :Date.cpp

    1. //==运算符重载
    2. bool Date::operator==(const Date& d) const
    3. {
    4. return _year == d._year
    5. && _month == d._month
    6. && _day == d._day;
    7. }
    8. //!=运算符重载
    9. bool Date::operator!=(const Date& d) const
    10. {
    11. return !(*this == d);
    12. }
    13. //<运算符重载
    14. bool Date::operator<(const Date& d) const
    15. {
    16. if (_year < d._year)
    17. {
    18. return true;
    19. }
    20. else if (_year == d._year && _month < d._month)
    21. {
    22. return true;
    23. }
    24. else if (_year == d._year && _month == d._month && _day < d._day)
    25. {
    26. return true;
    27. }
    28. else
    29. {
    30. return false;
    31. }
    32. }
    33. //<=运算符重载
    34. bool Date::operator<=(const Date& d) const
    35. {
    36. return *this == d || *this < d;
    37. }
    38. //>运算符重载
    39. bool Date::operator>(const Date& d) const
    40. {
    41. return !(*this <= d);
    42. }
    43. //>=运算符重载
    44. bool Date::operator>=(const Date& d) const
    45. {
    46. return !(*this < d);
    47. }

    3. 进阶运算符重载

    基础的运算符重载只能满足一部分的需求,还需要对日期的加减。

    3.1 日期+天数

    一个日期+一个天数即可得到另外的一个日期,这里就存在一个天数满了进位月份,月份满了进位年份,那么就需要一个另外的函数来获取某一月的天数。

    获取天数函数:

    1. //获取某一个月的天数
    2. int Date::GetMonthDay(int year, int month) const
    3. {
    4. const static int MonthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    5. if (month == 2
    6. && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    7. {
    8. return 29;
    9. }
    10. return MonthArray[month];
    11. }

     首先判断是否为闰年,然后根据月份返回相对应的天数。

    日期+天数代码:

    头文件:Date.h

    1. //日期类
    2. class Date
    3. {
    4. public:
    5. //拷贝构造函数(全缺省)
    6. Date(int year = 1949, int month = 10, int day = 1);
    7. //获取某一个月的天数
    8. int GetMonthDay(int year, int month) const;
    9. //+=运算符重载
    10. Date& operator+=(int day);
    11. //+运算符重载
    12. Date operator+(int day) const;
    13. private:
    14. int _year; //年
    15. int _month; //月
    16. int _day; //日
    17. };

    源文件:Date.cpp

    1. //获取某一个月的天数
    2. int Date::GetMonthDay(int year, int month) const
    3. {
    4. const static int MonthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    5. if (month == 2
    6. && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    7. {
    8. return 29;
    9. }
    10. return MonthArray[month];
    11. }
    12. //+=运算符重载
    13. Date& Date::operator+=(int day)
    14. {
    15. _day += day;
    16. //判断天数的合理性
    17. while (_day > GetMonthDay(_year, _month))
    18. {
    19. _day -= GetMonthDay(_year, _month);
    20. ++_month; //天满了进位月份
    21. if (_month == 13)
    22. {
    23. ++_year; //月份满了进位年份
    24. _month = 1;
    25. }
    26. }
    27. return *this;
    28. }
    29. //+运算符重载
    30. Date Date::operator+(int day) const
    31. {
    32. //构造一个新的Date
    33. Date tmp(*this);
    34. tmp += day;
    35. return tmp;
    36. }

    3.2 日期-天数

    头文件:Date.h

    1. //日期类
    2. class Date
    3. {
    4. public:
    5. //拷贝构造函数(全缺省)
    6. Date(int year = 1949, int month = 10, int day = 1);
    7. //获取某一个月的天数
    8. int GetMonthDay(int year, int month) const;
    9. //-=运算符重载
    10. Date& operator-=(int day);
    11. //-运算符重载
    12. Date operator-(int day) const;
    13. private:
    14. int _year; //年
    15. int _month; //月
    16. int _day; //日
    17. };

    源文件:Date.cpp

    1. //获取某一个月的天数
    2. int Date::GetMonthDay(int year, int month) const
    3. {
    4. const static int MonthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    5. if (month == 2
    6. && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    7. {
    8. return 29;
    9. }
    10. return MonthArray[month];
    11. }
    12. //-=运算符重载
    13. Date& Date::operator-=(int day)
    14. {
    15. _day -= day;
    16. while (_day <= 0)
    17. {
    18. //向月份借位
    19. --_month;
    20. if (_month == 0)
    21. {
    22. //向年份借位
    23. --_year;
    24. _month = 12;
    25. }
    26. _day += GetMonthDay(_year, _month);
    27. }
    28. return *this;
    29. }
    30. //-运算符重载
    31. Date Date::operator-(int day) const
    32. {
    33. Date tmp(*this);
    34. tmp -= day;
    35. return tmp;
    36. }

     3.3 日期+、-天数的优化版本

    上面实现的这两个运算符重载还是有一定的缺陷的,万一有的人在+天数时传递负值就会出现问题,如果有人在-天数的时候传递负值也是会出现bug,那么就需要再次进行优化。

    当使用日期+天数时传递负值直接复用-=操作 

    当使用日期-天数时传递负值直接复用+=操作 

    1. //+=运算符重载
    2. Date& Date::operator+=(int day)
    3. {
    4. //负值天数操作
    5. if (day < 0)
    6. {
    7. return *this -= (-day);
    8. }
    9. _day += day;
    10. //判断天数的合理性
    11. while (_day > GetMonthDay(_year, _month))
    12. {
    13. _day -= GetMonthDay(_year, _month);
    14. ++_month; //天满了进位月份
    15. if (_month == 13)
    16. {
    17. ++_year; //月份满了进位年份
    18. _month = 1;
    19. }
    20. }
    21. return *this;
    22. }
    23. //-=运算符重载
    24. Date& Date::operator-=(int day)
    25. {
    26. //负值天数操作
    27. if (day < 0)
    28. {
    29. return *this += (-day);
    30. }
    31. _day -= day;
    32. while (_day <= 0)
    33. {
    34. //向月份借位
    35. --_month;
    36. if (_month == 0)
    37. {
    38. //向年份借位
    39. --_year;
    40. _month = 12;
    41. }
    42. _day += GetMonthDay(_year, _month);
    43. }
    44. return *this;
    45. }

    3.4 前置++、后置++、前置--、后置--

    上篇的文章中提到过前置和后置的区别就是后置++多了一个int参数

    头文件:Date.h

    1. //日期类
    2. class Date
    3. {
    4. public:
    5. //前置++运算符重载
    6. Date& operator++();
    7. //后置++运算符重载
    8. Date operator++(int) const;
    9. //前置--运算符重载
    10. Date& operator--();
    11. //后置--运算符重载
    12. Date operator--(int) const;
    13. private:
    14. int _year; //年
    15. int _month; //月
    16. int _day; //日
    17. };

    源文件:Date.cpp

    1. //前置++运算符重载
    2. Date& Date::operator++()
    3. {
    4. *this += 1;
    5. return *this;
    6. }
    7. //后置++运算符重载
    8. Date Date::operator++(int) const
    9. {
    10. Date tmp(*this);
    11. tmp += 1;
    12. return tmp;
    13. }
    14. //前置--运算符重载
    15. Date& Date::operator--()
    16. {
    17. *this -= 1;
    18. return *this;
    19. }
    20. //后置--运算符重载
    21. Date Date::operator--(int) const
    22. {
    23. Date tmp(*this);
    24. tmp -= 1;
    25. return tmp;
    26. }

    3.5 日期与日期相差天数

    日期与日期相差天数也是使用-的运算符重载,日期-日期这里可以直接减,也可以使用两个日期同时减去2000年1月1日,然后根据差值计算天数,需要注意的是日期-日期是会出现负数的。还有一种比较简单的方法计数法:设置一个标志值记录正负,然后记录两个日期当中最大的一个日期,然后设置一个计数值,将小日期加1,同时也将计数值加1,直到小日期加到和大日期相等,那么此时这个计数值就是它们之间相差的日期。

    源文件:Date.cpp

    1. //日期-日期
    2. int Date::operator-(const Date& d) const
    3. {
    4. Date dmax = *this;
    5. Date dmin = d;
    6. int flag = 1; //记录正负
    7. //找出较大的日期
    8. if (*this < d)
    9. {
    10. dmax = d;
    11. dmin = *this;
    12. flag = -1;
    13. }
    14. //设置计数值
    15. int coutN = 0;
    16. while (dmin != dmax)
    17. {
    18. ++dmin;
    19. ++coutN;
    20. }
    21. //将差值再*记录值
    22. return coutN * flag;
    23. }

    4. 流提取、流插入运算符重载

    如果我们使用cout来直接打印日期类的话是不能打印的,同样的使用cin来对日期类输入也是不支持的,因为日期类是不属于内置类型的,如果我们也要像内置类型一样使用cin和cout就需要对>>(流提取)<<(流插入)进行重载

    cin是一个istream类中的对象,内置类型能使用流提取的原因就是库里面将内置类型都进行重载了。

    cout是一个ostream类中的对象

    4.1 流插入

    我们要实现自定义类型的流插入就需要用到库里面的流插入来进行辅助:

    1. //头文件:Date.h
    2. //日期类
    3. class Date
    4. {
    5. public:
    6. //拷贝构造函数(全缺省)
    7. Date(int year = 1949, int month = 10, int day = 1);
    8. //打印函数
    9. //流插入
    10. void operator<<(ostream& out);
    11. private:
    12. int _year; //年
    13. int _month; //月
    14. int _day; //日
    15. };
    16. //源文件:Date.cpp
    17. //流插入
    18. void Date::operator<<(ostream& out)
    19. {
    20. out << _year << "/" << _month << "/" << _day << endl;
    21. }

    如果这样子写的话会出现一个问题:我们正常调用会调用不了:

    类成员函数的第一个参数是隐藏的this指针,所以正常调用的话跟重载的顺序不匹配:

    实现为这样非要调用的话只需要将顺序换一下即可:

    但是这样子调用又不符合日常调用习惯,所以我们需要将两个参数的位置调换,但是类成员函数的第一个函数是默认隐藏的,所以需要调换的话就需要写为全局运算符重载函数,但是设为全局又存在一个新的问题,类中的私有函数在类外面不能访问,这里有两种方法:

    1. 使用单独的函数将年份、月份、天数都分别保存起来直接使用。

    2. 将流插入运算符重载设置为友元函数

    在这里我们使用第二种方法:

    1. //头文件:Date.h
    2. //日期类
    3. class Date
    4. {
    5. //友元声明
    6. friend void operator<<(ostream& out, const Date& d);
    7. public:
    8. //拷贝构造函数(全缺省)
    9. Date(int year = 1949, int month = 10, int day = 1);
    10. //...
    11. private:
    12. int _year; //年
    13. int _month; //月
    14. int _day; //日
    15. };
    16. //流插入
    17. void operator<<(ostream& out, const Date& dt);
    18. //源文件:Date.cpp
    19. //流插入
    20. void operator<<(ostream& out, const Date& d)
    21. {
    22. out << d._year << "/" << d._month << "/" << d._day << endl;
    23. }

    这样写的话还是存在一个小小的问题:每次输出的话只能一个一个日期类进行输出,不能一次性输出多个:

    这个问题的主要原因是前面的输出d1是一个正常的运算符重载,而这个运算符重载的返回值又是void,而接下来的运算符重载不认识void类型,所以需要返回一个ostream类型才能完成下面的输出。

    完整的优化代码:

    头文件:Date.h

    1. //日期类
    2. class Date
    3. {
    4. //友元声明
    5. friend ostream& operator<<(ostream& out, const Date& d);
    6. public:
    7. //不涉及内部数据的修改的函数可以加上const修饰
    8. //拷贝构造函数(全缺省)
    9. Date(int year = 1949, int month = 10, int day = 1);
    10. //...
    11. private:
    12. int _year; //年
    13. int _month; //月
    14. int _day; //日
    15. };
    16. //流插入
    17. ostream& operator<<(ostream& out, const Date& d);

    源文件:Date.cpp

    1. //流插入
    2. ostream& operator<<(ostream& out, const Date& d)
    3. {
    4. out << d._year << "/" << d._month << "/" << d._day << endl;
    5. return out;
    6. }

    4.2 流提取

    cin是一个istream类中的对象,所以我们可以借助istream来实现对于日期类的输入:

    头文件:Date.h

    1. //日期类
    2. class Date
    3. {
    4. //友元声明
    5. friend ostream& operator<<(ostream& out, const Date& d);
    6. friend istream& operator>>(istream& in, Date& d);
    7. public:
    8. //不涉及内部数据的修改的函数可以加上const修饰
    9. //拷贝构造函数(全缺省)
    10. Date(int year = 1949, int month = 10, int day = 1);
    11. //...
    12. private:
    13. int _year; //年
    14. int _month; //月
    15. int _day; //日
    16. };
    17. //流插入
    18. ostream& operator<<(ostream& out, const Date& d);
    19. //流提取
    20. istream& operator>>(istream& in, Date& d);

    源文件:Dtae.cpp

    1. //流插入
    2. ostream& operator<<(ostream& out, const Date& d)
    3. {
    4. out << d._year << "/" << d._month << "/" << d._day << endl;
    5. return out;
    6. }
    7. //流提取
    8. istream& operator>>(istream& in, Date& d)
    9. {
    10. in >> d._year >> d._month >> d._day;
    11. return in;
    12. }

    5. 完整代码

    头文件:Date.h

    1. #pragma once
    2. #include
    3. using namespace std;
    4. //日期类
    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. //不涉及内部数据的修改的函数可以加上const修饰
    12. //拷贝构造函数(全缺省)
    13. Date(int year = 1949, int month = 10, int day = 1);
    14. //打印函数
    15. void Print() const;
    16. //获取某一个月的天数
    17. int GetMonthDay(int year, int month) const;
    18. //==运算符重载
    19. bool operator==(const Date& d) const;
    20. //!=运算符重载
    21. bool operator!=(const Date& d) const;
    22. //<运算符重载
    23. bool operator<(const Date& d) const;
    24. //<=运算符重载
    25. bool operator<=(const Date& d) const;
    26. //>运算符重载
    27. bool operator>(const Date& d) const;
    28. //>=运算符重载
    29. bool operator>=(const Date& d) const;
    30. //+=运算符重载
    31. Date& operator+=(int day);
    32. //+运算符重载
    33. Date operator+(int day) const;
    34. //-=运算符重载
    35. Date& operator-=(int day);
    36. //-运算符重载
    37. Date operator-(int day) const;
    38. //前置++运算符重载
    39. Date& operator++();
    40. //后置++运算符重载
    41. Date operator++(int) const;
    42. //前置--运算符重载
    43. Date& operator--();
    44. //后置--运算符重载
    45. Date operator--(int) const;
    46. //日期-日期
    47. int operator-(const Date& d) const;
    48. private:
    49. int _year; //年
    50. int _month; //月
    51. int _day; //日
    52. };
    53. //流插入
    54. ostream& operator<<(ostream& out, const Date& d);
    55. //流提取
    56. istream& operator>>(istream& in, Date& d);

    源文件:Date.cpp

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include"Date.h"
    3. //拷贝构造函数(全缺省)
    4. Date::Date(int year, int month, int day)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. //打印函数
    11. void Date::Print() const
    12. {
    13. cout << _year << "/" << _month << "/" << _day << endl;
    14. }
    15. //==运算符重载
    16. bool Date::operator==(const Date& d) const
    17. {
    18. return _year == d._year
    19. && _month == d._month
    20. && _day == d._day;
    21. }
    22. //!=运算符重载
    23. bool Date::operator!=(const Date& d) const
    24. {
    25. return !(*this == d);
    26. }
    27. //<运算符重载
    28. bool Date::operator<(const Date& d) const
    29. {
    30. if (_year < d._year)
    31. {
    32. return true;
    33. }
    34. else if (_year == d._year && _month < d._month)
    35. {
    36. return true;
    37. }
    38. else if (_year == d._year && _month == d._month && _day < d._day)
    39. {
    40. return true;
    41. }
    42. else
    43. {
    44. return false;
    45. }
    46. }
    47. //<=运算符重载
    48. bool Date::operator<=(const Date& d) const
    49. {
    50. return *this == d || *this < d;
    51. }
    52. //>运算符重载
    53. bool Date::operator>(const Date& d) const
    54. {
    55. return !(*this <= d);
    56. }
    57. //>=运算符重载
    58. bool Date::operator>=(const Date& d) const
    59. {
    60. return !(*this < d);
    61. }
    62. //获取某一个月的天数
    63. int Date::GetMonthDay(int year, int month) const
    64. {
    65. const static int MonthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    66. if (month == 2
    67. && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    68. {
    69. return 29;
    70. }
    71. return MonthArray[month];
    72. }
    73. //+=运算符重载
    74. Date& Date::operator+=(int day)
    75. {
    76. //负值天数操作
    77. if (day < 0)
    78. {
    79. return *this -= (-day);
    80. }
    81. _day += day;
    82. //判断天数的合理性
    83. while (_day > GetMonthDay(_year, _month))
    84. {
    85. _day -= GetMonthDay(_year, _month);
    86. ++_month; //天满了进位月份
    87. if (_month == 13)
    88. {
    89. ++_year; //月份满了进位年份
    90. _month = 1;
    91. }
    92. }
    93. return *this;
    94. }
    95. //+运算符重载
    96. Date Date::operator+(int day) const
    97. {
    98. //构造一个新的Date
    99. Date tmp(*this);
    100. tmp += day;
    101. return tmp;
    102. }
    103. //-=运算符重载
    104. Date& Date::operator-=(int day)
    105. {
    106. //负值天数操作
    107. if (day < 0)
    108. {
    109. return *this += (-day);
    110. }
    111. _day -= day;
    112. while (_day <= 0)
    113. {
    114. //向月份借位
    115. --_month;
    116. if (_month == 0)
    117. {
    118. //向年份借位
    119. --_year;
    120. _month = 12;
    121. }
    122. _day += GetMonthDay(_year, _month);
    123. }
    124. return *this;
    125. }
    126. //-运算符重载
    127. Date Date::operator-(int day) const
    128. {
    129. Date tmp(*this);
    130. tmp -= day;
    131. return tmp;
    132. }
    133. //前置++运算符重载
    134. Date& Date::operator++()
    135. {
    136. *this += 1;
    137. return *this;
    138. }
    139. //后置++运算符重载
    140. Date Date::operator++(int) const
    141. {
    142. Date tmp(*this);
    143. tmp += 1;
    144. return tmp;
    145. }
    146. //前置--运算符重载
    147. Date& Date::operator--()
    148. {
    149. *this -= 1;
    150. return *this;
    151. }
    152. //后置--运算符重载
    153. Date Date::operator--(int) const
    154. {
    155. Date tmp(*this);
    156. tmp -= 1;
    157. return tmp;
    158. }
    159. //日期-日期
    160. int Date::operator-(const Date& d) const
    161. {
    162. Date dmax = *this;
    163. Date dmin = d;
    164. int flag = 1; //记录正负
    165. //找出较大的日期
    166. if (*this < d)
    167. {
    168. dmax = d;
    169. dmin = *this;
    170. flag = -1;
    171. }
    172. //设置计数值
    173. int coutN = 0;
    174. while (dmin != dmax)
    175. {
    176. ++dmin;
    177. ++coutN;
    178. }
    179. //将差值再*记录值
    180. return coutN * flag;
    181. }
    182. //流插入
    183. ostream& operator<<(ostream& out, const Date& d)
    184. {
    185. out << d._year << "/" << d._month << "/" << d._day << endl;
    186. return out;
    187. }
    188. //流提取
    189. istream& operator>>(istream& in, Date& d)
    190. {
    191. in >> d._year >> d._month >> d._day;
    192. return in;
    193. }

    朋友们、伙计们,美好的时光总是短暂的,我们本期的的分享就到此结束,欲知后事如何,请听下回分解~,最后看完别忘了留下你们弥足珍贵的三连喔,感谢大家的支持!

  • 相关阅读:
    [英雄星球七月集训LeetCode解题日报] 第5日 双指针
    ubunut搭建aarch64 cuda交叉编译环境记录
    【大数据入门核心技术-Hive】Hive3.1.2高可用集群搭建
    JavaSE:String类
    对象分配规则
    Docker 及 Docker Compose 安装指南
    全球创新创业数据国内版
    对zygote的理解
    Java模板方法模式源码剖析及使用场景
    js录制屏幕并输出视频
  • 原文地址:https://blog.csdn.net/Yikefore/article/details/133611275