C++ 日期类的实现(附源码)
前言:在实现日期类的过程中,对于前面在类和对象中已经写过的内容,这里将不在重复赘述。希望在看本篇博客的友友,能将我前面在类和对象中的内容简单过一遍,这样在理解日期类的实现时,将更加容易。
在日常生活中日期对我们尤其重要,我们有时候会去计算第N天之后的日期是多少,然后安排自己的时间等。在模拟实现之前我们可以先在网上找一个日期计算器:
此时我们使用的这个日期类可以计算两个日期之间差多少天和推算几天之后的日期…在大概了解日期类的一些功能之后,我们就可以计划开始着手实现。
一.准备工作
在动手实现之前,我们需要先想好实现的功能有那些。
- 比较两个日期之间的大小。
- 日期加减N天之后的日期。
- 两个日期之间相差多少天。
二.实现
2.1类的成员变量和默认成员函数
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1);
void Print() const
{
cout << _year << '-' << _month << '-' << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
对于类中默认的6个成员函数是否需要我们显式实现。因为成员变量都是内置类型,因此构造函数需要我们自己实现,其他的使用编译器默认生成的就行。
Date::Date(int year, int month, int day)
{
//判断日期是否正确
//GetMonthDay函数的作用是返回当月的天数
if (month > 0 && month < 13 && day > 0 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "非法日期" << endl;
assert(false);
}
}
细节处理:在构造函数时需要判断输入的数据是否正确。
2.2 比较两个日期之间的大小
bool operator<(const Date& d) const;
bool operator==(const Date& d) const;
bool Date::operator<(const Date& d) const
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year && _month < d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day < d._day)
{
return true;
}
return false;
}
bool Date::operator==(const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
bool operator<=(const Date& d) const;
bool operator>(const Date& d) const;
bool operator>=(const Date& d) const;
bool operator!=(const Date& d) const;
//代码的复用
bool Date::operator<=(const Date& d) const
{
return ((*this < d) || (*this == d));
}
bool Date::operator>(const Date& d) const
{
return !(*this <= d);
}
bool Date::operator>=(const Date& d) const
{
return !(*this < d);
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
细节处理:当我们写了对日期的大或小和等于的判断之后,其他的比较运算符重载都可以在此基础上复用上面的代码。
2.3日期加减N天之后的日期
2.3.1 |N| > 1
在推算N天之后的日期之前,我们可以先写一个获取当前年当前月的天数的函数。然后将N天加在当前的天数后减去当前月的天数,月数加1,以此往复就能计算出N天之后的日期。
//方便在类外调用,可将其设为静态
static int GetMonthDay(int year, int month);
int Date::GetMonthDay(int year, int month)
{
//2次优化
//1.静态的局部变量,重复调用的时候,不用每次都创建
static int monthday[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//2.month == 2写在前面,不用每一次都先去判断闰年
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
return 29;
}
else
{
//使用数组方式返回比分支更方便
return monthday[month];
}
}
GetMonthDay函数的细节优化:
- 函数内表达式编译为指令后可能数量较多,编译器可能不会将其变为内联函数。可以将GetMonthDay函数设为静态,方便在类外直接访问。
- 因为GetMonthDay函数可能被多次调用,因此将数组设为静态,避免多次调用该函数时创建大量栈帧,消耗大。
- 函数内使用数组下标的方式返回,使的编写程序时和返回更加方便。
- 判断闰年时先判断month == 2,比先判断闰年在判断月数更高效。
- operator+=
operator+=运算符重载会返回当前日期加上N天之后的日期(当前日期被改变)。
Date& operator+=(int day);
Date& Date::operator+=(int day)
{
//day出现负数的情况
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
operator+=细节处理:
- 注意day为负数的情况。
- 为了方便对+=之后的日期的连续赋值,返回值为该类型的引用。
- operator+
operator+运算符重载会返回当前日期加上N天之后的日期的拷贝(当前日期未被改变)。
Date operator+(int day) const;
Date Date::operator+(int day) const
{
Date temp(*this);// 第1次拷贝构造
temp += day;
return temp; //第二次拷贝构造
}
注意:
- 若先实现operator+=之后operator+复用operator+=的代码,此时会调用2次拷贝构造函数(更高效)。
- 若是先实现operator+之后operator+=复用operator+的代码,将会调用4次拷贝构造函数。
Date Date::operator+(int day)
{
Date temp(*this); //第3次
temp._day += day;
while (temp._day > GetMonthDay(temp._year, temp._month))
{
temp._day -= GetMonthDay(temp._year, temp._month);
++temp._month;
if (temp._month == 13)
{
++temp._year;
temp._month = 1;
}
}
return temp; //第4次
}
Date& Date::operator+=(int day)
{
*this = *this + day; //传参和赋值 调用了2次
return *this;
}
同理日期减N天之后:
Date& operator-=(int day);
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0) //0号不合法
{
--_month;
//放这里,1月的情况
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
细节处理:
- _day = 0时是不符合的。
- (_month == 0)的判断应该放在–_month之后。
Date operator-(int day) const;
Date Date::operator-(int day) const
{
Date temp(*this);
temp -= day;
return temp;
}
2.3.2 N = 1
- 前置++和后置++
Date& operator++();
Date operator++(int);
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date temp(*this);
temp += 1;
return temp;
}
同理:
Date& operator--();
Date operator--(int);
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date temp(*this);
temp -= 1;
return temp;
}
注意:使用前置的加减,效率更高。
2.4两个日期之间相差多少天
int operator-(const Date& d);
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1; //返回时区分正负号
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int count = 0;
while (min != max)
{
++min;
++count;
}
return count * flag;
}
三.流插入和流提取重载
friend ostream& operator<<(ostream& out, Date& d);
friend istream& operator>>(istream& in, Date& d);
ostream& operator<<(ostream& out, Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
int year = 0, month = 0, day = 0;
in >> year >> month >> day;
if (month > 0 && month < 13 && day > 0 && day <= Date::GetMonthDay(year, month))
{
d._year = year;
d._month = month;
d._day = day;
}
else
{
cout << "非法日期" << endl;
assert(false);
}
return in;
}
四.源码
- 头文件
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
Date(int year = 1, int month = 1, int day = 1);
void Print() const
{
cout << _year << '-' << _month << '-' << _day << endl;
}
//重载有意义的运算符
bool operator<(const Date& d) const;
bool operator==(const Date& d) const;
bool operator<=(const Date& d) const;
bool operator>(const Date& d) const;
bool operator>=(const Date& d) const;
bool operator!=(const Date& d) const;
static int GetMonthDay(int year, int month);
Date& operator+=(int day);
Date operator+(int day) const;
Date& operator-=(int day);
Date operator-(int day) const;
//d1 - d2
int operator-(const Date& d);
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
private:
int _year;
int _month;
int _day;
};
- 源文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
Date::Date(int year, int month, int day)
{
//防止创建对象的时候出错
if (month > 0 && month < 13 && day > 0 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "非法日期" << endl;
assert(false);
}
}
bool Date::operator<(const Date& d) const
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year && _month < d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day < d._day)
{
return true;
}
return false;
}
bool Date::operator==(const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
//代码的复用
bool Date::operator<=(const Date& d) const
{
return ((*this < d) || (*this == d));
}
bool Date::operator>(const Date& d) const
{
return !(*this <= d);
}
bool Date::operator>=(const Date& d) const
{
return !(*this < d);
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
int Date::GetMonthDay(int year, int month)
{
static int monthday[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)))
{
return 29;
}
else
{
return monthday[month];
}
}
Date& Date::operator+=(int day)
{
//day出现负数的情况
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day) const
{
Date temp(*this);
temp += day;
return temp;
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day) const
{
Date temp(*this);
temp -= day;
return temp;
}
//两个日期相减
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int count = 0;
while (min != max)
{
++min;
++count;
}
return count * flag;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date temp(*this);
temp += 1;
return temp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date temp(*this);
temp -= 1;
return temp;
}
ostream& operator<<(ostream& out, Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
int year = 0, month = 0, day = 0;
in >> year >> month >> day;
if (month > 0 && month < 13 && day > 0 && day <= Date::GetMonthDay(year, month))
{
d._year = year;
d._month = month;
d._day = day;
}
else
{
cout << "非法日期" << endl;
assert(false);
}
return in;
}