环境:win10,vs2013
Date.h
#include<stdlib.h>
using namespace std;
#include<iostream>
class Date
{
public:
Date();//默认构造函数
Date(int year, int month, int day);//构造函数
//赋值运算符重载:需要注意的四个问题:1.返回值是引用的形式 2.返回当前对象 3.判断自己给自己赋值 4.参数,传引用
Date& operator=(const Date&d);//赋值操作符重载
Date(const Date&d);//拷贝构造函数
void Print();
Date operator+(int days);//返回当前日期days天之后的日期
Date operator-(int days);//返回当前日期days天之前的日期
Date&operator++();//前置++
Date operator++(int d);//后置++
Date& operator--();//前置--
Date operator--(int d);//后置--
// 计算两个日期之间差距
int 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 int d);
Date operator -=(const int d);
private:
int _year;
int _month;
int _day;
//声明友元函数,可以在类外访问类的私有成员
friend int GetMonthDays(int year,int month);//得到某月的天数
friend bool IsleepYear(int year);//判断闰年
friend ostream&operator<<(ostream&_cout, const Date&d);//重载输出操作符
};
Date.cpp
#include"Date.h"
int GetMonthDays(int year,int month)//得到某月的天数
{
int dayOfMonth[13] = { 0, 31, 28, 30, 31, 30, 31, 31, 31, 30, 31, 30, 31 };//给13是为了让下标和月份对应
if (2 == month&&IsleepYear(year))
{
dayOfMonth[month] += 1;
}
return dayOfMonth[month];
}
bool IsleepYear(int year)//判断闰年
{
if ((0 == year % 4 && 0 != year % 100) || (0 == year % 400))
return true;
return false;
}
ostream&operator<<(ostream&_cout, const Date&d)
{
_cout << d._year << "-" << d._month << "-" << d._day;//这里不换行,因为不知道用户的需求,需不需要换行外部用户决定
return _cout;
}
Date::Date()//默认构造函数
{
_year = 1990;
_month = 1;
_day = 1;
}
Date:: Date(int year, int month, int day)
:_year(year)
, _month(month)
, _day(day)
{
//如果输入一个非法的日期,就把该日期改为1990年1月1日
if (!((year > 0) && (month > 0 && month<13) && (day>0 && day <= GetMonthDays(year, month))))
{
_year = 1990;
_month = 1;
_day = 1;
}
}
Date&Date:: operator=(const Date&d)//赋值操作符重载
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
Date::Date(const Date&d)//拷贝构造函数
{
_year = d._year;
_month = d._month;
_day = d._day;
}
void Date::Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
Date Date::operator+(int days)//返回当前日期days天之后的日期,加完之后需要判断日期是否合法
{
if (days < 0)//如果传过来的是负数,调用operator-()
return *this - (0 - days);
Date temp(*this);
temp._day += days;
int daysInMonth = 0;
while(temp._day>(daysInMonth = GetMonthDays(temp._year, temp._month)))
{
temp._day -= daysInMonth;
temp._month++;//减去某月的天数,月份增加
if (temp._month > 12)
{
temp._year += 1;
temp._month = 1;//年数加了之后,把月份初始化为1月
}
}
return temp;
}
Date Date::operator-(int days)//返回当前日期days天之前的日期
{
if (days < 0)
return *this+(0 - days);
Date temp(*this);
temp._day -= days;
while (temp._day <= 0)
{
temp._month -= 1;
if (temp._month == 0)
{
temp._year -= 1;
temp._month = 12;
}
temp._day += GetMonthDays(temp._year, temp._month);//把某月的天数加到temp._day,然后在判断循环
}
return temp;
}
Date&Date::operator++()//前置++
{
*this = *this + 1;
return *this;
}
Date Date:: operator++(int d)//后置++
{
Date temp(*this);
*this = *this + 1;
return temp;
}
Date& Date::operator--()//前置--
{
*this = *this -1;
return *this;
}
Date Date::operator--(int d)//后置--
{
Date temp(*this);
*this = *this - 1;
return temp;
}
bool Date::operator==(const Date&d)//判断是否相等
{
return _year == d._year&&_month == d._month&&_day == d._day;
}
bool Date::operator!=(const Date&d)//判断是否不相等
{
return !(*this == d);
}
bool Date::operator>(const Date&d)//判断是否大于
{
if ((_year > d._year)||(_year == d._year&&_month > d._month) || (_year == d._year&&_month == d._month&&_day > d._day))
return true;
return false;
}
bool Date::operator<(const Date&d)//判断是否小于
{
return!(*this>d);
}
// 计算两个日期之间差距
int Date::operator-(const Date& d)
{
Date minDate(*this);
Date maxDate(d);
if (minDate > maxDate)
{
minDate = d;
maxDate = *this;
}
int count = 0;
while (minDate != maxDate)
{
count++;
++minDate;
}
return count;
}
Date Date::operator +=(const int d)
{
*this = *this + d;
return *this;
}
Date Date::operator -=(const int d)
{
*this = *this - d;
return *this;
}
test.cpp
#include"Date.h"
int main()
{
Date d1(2017, 12, 5);
d1.Print();
Date d2(d1);
d2.Print();
Date d3 = d1;
d3.Print();
Date d4;
d4.Print();
d4 = d2;
d4.Print();
cout << endl;
cout <<d4 + 900 << endl;
Date d5(2018, 1, 12);
d5.Print();
cout << d5 - 55 << endl;
cout << endl;
d1++;
d1.Print();
++d1;
d1.Print();
d1--;
d1.Print();
--d1;
d1.Print();
cout << endl;
cout << d1 - d4 << endl;
cout << d4 - d5<< endl;
cout << endl;
d5 += 20;
d5.Print();
d5 -= 50;
d5.Print();
system("pause");
return 0;
}
运行结果: