手撸日期类
首先规定日期由年月日组成,因此日期类有三个成员变量,分别是_year,_month, _day。
class Date
{
private:
int _year;
int _month;
int _day;
};
在定义一个Date对象时,首先会自动调用构造函数用来初始化对象。需要注意的是,构造函数不写,编译器也会自动生成,但是自动生成的构造函数是不对基本类型成员变量进行初始化,而对于自定义类型的成员变量,会调用自定义类型的构造函数初始化。因此我们需要自己定义一个构造函数,初始化年月日。
Date(int year = 1999, int month = 2, int day = 2)
{
_year = year;
_month = month;
_day = day;
}
因为日期类没有资源要清理,所以析构函数可以不写,让编译自动生成。析构函数与构造函数类似,对于内置类型成员不处理,对自定义类型成员,会调用它的析构函数。
拷贝构造函数是构造函数的一个重载形式,参数只有一个,且必须使用引用传参,使用传值传参会发生无穷递归。同样的,编译器自动生成的拷贝构造函数对内置类型会完成浅拷贝,对于自定义类型会调用它的拷贝构造完成拷贝,当浅拷贝不满足要求时,需要我们完成深拷贝。对于日期类,浅拷贝足矣。
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
赋值运算符也是一种拷贝行为,不一样的是,拷贝构造是创建一个对象时,拿同类对象初始化,赋值拷贝的两个对象都已存在,被初始化过了。
Date& operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
对于日期来说,两个日期相减、日期加/减天数是有意义的。这就要用到运算符重载。两个日期相减如下。
int operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
int n = 0;
if (min > max)
{
max = d;
min = *this;
flag = -1;
}
while (min < max)
{
min++;
n++;
}
return n * flag;
}
日期加/减天数需要考虑天数是正数形式还是负数。+= 操作时,若天数为负数,就相当于 -= 操作。同理, -= 操作时,若天数为负数, 就像当于 += 操作。
int GetMonthDay(int year, int month)
{
static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
days[2] += 1;
}
return days[month];
}
Date& operator+=(int day)
{
if (day < 0)
{
*this -= -day;
}
else
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
}
return *this;
}
Date& operator-=(int day)
{
if (day < 0)
{
*this += -day;
}
else
{
_day -= day;
while (day <= 0)
{
_month--;
if (_month == 0)
{
_month == 12;
_year--;
}
_day += GetMonthDay(_year, _month);
}
}
}
代码链接
完整代码
|