1、构造函数
2、拷贝构造函数
3、常见运算符的重载
4、函数的复用 实现的功能:
1)、在当前基础下加一天,减一天后的日期
2)、在当前基础下加多天,减多天后的日期
3)、当前日期与要计算的日期相差多少天
Date.h
#define _CRT_SECURE_NO_WARNING 1
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
Date(int year = 2018, int month = 6, int day = 15)
: _year(year)
, _month(month)
, _day(day)
{
if(month<1 || month>12 || day<0 || day>GetMonthDay(_year,_month))
assert(false);
}
Date(const Date& d)
: _year(d._year)
, _month(d._month)
, _day(d._day)
{}
// 当前日期days天后是什么日期?
Date operator+(int days);
int GetMonthDay(int year,int month);
bool isLeapYear(int year);
// 当前日期days天前是什么日期?
Date operator-(int days);
// 两个日期之间差了多少天?
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);
bool operator==(const Date& d);
bool operator!=(const Date& d);
Date& operator=(const Date& d);
// 重载取地址符号
Date* operator&();
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 前置--
Date& operator--();
// 后置++
Date operator--(int);
void show();
private:
int _year;
int _month;
int _day;
};
Date.cpp
#define _CRT_SECURE_NO_WARNING 1
#include "Date.h"
bool Date::isLeapYear(int year)
{
if( (year % 4 == 0) && (year % 100 != 0) ||(year % 400 == 0) )
return true;
return false;
}
int Date::GetMonthDay(int year,int month)
{
int MonthDay[13] = {0,31,28,31,30,31,30,31,30,30,31,30,31};
if(month == 2&&isLeapYear(year))
{
MonthDay[2] = 29;
}
int day = MonthDay[month];
return day;
}
// 当前日期days天后是什么日期?
Date Date::operator+(int days)
{
if(days<0)
return *this-(days);
Date ret(*this);
ret._day += days;
while(ret._day > GetMonthDay(ret._year,ret._month))
{
ret._day -= GetMonthDay(ret._year,ret._month);
ret._month++;
if(ret._month == 13)
{
ret._year++;
ret._month = 1;
}
}
return ret;
}
// 当前日期days天前是什么日期?
Date Date::operator-(int days)
{
if(days<0)
return *this+(-days);//当前日期加上该天数
Date ret(*this);
ret._day -= days;
while(ret._day <= 0)
{
ret._month--;
if(ret._month == 0)
{
ret._year--;
ret._month = 12;
}
ret._day += GetMonthDay(ret._year,ret._month);
}
return ret;
}
// 两个日期之间差了多少天?
Date Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;//标记当前日期和另一个日期相差的正负
if( (*this) < d )
{
max = d;
min = *this;
flag = -1;
}
int day = 0;//标记相差的天数
while(min <= max)
{
min++;
day++;
}
return day*flag;//正,表示当前日期在后,负,表示当前日期在前
}
// 日期比大小
bool Date::operator>(const Date& d)
{
if(_year > d._year ||
_year == d._year && _month > d._month ||
_year == d._year && _month == d._month && _day > d._year)
{
return true;
}
return false;
}
bool Date::operator<(const Date& d)
{
if(_year < d._year ||
_year == d._year && _month < d._month ||
_year == d._year && _month == d._month && _day < d._year)
{
return true;
}
return false;
}
bool Date::operator==(const Date& d)
{
if( (_year = d._year) &&
(_month = d._month) &&
(_day = d._day) )
{
return true;
}
return false;
}
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
bool Date::operator>=(const Date& d)
{
return !(*this<d);
}
bool Date::operator<=(const Date& d)
{
return (*this == d || *this < d);
}
//赋值运算符的重载
Date& Date::operator=(const Date& d)
{
if( (*this) != d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._year;
}
return (*this);
}
// 重载取地址符号
Date* Date::operator&()
{
return this;
}
// 前置++
Date& Date::operator++()
{
*this = *this+1;
return *this;
}
// 后置++
Date Date::operator++(int)
{
Date temp(*this);
*this = *this+1;
return temp;
}
// 前置--
Date& Date::operator--()
{
*this = *this-1;
return *this;
}
// 后置++
Date Date::operator--(int)
{
Date temp(*this);
*this = *this-1;
return (*this);
}
void Date::show()
{
cout<<_year<<"/"<<_month<<"/"<<_day<<endl;
}
test.cpp
#define _CRT_SECURE_NO_WARNING 1
#include "Date.h"
int main()
{
Date d1(2018,6,10);
d1.show();
/*Date d2(2018,3,14);
d2.show();
Date d3(2018,3,2);
d3.show();
(d1+30).show();
(d1+(-4)).show();
(d1-5).show();
(--d1).show();
(++d1).show();
(d1++).show();
(d1--).show();
d1=d2=d3;*/
Date d2(2018,3,10);
cout<< (d1==d2) <<endl;
cout<< (d1>=d2) <<endl;
cout<< (d1<=d2) <<endl;
return 0;
}