Date日期类

本文介绍了一个日期类的实现过程,包括构造函数、拷贝构造函数、赋值运算符重载及各种比较运算符重载等成员函数。同时实现了日期的加减操作及自增自减功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对日期类实现的大致流程

  1. 在私有成员中定义 int 整形的年(_year),月(_month),日(_day).
  2. 在公有成员中实现
    a.构造函数 Date(int year=1990,int month=1,int day=1);
    b.拷贝构造函数 Date(const Date& d);
    c.赋值运算符的重载 Date& operator=(const Date& d);
    d等号运算符重载 bool operator==(const Date& d);
    e.加等运算符重载 Date& operator+=(const Date& d);
    f.减等运算符重载 Date& operator-=(const Date& d);
    g.大于号运算符重载 bool operator>(const Date& d);
    h. 小于号运算符重载 bool operator<(const Date& d);
    i:前置++运算符重载 Date& opetator++();
    j:前置–运算符重载 Date& operator–();
  3. 在实现上述接口之前,我们需要实现一个获取当月日期的函数
    int getday(int year,intmonth);

首先来实 getday 函数;

int getday(int year,int month){ 
//建立一个数组用来保存每月的天数(其中2月保存平年的天数)
      int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
      //先获取到一个天数, 在对特殊情况2月进行判定
      int days=day[month-1];
        if(month==2){
       // 如果该年为闰年,则给获取到的天数加1
             if((year%4==0&&year&100!=0)||year%400==0){
                       days++;
        }
      }
      return days;
    }

接下来我们创建类

classs Date{
public:

private:
   int _year;
   int _month;
   int _day;
}

构造函数

Date(int year=1990,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){
this->_year=d._year;
this->_month=_month;
this->_day=_day;
}

等号运算符重载

bool operator==(const Date& d){
return this->_year==d._year && 
          this->_month==d._month && 
          this->_day==d._day;
}

大于号运算符重载

bool operator>(const Date& d){
if(this->_year>d._year){
     return true;
     }
   else  if(this->_year==d._year){
            if(this->_month>d._month){
                 return yrue;
                 }
               else  if(this->_month==d._month){
                       if(this->_day>d._day){
                       return ture;
                      }
                    }
                 }
           return flase;
           }

加等运算符重载

Date& operator +=(int day){
   _day=_day+day;
   whide(_day > getday(_year,_day)){
         _day=_day-getday(_year,_month);
         _month++;
         if(_month==13){
            _month=1;
            _year++;
           }
        }
        cout<<_year"---"<<_month<<"---"<<_day<<endl;
        return *this;
}

减等运算符重载

Date& operatoe -=(int day){
if(day<0){
    return *this += (-day);
}
_day=_day-day;
while(_day<=0){
    _month--;
    if(_month==0){
        _year--;
        _month=12;
}
    _day=getday(_year,_month)+_day;
    cour<<_year<<"---"<<_month<<"---"<<_day<<endl;
}
return *this;
}

前置++运算符重载

Date& operator++(){
_day = _day + 1;
if (_day > getday(_year, _month)){
      _month++;
      if (_month == 13){
        _month = 1;
        _year++;
     }
      _day = 1;
  }
  cout << _year << "---" << _month << "---" << _day << endl;
  return *this;
 }

前置–运算符重载

Date& operator--(){
  _day--;
  if (_day <= 0){
      _month--;
      if (_month == 0){
          _month = 12;
          _year--;
     }
  _day = getday(_year, _month);
  }
 cout << _year << "---" << _month << "---" << _day << endl;
 return *this;
 }

整体代码如下

#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
class Date
{
public:
 int getday(int _year, int _month){
  int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  int day = days[_month - 1];
  if (_month == 2){
   if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 != 0){
    day++;
   }
  }
  return day;
 }
 //等号运算符重载
 bool operator==(const Date& d)const{
  return this->_year == d._year&&
   this->_month == d._month&&
   this->_day == d._day;
 }
 //赋值运算符重载
 Date operator=(const Date&d){
  this->_year = d._year;
  this->_month = d._month;
  this->_day = d._day;
 }
 //减等运算符重载
 Date& operator-=(int day){
  if (day < 0){
   *this += (-day);
  }
  _day = _day - day;
  while (_day <= 0){
   _month--;
   if (_month<=0){
    _month = 12;
    _year--;
   }
   _day = getday(_year, _month) + _day;
  }
  cout << _year << "--" << _month << "--" << _day << endl;
  return *this;
 }
 //加等运算符重载
 Date& operator+=(int day){
  _day = _day + day;
  while (_day > getday(_year, _month)){
   _day = _day - getday(_year, _month);
   _month++;
   if (_month == 13){
    _month = 1;
    _year++;
   }
  }
  cout << _year << "--" << _month << "--" << _day << endl;
  return *this;
 }
 //默认构造函数
 Date(int year=1990, int month=1, int day=1){
  _year = year;
  _month = month;
  _day = day;
  cout << _year<<"--"<<_month<<"--"<<_day << endl;
 }
 //拷贝构造函数
 Date(const Date& d){
  _year = d._day;
  _month = d._month;
  _day = d._year;
 }
 //大于号运算符重载
 bool operator>(const Date& d)const{
  if (_year > d._year){
   return true;
  }
  else if (_year == d._year){
   if (_month > d._month){
    return true;
   }
   else if (_month == d._month){
    if (_day > d._day){
     return true;
    }
   }
  }
  return false;
 }
 //大于等于运算符重载
 bool operator>=(const Date& d)const{
  return (*this > d)||(*this==d);
 }
 //小于运算符重载
 bool operator<(const Date& d)const{
  return !(*this >= d);
 }
 //前置++运算符重载
 Date& operator++(){
  _day = _day + 1;
  if (_day > getday(_year, _month)){
   _month++;
   if (_month == 13){
    _month = 1;
    _year++;
   }
   _day = 1;
  }
  cout << _year << "---" << _month << "---" << _day << endl;
  return *this;
 }
 //前置--运算符重载
 Date& operator--(){
  _day--;
  if (_day <= 0){
   _month--;
   if (_month == 0){
    _month = 12;
    _year--;
   }
   _day = getday(_year, _month);
  }
 cout << _year << "---" << _month << "---" << _day << endl;
 return *this;
 }
private:
 int _year;
 int _month;
 int _day;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值