• 日期类的实现


    目录

     运算符重载:

     日期类:

    日期的>

    日期类+天数:

    +=的实现:

    +天数函数:

     


    1. class Stack
    2. {
    3. public:
    4. Stack(int capacity=4 )
    5. {
    6. _a = (int*)malloc(sizeof(int)*capacity);
    7. if (_a == nullptr)
    8. {
    9. perror("malloc fail");
    10. exit(-1);
    11. }
    12. _top = 0;
    13. _capacity = capacity;
    14. cout << "Stack构造函数()" << endl;
    15. }
    16. Stack(const Stack&st)
    17. {
    18. _a=(int*)malloc(sizeof(int)*st._capacity);
    19. if (_a == nullptr)
    20. {
    21. perror("malloc fail");
    22. exit(-1);
    23. }
    24. memcpy(_a, st._a, sizeof(int)*st._top);
    25. _top = st._top;
    26. _capacity = st._capacity;
    27. }
    28. void Push(int x)
    29. {
    30. _a[_top++] = x;
    31. }
    32. ~Stack()
    33. {
    34. free(_a);
    35. _a = nullptr;
    36. _top = _capacity = 0;
    37. cout << "Stack析构函数()" << endl;
    38. }
    39. private:
    40. int*_a;
    41. int _capacity;
    42. int _top;
    43. };
    44. class MyQueue
    45. {
    46. public:
    47. void push(int x)
    48. {
    49. }
    50. private:
    51. Stack _pushSt;
    52. Stack _popSt;
    53. size_t size ;
    54. };
    55. int main()
    56. {
    57. MyQueue m1;
    58. MyQueue q(m1);
    59. return 0;
    60. }

    MyQueue这个类需要自定义拷贝构造函数吗?

    答:不需要,对于内置类完成值拷贝,对于自定义类型调用其默认拷贝构造。

    Stack有默认的拷贝构造。

     

    完成了深拷贝。

     运算符重载

    运算符重载使类函数调用可读更强。

     日期类

    我们先写一个日期类比较大小。

     我们要想实现符号重载,需要自主实现一个>函数。

    我们可以先实现一个简单的判断相等函数:

    1. bool operator==(const Date&d1, const Date&d2)
    2. {
    3. return d1._year == d2.__year
    4. &&d1._month == d2._month
    5. &&d1._day == d2._day;
    6. }

    但是我们的函数写在类外面,无法访问类里面的成员变量,我们的方法是把函数定义在类里面:

    但是注意:==函数有两个操作数,所以只有两个参数:

    类成员函数都有一个默认参数:隐含的 this指针,所以:

    1. bool operator==(const Date&d1)
    2. {
    3. return _year == d1._year
    4. &&_month == d1._month
    5. &&_day == d1._day;
    6. }

     <<优先级要大于==,所以我们要加上()。

     

     假如我们必须在类外面进行运算符重载呢?

    我们无法提取到成员变量,我们可以设计一个成员函数,提取到对应对象的成员变量:

    1. int GetYear()
    2. {
    3. return _year;
    4. }
    5. int GetMonth()
    6. {
    7. return _month;
    8. }
    9. int GetDay()
    10. {
    11. return _day;
    12. }

    日期的>

    1. bool operator>(const Date&d1)
    2. {
    3. if (_year > d1._year)
    4. {
    5. return true;
    6. }
    7. else if (_year ==d1._year&&_month >d1._month)
    8. {
    9. return true;
    10. }
    11. else if (_year == d1._year&&_month == d1._month&&_day >d1._day)
    12. {
    13. return true;
    14. }
    15. else
    16. return false;
    17. }
    1. int main()
    2. {
    3. Date d1(2022, 9, 22);
    4. Date d2(2022, 9, 21);
    5. /*d1.operator==(d2);*/
    6. cout << (d1>d2) << endl;
    7. }

    有了大于,我们就可以写出大于等于了。

    1. bool operator>=(const Date&d1)
    2. {
    3. return *this > d1||*this == d1;
    4. }
    1. int main()
    2. {
    3. Date d1(2022, 9, 22);
    4. Date d2(2022, 9, 21);
    5. /*d1.operator==(d2);*/
    6. cout << (d1>=d2) << endl;
    7. }

    日期类+天数:

    1. int GetMonthDay(int year, int month)
    2. {
    3. static int monthDayArray[13] = { 0, 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 monthDayArray[month];
    11. }
    12. }

    闰年:年数与4的余数等于0且年数与100的余数不等于0和年数与400的余数等于0.

    这里为什么要创建静态数组?

    答:静态数组是在代码段(静态区),如果我们创建普通数组,每一个对象都会多存储13个整型的字节,消耗空间。

    +=的实现:

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

    分析:

    case1:当日期的天数加上对应天数小于对应月(9)的天数(30),我们直接加日期就可以。

    case2:当日期的天数加上对应天数大于对应月的天数,我们让日期加天数-该月的天数,月++。

    case3:同case2,当月++大于13时,把月置为1,年++。

    最后返回*this,日期类指针的解引用。

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

    我们返回的也是一个日期类,并且这个日期类是this指针指向日期加天数之后的结果,调用完毕之后这个日期依旧存在,所以要用传引用返回。

    加等表示d1的值也发生了改变。 

    +天数函数:

     

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

    我们创建一个临时变量,让临时变量代替*this+=天数,然后返回临时变量,不能传引用返回,因为临时变量在函数调用时自动销毁。

    1. int main()
    2. {
    3. Date d1(2022, 9, 22);
    4. Date d2;
    5. /*d1.operator==(d2);*/
    6. d2 = d1 + 30;
    7. return 0;
    8. }

    d1+30之后,d1本身没有发生改变。 

  • 相关阅读:
    Python unittest单元测试框架 TestSuite测试套件
    Java-IO流(常用类)
    对抗微信如何要把网页都变成快应用
    docker 安装nacos,使用自定义mysql
    借助Web3盘活日本优质IP:UneMeta 与 OpenSea 的差异化竞争
    java计算机毕业设计线上远程教学及自主学平台的设计与实现源码+系统+数据库+lw文档
    【Linux】线程同步和互斥
    Java设计模式 --建造者模式【Builder Pattern】
    如何将数据输入神经网络,神经网络的数据处理
    【附源码】计算机毕业设计SSM外卖调度管理系统
  • 原文地址:https://blog.csdn.net/qq_66581313/article/details/132834228