Date(日期)类:
练习实现类的成员函数。
运算符的重载
Date.h
#include <assert.h>
class Date
{
//成员函数
public:
Date(){}
Date(int year, int month, int day); //全缺省构造函数
Date(const Date& d); //拷贝构造
Date& operator=(const Date& d); //赋值运算符的重载 >> 返回引用,防止连续赋值
~Date(){}
bool IsInvalid(); //判断日期是否合法
bool IsLeapYear(int year); //检测闰年
int GetMonthDay(int year, int month);//得到这个月有多少天
void Show();
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+(int day);
Date& operator+=(int day);
Date operator-(int day);
Date& operator-=(int day);
int operator-(const Date& d);
Date& operator++()//前置---比后置效率高(不创建对象)
{
*this += 1;
return *this;
}
Date operator++(int)//后置(创建空间, 调用了拷贝构造)
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date& operator--()//前置
{
*this -= 1;
return *this;
}
Date operator--(int)//后置 int占位
{
Date& tmp(*this);
*this -= 1;
return tmp;
}
private: //成员变量
int _year;
int _month;
int _day;
};
Date.cpp
#include "Test_class.h"
#include <stdio.h>
/////////////////////////////////////////////类成员函数
Date::Date(int year = 1900, int month = 1, int day = 1) //全缺省构造函数
{
_year = year;
_month = month;
_day = day;
if (!IsInvalid()) //日期非法
{
assert(false); //直接中断
}
}
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
bool Date::IsInvalid()
{
if (_year > 0 &&
_month > 0 && _month < 13 &&
_day > 0 && _day < GetMonthDay(_year, _month))
return true;
return false;
}
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 days[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = days[month];
if (month == 2 && IsLeapYear(year))
{
day = 29;
}
return day;
}
void Date::Show()
{
printf("%d-%d-%d", _year, _month, _day);
}
/////////////////////////////////////////////////////运算符重载
Date& Date::operator=(const Date& d) //赋值运算符的重载 >> 返回引用,防止连续赋值
{
if (this != &d) //检测是否是自身赋值
{
this->_year = d._year;
this->_month = d._month;
this->_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 == d);
}
bool Date::operator>=(const Date& d)
{
return !(*this < d);
}
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)
{
if (_year < d._year ||
_year == d._year && _month < d._month ||
_year == d._year && _month == d._month && _day < d._day)
return true;
return false;
}
//////////////////////////////////////////////////////日期计算
Date Date::operator+(int day)
{
if (day < 0)
{
return *this - (-day);
}
Date ret(*this);
ret._day += day;
while (ret.IsInvalid() == false)//加的day不合法
//while (ret._day > GetMonthDay(ret._year, ret._month)) //加完后只要天不合法即为不合法
{
int monthday = GetMonthDay(ret._year, ret._month);
ret._day -= monthday;
ret._month++;
if (ret._month == 13)
{
ret._year++;
ret._month = 1;
}
}
return ret;
}
Date& Date::operator+=(int day)
{
*this = *this + day;
return *this;
}
//d1 - 100
Date Date::operator-(int day)
{
if (day < 0)
{
return *this + (-day);
}
Date ret(*this);
ret._day -= day;
while (ret._day <= 0) //小于等于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;
}
//d1 - d2
int Date::operator-(const Date& d)
{
int flag = 1;
Date max(*this);
Date min(d);
int retday = 0;
if (max < min)
{
max = d;
min = *this;
flag = -1;
}
while (min < max)
{
min++;
retday++;
}
return retday*flag;
}