前言
之前我们已经学习了四个默认成员函数(构造、析构、拷贝构造、运算符重载)
今天我们就来尝试用已经学到的知识进行日期类的完整实现
提示:
依然是分成三个文件来写 只是在讲解的时候就不区分文件了 完整的代码在文章最后
并且之前文章中提到的知识在此不会再详细介绍
Date类创建
//对类的成员变量和函数进行声明
class Date
{
public:Date(int year = 1, int month = 1, int day = 1);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);private:int _year;int _month;int _day;
};
构造函数
//一个简单的构造函数
Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;//_year _month _day前加不加this->都可以 加上只是为了方便理解
}
比较功能
==
bool Date::operator==(const Date& d)
{return _year == d._year&& _month == d._month&& _day == d._day;
}
<
比较大小 可以看看其中的逻辑 其他没啥需要强调的
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;
}
<=
有了<和==的重载 我们先来看看<=的重载能不能用上前二者
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)
{return !(*this < d);
}
!=
那就是==的逻辑取反
bool Date::operator!=(const Date& d)
{return !(*this == d);
}
运算功能
辅助函数
创建一个函数用于辅助计算
int GetMonthDay(int year, int month){assert(month > 0 && month < 13);static int monthDays[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;}return monthDays[month];}
类+=天数
Date& Date::operator+=(int 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)
{Date tmp = *this;//这里是拷贝构造// 因为tmp原本不存在 是新创建的//相当于 Date tmp(*this);tmp += day;return tmp;
}
类-=天数
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}
类-天数
Date Date::operator-(int day)
{Date tmp = *this;tmp -= day;return tmp;
}
类前置++
Date& Date::operator++()
{*this += 1;return *this;
}
类后置++
Date Date::operator++(int)
{Date tmp = *this;*this += 1;return tmp;
}
类-类
int Date::operator-(const Date& d)
{int flag = 1;Date max = *this;Date min = d;if (*this < d){int flag = -1;swap(d, *this);}int n = 0;while (min != max){++min;++n;}return n * flag;
}
输入输出
对于输入输出
cpp规定
成员函数的重载,this是第一个参数,所以Date自动成为第二个参数,所以他就只能是左操作数了
我们可以写成全局函数 但是不行的因为成员变量是私有的 无法访问
输出(流插入)
我们可以用友元函数来解决这个问题 详见另一篇文章
代码如下
//在类里面声明
friend ostream& operator<<(ostream& out, const Date& d);//在类外面实现
ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" <<d._month << "月" << d._day << "日" << endl;return out;
}
对于流插入的详细介绍到后期再详细介绍 现在只是了解即可
输入(流提取)
friend istream& operator>>(istream& out, Date& d);
//d不加constistream& operator>>(istream& out, Date& d)
{cout << "输入年月日" << endl;in << d._year << d._month << d._day;return in;
}
检查
实现一个check函数来检查输入的日期是否合法
//用于构造函数和流提取中
bool CheckInvalid()
{if(_year <= 0 || _month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month))return false;
}
结语
关于日期类的简单实现到这里就结束了 属于是综合了前面的众多知识 还是需要好好消化一番的 希望对你有帮助 我们下次见~~