日期类的实现主要包括以下操作:
(1)构造函数、析构函数、拷贝构造函数
(2)“=”运算符的重载
(3)“+”、“-”、“+=”、“-=”运算符的重载
(4)“++”、“--”运算符的重载
(5)“>”、“>=”、“<”、“<=”运算符的重载
1. “=”运算符的重载
在之前的博客有介绍过了,所以这里不再介绍。
2. “+”、“-”、“+=”、“-=”运算符的重载
首先我们实现“+=”运算符的重载,实现过程中要注意:day“+=”之后,可能会超过当前月的最大天数,这是就要++month,若month也超过了12,则year++....一直到年月日都满足实际情况,整“+=”操作才算结束。
对于“+”的重载,因为“+=”已经实现了,所以我们只要调用“+=”就可以了。这种函数的复用适用于所有类。
3. “++”、“--”运算符的重载
(1)实现的过程中,首先要注意的也是年月日是否符合实际情况;
(2)“++”可以分为前置++和后置++,对于前置++,返回值一定是加1之后 的结果,因为它是先++再运算;对于后置++返回值则是本来的数值,但是返回之后,对执行后置++的变量也要+1,因为它是先运算再++。这个原理也适用于“--”操作符
4. “>”、“>=”、“<”、“<=”运算符的重载
对于这些运算符的重载,判断条件和实际生活中判断一样。而且他们也可以进行复用。比如实现一个“>”和“==”就可以通过复用写出其他几个运算符的重载函数。
具体实现代码如下:
class Date
{
public:
Date(int year=1990, int month=1, int day=1)//构造函数
{
_year = year;
_month = month;
_day = day;
}
~Date()//析构函数
{}
Date(const Date& d)//拷贝构造函数
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
void Display()
{
cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
int GetMonthDay(int year, int month)
{
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 ,31};
if(month == 2 && ((year%400 == 0) || (year%4 == 0 && year%100 != 0)))
days[2] = 29;
return days[month];
}
(1)赋值运算符重载
因为是对一个已存在对象进行赋值,出作用域该对象还在,所以用引用返回
//Date& operator=(const Date& d1, const Date& d2);
Date& operator=(const Date& d)
{//赋值时d1=d3, d1即this,d3即传入的d
if(this != &d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}
//(2)"<"运算符重载
bool operator<(const Date& d)//运算符重载
{//这里比较时d1<d2,d1相当于隐藏的this,d2相当于传入的d
if(_year < d._year)
return true;
else if(_year == d._year)
{
if(_month < d._month)
return true;
else if(_month == d._month)
{
if(_day < d._day)
return true;
}
}
return false;
}
//(3)"=="运算符的重载
bool operator==(const Date& d)
{
return (_year==d._year && _month==d._month && _day==d._day);
}
//(4)"<="运算符的重载
bool operator<=(const Date& d)
{
return (*this<d || *this==d);
}
//(5)">"运算符的重载
bool operator>(const Date& d)
{//这里不仅在逻辑上执行代码有开销,且在调用函数时也有开销
return !(*this <= d);
}
//(6)">="运算符重载
bool operator>=(const Date& d)
{
return !(*this < d);
}
//(7)"-="运算符重载
Date operator-=(int day)
{
_day -= day;
while(_day <= 0)
{
_month--;
if(_month == 0)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
//(8)"-"运算符重载
Date operator-(int day)
{//因为该运算符,只是得到减后的结果,并不要改变原数的值,所以不能直接用this去减
Date ret(*this);//拷贝构造函数,ret拷贝this的值
ret -= day;
return ret;
}
//(9)"+="运算符重载
Date operator+=(int day)
{
_day += day;
while(_day >GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if(_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
//(10)"+"运算符重载
Date operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
//(11)"++"运算符重载
//1)前置++ ->在返回时本身值就加1了
Date& operator++()
{
*this += 1;
return *this;
}
//2)后置++ ->在返回时本身值还未加1,返回后+1
Date operator++(int)
{
Date ret(*this);
*this += 1;
return ret;
}
//(12)"--"运算符重载
//1)前置--
Date& operator--()
{
*this -= 1;
return *this;
}
//2)后置--
Date operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
private:
int _year;
int _month;
int _day;
};
//Date& Date::operator=(const Date& d1, const Date& d2)
//{//赋值时d1=d3, d1即this,d3即传入的d
// if(d1 != d2)
// {
// d1._year = d2._year;
// d1_month = d2._month;
// d1_day = d2._day;
// }
// return d1;
//}
//Date& Date::operator=(const Date& d)
//{//赋值时d1=d3, d1即this,d3即传入的d
// if(this != &d)
// {
// this->_year = d._year;
// this->_month = d._month;
// this->_day = d._day;
// }
// return *this;
//}
//bool Date::operator<(const Date& d)
//{
// if(_year < d._year)
// return true;
// else if(_year == d._year)
// {
// if(_month < d._month)
// return true;
// else if(_month == d._month)
// {
// if(_day < d._day)
// return true;
// }
// }
// return false;
//}
这里没有给出测试用例,大家在写测试用例时尽量将所有可能出现的情况都测试一下。