• c++日期类的实现


    一.赋值运算符重载

    1.1运算符重载

    运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似

    函数名字是:返回值类型 operator操作数(参数列表)

    // 全局的operator==
     class Date
     { 
    public:
     Date(int year = 1900, int month = 1, int day = 1)
        {
     _year = year;
     _month = month;
     _day = day;
        }    
    //private:
     int _year;
     int _month;
     int _day;
     };
     // 这里会发现运算符重载成全局的就需要成员变量是公有的,那么问题来了,封装性如何保证?
    // 这里其实可以用我们后面学习的友元解决,或者干脆重载成成员函数。
    bool operator==(const Date& d1, const Date& d2)
     {
     return d1._year == d2._year
     && d1._month == d2._month
     && d1._day == d2._day;
     }
     void Test ()
     {
     Date d1(2018, 9, 26);
     Date d2(2018, 9, 27);
     cout<<(d1 == d2)<<endl;
     }
      class Date
     { 
    public:
     Date(int year = 1900, int month = 1, int day = 1)
     {
        }
     _year = year;
     _month = month;
     _day = day;
     // bool operator==(Date* this, const Date& d2)
     // 这里需要注意的是,左操作数是this,指向调用函数的对象
    bool operator==(const Date& d2)
     {
     return _year == d2._year;
     && _month == d2._month
     && _day == d2._day;
     }
     private:
     int _year;
     int _month;
     int _day;
     };
    

    1.2赋值运算符重载

    1.2.1赋值运算符重载格式

    参数类型:const T&,传递引用可以提高传参效率
    返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
    检测是否自己给自己赋值
    返回*this :要复合连续赋值的含义

    class Date
     { 
    public :
     Date(int year = 1900, int month = 1, int day = 1)
        {
     _year = year;
     _month = month;
     _day = day;
        }
     Date (const Date& d)
        {
     _year = d._year;
     _month = d._month;
     _day = d._day;
        }
     Date& operator=(const Date& d)
     {
     if(this != &d)
            {
     _year = d._year;
     _month = d._month;
     _day = d._day;
            }
     }
     private:
     int _year ;
     int _month ;
     int _day ;
     };
    

    1.3前置++和后置++

    class Date
     {
     public:
     Date(int year = 1900, int month = 1, int day = 1)
     {
     _year = year;
     _month = month;
     _day = day;
     }
     // 前置++:返回+1之后的结果
    // 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
    Date& operator++()
     {
     _day += 1;
     return *this;
     }
    // 后置++:
    // 前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载
    // C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器
    自动传递
    // 注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存
    一份,然后给this+1
     //       
    private:
     int _year;
     int _month;
     int _day;
     };
    

    二.日期类的实现

    Date.h

    #pragma  once 
      2 #include <iostream>
      3 #include <assert.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 using namespace std;
      7 
      8 class Date
      9 {
     10 public:
     11   Date(int year = 1, int month = 1, int day = 1);
     12   bool operator<(const Date& d);
     13   bool operator<=(const Date& d);
     14   bool operator>(const Date& d);
     15   bool operator>=(const Date& d);
     16   bool operator==(const Date& d);
     17   bool operator!=(const Date& d);
     18 
     19   // d1 + 100
     20   Date& operator+=(int day);
     21   Date operator+(int day);
     22   // d1 - 100
     23   Date operator-(int day);
     24   Date& operator-=(int day);
     25 
     26   // ++d1
     27   Date& operator++();
     28   // 特殊处理:解决语法逻辑不自洽,自相矛盾的问题
     29   // d1++
     30   // 为了跟前置++区分,强行增加一个int形参,够成重载区分
     31   Date operator++(int); 
     Date operator--(int);                                                   
     34   Date& operator--();
     35 
     36   // d1 - d2
     37   int operator-(const Date& d);
     38 
     39   // 本质就是inline
     40   int GetMonthDay(int year, int month)
     41   {
     42     assert(month>0&&month<13);
     43     int monthDay[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     44 
     45     if(((year%4 == 0&&year&&year%100!=0)&&year%400==0 ) &&month==2)
     46     {
     47         return 29;
     48     }
     49   
     50       return  monthDay[month];
     51   }
     52 
     53   void Print()
     54   {
     55     cout << _year << "/" << _month << "/" << _day << endl;
     56   }
     57 private:
     58   int _year;
     59   int _month;
     60   int _day;
     61 };
    
    

    Date.c

    #include "Date.h"                
        2 Date::Date(int year, int month, int day)              
        3 {                                                     
        4   _year = year;
        5   _month = month;                                            
        6   _day = day;                                                
        7 }                                     
        8 
        9 bool Date::operator<(const Date& d)                        
       10 {                                     
       11   if(_year<d._year)                               
       12   {                                                                  
       13     return true;                      
       14   }                                                      
       15   else  if(_year==d._year)
       16   {                                                        
       17       if(_month<d._month)                
       18       {                                                             
       19         return true;
       20       }                                                          
       21       else if (_month==d._month)      
       22       {
       23         if(_day<d._day)                                                
       24         {                                  
       25           return true;                                  
       26         }                                  
       27       }                               
       28     }                                                               
       29     return false;                                                       
       30 }                                     
       31                                                     
       32 // d1 <= d2                              
       33 bool Date::operator<=(const Date& d) 
       34 {                                                             
       35   return *this < d || *this == d; }
       37 
       38 bool Date::operator>(const Date& d)
       39 {                                                                       
       40   return !(*this <= d);
       41 }
       42 
       43 bool Date::operator>=(const Date& d)
       44 {
       45   return !(*this < d);
       46 }
       47 
       48 bool Date::operator==(const Date& d)
       49 {
       50   return _year == d._year
       51     && _month == d._month
       52     && _day == d._day;
       53 }
       54 
       55 bool Date::operator!=(const Date& d)
       56 {
       57   return !(*this == d);
       58 }
       59 
       60 // d1 += 10
       61 Date& Date::operator+=(int day)
       62 {
       63   _day += day;
       64   while (_day > GetMonthDay(_year, _month))
       65   {
       66     _day -= GetMonthDay(_year, _month);
       67     ++_month;
       68     if (_month == 13)
    {                                                                   
       70       ++_year;
       71       _month = 1;
       72     }
       73   }
       74 
       75   return *this;
       76 }
       77 
       78 Date Date::operator+(int day)
       79 {
       80   //Date tmp(*this);
       81   Date tmp = *this; // 
       82   tmp += day;
       83 
       84   return tmp;
       85 }
       Date Date::operator-(int day)
      117 {
      118   Date tmp = *this;
      119   tmp -= day;
      120 
      121   return tmp;
      122 }
      123                                                                         
      124 Date& Date::operator-=(int day)
      125 {
      126   _day -= day;
      127   while (_day <= 0)
      128   {
      129     --_month;
      130     if (_month == 0)
      131     {
      132       --_year;
      133       _month = 12;
      134     }
      135 
      136     _day += GetMonthDay(_year, _month);
      137   }
      138 
      139   return *this;
      140 }
      141 
      142 // ++d ->d.operator++()
      143 Date& Date::operator++()
      144 {
      145   *this += 1;
      146   return *this;
      147 }
    Date Date::operator++(int)                                              
      151 {
      152   Date tmp = *this;
      153   *this += 1;
      154   return tmp;
      155 }
      156 
      157 // d1 - d2
      158 int Date::operator-(const Date& d)
      159 {
      160   int flag = 1;
      161   Date max = *this;
      162   Date min = d;
      163 
      164   if (*this < d)
      165   {
    W>166     int flag = -1;
      167     max = d;
      168     min = *this;
      169   }
      170 
      171   int n = 0;
      172   while (min != max)
      173   {
      174     ++min;
      175     ++n;
      176   }
      177 
      178   return n * flag;
      179 }
    
    
    
  • 相关阅读:
    如何在中国大陆三大运营商申请公网IPV4地址
    <学习笔记>从零开始自学Python-之-web应用框架Django( 八)Django表单
    SpringSecurity---PasswordEncoder
    初识C语言
    一份面向刷题的 C++ STL 使用指南
    间隔一分钟按某个按键一次——VBS脚本
    [附源码]计算机毕业设计基于Springboot校园招聘系统
    Flutter 3.3 现已发布
    netfilter编程实例——一个简单的防火墙
    python+selenium的web自动化之针对上传操作的实现方案
  • 原文地址:https://blog.csdn.net/qq_63168095/article/details/139742534