自主實現C  日期類操作符過載

自主實現C date類

#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
//建構函式(全預設)d1
Date(int year=1900, int month=1, int day=1)
//建構函式的初始化
:_year(year)   
, _month(month)
, _day(day)
{
if(!IsInvalid())
{
assert(false);
}
}
//拷貝建構函式  Date d2(d1)
Date(const Date& d)
{
_year=d._year;
_month=d._month;
_day=d._day;
}
~Date()
{}
bool IsInvalid();
void Show()
{
cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
//賦值運算子過載  d2=d3 (兩個物件已經存在)         
Date& operator=(const Date& d);  
bool operator==(const Date& d); // ==過載
bool operator!=(const Date& d); // !=過載
bool operator>=(const Date& d); //  >=過載
bool operator<=(const Date& d); //  <=過載
//d1<d2     // >過載
bool operator>(const Date& d);  
//d1>d2    // <過載
bool operator<(const Date& d);
//d1 10     // 過載
Date operator (int day);
//d1 =10   // =過載
Date& operator =(int day);
Date operator-(int day);
Date& operator-=(int day);
//  d1       //前置  
Date& operator  ();
//d1        //後置  
Date  operator  (int);
Date& operator--();
Date operator--(int);
//計算兩個日期相差的天數
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
bool IsLeapYear(int year)
{
if(((year%4==0)&&(year%100==0))||year%400==0)
{
return true;
}
return false;
}
int GetMonthDay(int year, int month)
{
int arr[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
int day=arr[month];
if(month==2&&IsLeapYear(year))
{
day=29;
}
return day;
}
bool Date::IsInvalid()
{
return _year>=0
&&_month>=0&&_month<=12
&&_day>0&&_day<=GetMonthDay(_year,_month);
}
Date& Date::operator=(const Date& d)
{
if(this!=&d)
{
_year=d._year;
_month=d._month;
_day=d._day;
}
return *this;
}
bool Date::operator==(const Date& d)
{
return this->_year==d._year
&&this->_month==d._month
&&this->_day==d._day;
}
bool Date::operator!=(const Date& d)
{
return this->_year!=d._year
||this->_month!=d._month
||this->_day!=d._day;
}
bool Date::operator<=(const Date& d)
{
return *this<d||*this==d;
}
bool Date::operator>=(const Date& d)
{
return !(*this<d);
}
bool Date::operator>(const Date& d)
{
return !(*this<=d);
}
bool Date::operator<(const Date& d)
{
if(_year<d._year)
{
return true;
}
else if(_month<d._month)
{
return true;
}
else
{
return false;
}
}
Date Date::operator (int day)    //本身並沒有改變,不能返回引用
{
Date ret(*this);
ret._day =_day;
while(ret._day>GetMonthDay(ret._year, ret._month))
{
int monthday=GetMonthDay(ret._year, ret._month);
ret._day-=monthday;
ret._month  ;
if(ret._month>12)
{
ret._month=1;
ret._year  ;
}
}
return ret;
}
Date& Date::operator =(int day)
{
*this=*this day;
return *this;
}
Date Date::operator-(int day)
{
if(day<0)
{
return *this (-day);
}
Date ret(*this);
ret._day-=day;
while(ret._day<0)
{
if(ret._month==1)
{
ret._month=12;
ret._year--;
}
else
{
ret._month--;
}
int monthday=GetMonthDay(ret._year, ret._month);
ret._day =monthday;
}
return ret;
}
Date& Date::operator-=(int day)
{
*this=*this-day;
return *this;
}
//前置    效率高
Date& Date::operator  ()
{
*this =1;
return *this;
}
//後置
Date Date::operator  (int)
{
Date tmp(*this);
tmp=*this 1;
return tmp;
}
Date& Date::operator--()
{
*this-=1;
return *this;
}
Date Date::operator--(int)
{
Date tmp(*this);
tmp=*this-1;
return tmp;
}
//計算兩個日期相差天數
int Date::operator-(const Date& d)
{
//先確定那個日期小,然後將小的往大的加,直到兩個日期相等,就得到相差天數
int flag=1;
Date max(*this);
Date min(d);
if(*this<d)
{
min=*this;
max=d;
flag= -1;
}
int days=0;
while(min<max)
{
min  ;
days  ;
}
return days*flag;
}
int main()
{
Date d1(2018,3,26);
d1.Show();
Date d2(2018,4,25);
d2=d1;
d2.Show();
cout<<"測試過載==:"<<d1.operator==(d2)<<endl;
cout<<"測試過載!=:"<<d1.operator!=(d2)<<endl;
cout<<"測試過載>:"<<d1.operator>(d2)<<endl;
cout<<"測試過載<:"<<d1.operator<(d2)<<endl;
cout<<"測試過載>=:"<<d1.operator>=(d2)<<endl;
cout<<"測試過載<=:"<<d1.operator<=(d2)<<endl;
Date d3=d2.operator (10);
d3.Show();
d2.operator =(20);
d2.Show();
Date d4=d2.operator-(-30);
d4.Show();
d2.operator-=(30);
d2.Show();
Date d5=d2  ;
d5.Show();
d2;
d2.Show();
--d2;
d2.Show();
Date d6=d2--;
d6.Show();
Date d(2018,3,27);
Date d7(2018,9,10);
d.Show();
d7.Show();
int days=d7-d;
cout<<"相差天數:"<<days<<endl;
return 0;
}

這裡寫圖片描述