class-date.h
#pragma once
#include <iostream>
using namespace std;
构造、析构、拷贝构造函数:
class Date
{
public :
Date ( int year = 1900 , int month = 1 , int day = 1 )
{
if ( _year >= 1900 && _month > 0 && _month < 13 && _day
> 0 && _day < GetMonthDay ( year, month) )
{
this - > _year = year;
this - > _month = month;
this - > _day = day;
}
}
~ Date ( )
{
}
Date ( const Date& d)
{
_year = d. _year;
_month = d. _month;
_day = d. _day;
}
赋值运算符的重载:
Date& operator = ( const Date& d)
{
if ( _year != d. _year&& _month != d. _month&& _day != d. _day)
{
_year = d. _year;
_month = d. _month;
_day = d. _day;
}
return * this ;
}
运算符的重载:
+号:
Date operator + ( int days)
{
Date tmp ( * this ) ;
tmp. _day + = days;
return tmp;
}
-号:
Date operator - ( int days)
{
Date tmp ( * this ) ;
tmp. _day - = days;
return tmp;
}
前置++:
Date& operator ++ ( )
{
* this = * this + 1 ;
return * this ;
}
后置++:
Date operator ++ ( int )
{
Date tmp ( * this ) ;
* this = * this + 1 ;
return tmp;
}
+=运算:
Date& operator + = ( int days)
{
_day + = days;
while ( _day > GetMonthDay ( _year, _month) )
{
_day - = GetMonthDay ( _year, _month) ;
_month + = 1 ;
if ( _month > 12 )
{
_year + = 1 ;
_month = 1 ;
}
}
return * this ;
}
-=运算:
Date& operator - = ( int days)
{
while ( _day < days)
{
_month - = 1 ;
if ( _month == 0 )
{
_year - = 1 ;
_month = 12 ;
}
_day + = GetMonthDay ( _year, _month) ;
}
_day - = days;
return * this ;
}
比较运算符的重载:
>:
bool operator > ( const Date& d)
{
if ( _year == d. _year)
{
if ( _month = d. _month)
{
if ( _day > d. _day)
{
return true ;
}
return false ;
}
else if ( _month > d. _month)
{
return true ;
}
return false ;
}
else if ( _year > d. _year)
{
return true ;
}
return false ;
}
<:
bool operator < ( const Date& d)
{
return ! ( * this > d) && ( * this != d) ;
}
>=:
bool operator >= ( const Date& d)
{
return ! ( * this < d) ;
}
<=:
bool operator <= ( const Date& d)
{
return ! ( * this > d) ;
}
==:
bool operator == ( const Date& d)
{
if ( _year == d. _year&& _month == d. _month&& _day == d. _day)
{
return true ;
}
return false ;
}
!=:
bool operator != ( const Date& d)
{
if ( _year == d. _year&& _month == d. _month&& _day == d. _day)
{
return false ;
}
return true ;
}
得出日期相差天数:
int operator - ( const Date& d)
{
Date min ( * this ) ;
Date max ( d) ;
int flag = - 1 ;
if ( * this > d)
{
max = * this ;
min = d;
flag = 1 ;
}
int count = 0 ;
while ( min != max)
{
count++ ;
min+ = 1 ;
}
return flag* count;
}
打印日期:
void PrintDate ( )
{
cout << _year << "-" << _month << "-" << _day << endl;
}
获取当前月份天数:
int GetMonthDay ( int year, int month)
{
int array[ 13 ] = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ;
if ( month == 2 && isYear ( year) == true )
{
++ array[ 2 ] ;
}
return array[ month] ;
}
判断是否是闰年:
bool isYear ( int year)
{
if ( year % 400 == 0 )
{
return true ;
}
else if ( year % 100 != 0 && year % 4 == 0 )
{
return true ;
}
return false ;
}
private :
int _year;
int _month;
int _day;
} ;
class-date.c
#include "class-date.h"
int main ( )
{
Date d1 ( 2019 , 6 , 18 ) ;
cout << "d1:" ;
d1. PrintDate ( ) ;
cout << endl;
Date d2;
d2 = d1 - 1 ;
cout << "d2=d1-1:" ;
d2. PrintDate ( ) ;
cout << endl;
Date d3 ( 2019 , 6 , 17 ) ;
cout << "d3:" ;
d3. PrintDate ( ) ;
cout << endl;
if ( d3 == d2)
{
cout << "d3 is the same as d2 !" << endl;
}
cout << endl;
cout << "d3-=40:" ;
d3 - = 40 ;
d3. PrintDate ( ) ;
cout << endl;
int days = d3 - d2;
cout << "d3与d2相差:" ;
cout << days << endl;
cout << endl;
if ( d3 < d2)
{
cout << "d3 is earlier than d2 !" << endl;
}
cout << endl;
cout << "出生日期:" ;
Date d4 ( 1996 , 9 , 10 ) ;
d4. PrintDate ( ) ;
cout << endl;
d4 + = 10000 ;
cout << "出生10000天纪念日:" ;
d4. PrintDate ( ) ;
cout << endl;
system ( "pause" ) ;
return 0 ;
}
代码运行测试图: