系列文章目录
文章目录
日期类的实现难点在于运算符重载,而运算符重载的要点在于函数复用
代码如下:
#include<iostream>
using namespace std;
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
int day[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)))
{
day[2] = 29;
}
return day[month];
}
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
:_year(year), _month(month), _day(day)
{
//cout << "构造函数" << endl;
}
// 拷贝构造函数
// d2(d1)
Date(const Date& d)
{
//cout << "拷贝构造函数" << endl;
_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d)
{
//cout << "赋值重载函数" << endl;
if (&d != this)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
// 析构函数
~Date()
{
//cout << "析构函数:" << _year << ' ' << _month << ' ' << _day << endl;
_year = 0;
_month = 0;
_day = 0;
}
// 日期+天数
Date operator+(int day)
{
if (day < 0)
{
return (*this) - (-day);
}
Date tem(*this);
tem._day += day;
while (tem._day > GetMonthDay(tem._year, tem._month))
{
tem._day -= GetMonthDay(tem._year, tem._month);
tem._month++;
if (tem._month == 13)
{
tem._year++;
tem._month = 1;
}
}
return tem;
}
// 日期+=天数
Date& operator+=(int day)
{
*this = *this + day;
return *this;
}
// 日期-天数
Date operator-(int day)
{
if (day < 0)
{
return (*this) + (-day);
}
Date tem(*this);
tem._day -= day;
while (tem._day <= 0)
{
tem._month--;
if (tem._month == 0)
{
tem._year--;
tem._month = 12;
}
tem._day += GetMonthDay(tem._year, tem._month);
}
return tem;
}
// 日期-=天数
Date& operator-=(int day)
{
(*this) = (*this) - day;
return *this;
}
// 前置++
Date& operator++()
{
*this += 1;
return *this;
}
// 后置++
Date operator++(int)
{
Date tem(*this);
*this += 1;
return tem;
}
// 后置--
Date operator--(int)
{
Date tem(*this);
*this -= 1;
return tem;
}
// 前置--
Date& operator--()
{
*this -= 1;
return *this;
}
// >运算符重载
bool 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;
}
// ==运算符重载
bool operator==(const Date& d)
{
return _year == d._year && _month == d._month && _day == d._day;
}
// >=运算符重载
bool operator >= (const Date& d)
{
return *this > d || *this == d;
}
// <运算符重载
bool operator < (const Date& d)
{
return !(*this >= d);
}
// <=运算符重载
bool operator <= (const Date& d)
{
return !(*this > d);
}
// !=运算符重载
bool operator != (const Date& d)
{
return !(*this == d);
}
// 日期-日期 返回天数
int operator-(const Date& d)
{
int flag = 1;
Date max = (*this);
Date min = d;
if ((*this) < d)
{
max = d;
min = (*this);
flag = -1;
}
int count = 0;
while (max != min)
{
++min;
++count;
}
return flag * count;
}
//友元声明(可以放在类的任何地方,不受三大限定符的影响)
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& ipt, Date& d);
void Print()
{
cout << _year << ' ' << _month << ' ' << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
//流插入重载
inline ostream& operator<<(ostream& out, const Date& d)
{
out << d._year <<' ' << d._month <<' ' << d._day << endl;
return out;
}
//流提取重载
inline istream& operator>>(istream& ipt, Date& d)
{
ipt >> d._year >> d._month >> d._day;
return ipt;
}
//测试
void test()
{
Date d1(2003, 6, 18);
Date d2(d1);
Date d3;
d1.Print();
d2.Print();
d3.Print();
if (d1 == d2)
{
cout << "d1==d2" << endl;
}
if (d1 != d3)
{
cout << "d1!=d3" << endl;
}
if (d1 > d3)
{
cout << "d1>d3" << endl;
}
if (d1 >= d3)
{
cout << "d1>=d3" << endl;
}
if (d3 < d1)
{
cout << "d3<d1" << endl;
}
if (d3 <= d1)
{
cout << "d3<=d1" << endl;
}
d1 += 1000;
d1.Print();
d1 -= 1000;
d1.Print();
Date tem1 = d1++;
Date tem2 = ++d1;
Date tem3 = d1--;
Date tem4 = --d1;
tem1.Print();
tem2.Print();
tem3.Print();
tem4.Print();
cout << d1 - d3 << endl;
Date d5;
cout << "输入时间(年:月:日)" << endl;
cin >> d5;
cout << d5;
}
int main()
{
test();
return 0;
}
运行结果: