1.要考虑到日期的合法性,如果不合法,置成1990.1.1
2.由于在日期类,没有用到资源的开辟,所以我们可以使用编译器自动合成的拷贝构造,赋值运算符重载等。
3.实现一个日期加上N天后,日期为多少
4.实现一个日期减去N天后,日期为多少
5.求两个日期之间相隔多少天
代码如下:
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{
if (!(year > 0 && month > 0 && month < 13 && day>0 && day <= GetDayOfMonth(year, month)))
{
_year = 1990;
_month = 1;
_day = 1;
}
}
bool isleap(int year) //判断是否是闰年
{
if ((0 == year % 4 && 0 != year % 100) || (year % 400 == 0))
{
return true;
}
else
{
return false;
}
}
int GetDayOfMonth(int year, int month)//每个月的天数不同,根据每个月来获取这个月的天数
{
int mon[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (2 == month)
{
if (isleap(year))
{
return mon[2] += 1;
}
}
return mon[month];
}
// 求当前日期第days天后是哪一天?
Date operator+(int days)
{
Date tmp(*this);
if (days < 0) // 加上一个负数的天数,相当于减去一个正数的天数
{
days = -days;
return tmp - days;
}
tmp._day += days;
while (tmp._day > GetDayOfMonth(tmp._year,tmp._month))
{
tmp._day -= GetDayOfMonth(tmp._year, tmp._month);
tmp._month++;
if (tmp._month > 12)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}
// 求当前日期第days天前是哪一天?
Date operator-(int days)
{
Date tmp(*this);
if (days < 0) //减去一个负数的天数,相当于加上一个正数的天数
{
days = -days;
return tmp + days;
}
tmp._day -= days;
while (tmp._day < 0)
{
tmp._month--;
if (tmp._month < 1)
{
tmp._year--;
tmp._month = 12;
}
tmp._day += GetDayOfMonth(tmp._year, tmp._month);
}
return tmp;
}
// 求两个日期之间的差值
int operator-(const Date& d)
{
int count = 0;
Date Mindate(*this); //假设最小
Date Maxdate(d); //假设最大
if (Mindate > Maxdate) //不满足,就交换两个对象,致使满足
{
Maxdate = Mindate;
Mindate = d;
}
while (Maxdate != Mindate)
{
Mindate++;
count++;
}
return count;
}
// 前置++
Date& operator++()
{
*this = *this + 1;
return *this;
}
// 后置++
Date operator++(int)
{
Date tmp(*this);
*this = *this + 1;
return tmp;
}
Date& operator--()
{
*this = *this - 1;
return *this;
}
Date operator--(int)
{
Date tmp(*this);
*this = *this - 1;
return tmp;
}
// 判断两个日期是否相等
bool operator==(const Date& d)
{
if (_year == d._year &&
_month == d._month &&
_day == d._day)
{
return true;
}
else
{
return false;
}
}
// 判断两个日期是否不等
bool operator!=(const Date& d)
{
if (*this == d)
{
return false;
}
else
{
return true;
}
}
bool 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;
}
else
{
return false;
}
}
bool operator>=(const Date& d)
{
if (*this > d || *this == d)
{
return true;
}
else
{
return false;
}
}
bool operator<(const Date& d)
{
if (*this > d)
{
return false;
}
else
{
return true;
}
}
bool operator<=(const Date& d)
{
if (*this >= d)
{
return false;
}
else
{
return true;
}
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2018, 5, 6);
Date d2(2026, 10, 25);
cout << d2 - d1 << endl;
d1++;
d1--;
--d1;
++d1;
//d1 - 999;
//d1 + 100;
//d1 - (-999);
return 0;
}
以上就是一个简单的日期类实现,如有建议,望得到你的建议,如有错误,望得到指正。