#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Date
{
//友元函数
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
private:
int _year;
int _month;
int _day;
public:
int GetMonthDay(int year, int month)
{
static int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// 是2月且是闰年返回29
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
return 29;
}
return monthDays[month];
}
Date (int year = 1, int month = 1, int day = 1)
{
if (year > 0 && (month > 0 && month < 13) && (day > 0 && day <= GetMonthDay(year, month)))
{
_year = year;
_month = month;
_day = day;
//cout << "Date" << endl;
}
else
cout << "非法日期" << endl;
}
// < == > <= >= !=
inline bool operator<(const Date& d) const //const 是给 this 指针加的
{
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;
}
inline bool operator==(const Date& d) const
{
return _year == d._year && _month == d._month && _day == d._day;
}
inline bool operator>(const Date& d) const
{
return !(*this < d || *this == d);
}
bool operator<=(const Date& d) const
{
return !(*this > d);
}
bool operator>=(const Date& d) const
{
return !(*this < d);
}
bool operator!=(const Date& d) const
{
return !(*this == d);
}
//-= += + - 前置-- 后置-- 前置++ 后置++
//d1 += 100 d1也会改变
Date& operator+=(int day)
{
if (day >= 0)
_day += day;
else
{
//如果输入的day是负数,就去调 operator-=
return *this -= -day; //记得是-day,不然会造成无限循环调用
}
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month); //这里注意,先变日期在变月份
++_month;
if (_month > 12)
{
++_year;
_month = 1;
}
}
return *this;
}
//d1 -= 100 d1也会改变
Date& operator-=(int day)
{
if (day >= 0)
_day -= day;
else
{
//如果_day 是负数就去调 operator-=
return *this += -day; //记得是-day,不然会造成无限循环调用
}
while (_day < 1)
{
_day += GetMonthDay(_year, _month); //注意点同上
--_month;
if (_month < 1)
{
--_year;
_month = 12;
}
}
return *this;
}
//d1 + 100 d1不会改变
Date operator+(int day) const
{
Date ret = *this;
ret += day;
return ret; //这里返回的是临时变量,出了作用域会自动销毁,所以不用 Date&
}
//d1 - 100 d1不会改变
Date operator-(int day) const
{
Date ret = *this;
ret -= day;
return ret;
}
//前置++
Date& operator++()
{
return *this += 1;
}
//后置++,为了构成重载所以加了一个int
Date operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
//前置--
Date& operator--()
{
return *this -= 1;
}
//后置--
Date operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
// & 取地址
const Date* operator&() const //参数为 const Date* this
{
return this; //返回的也是const Date* ,如果函数名不加const,返回值类型就变成了 Date* ,不符合
}
void print() //const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
int operator- (const Date & d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (max < min)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (max != min)
{
++min;
++n;
}
return n * flag;
}
};
//重载 cout
ostream& operator<<(ostream& out,const Date& d)
{
out << d._year << "-" << d._month << "-" << d._day << endl;
return out;
}
//重载 cin
istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}
int main()
{
Date d1(2020, 4, 5);
Date d2(2020, 4, 20);
cin >> d1 >> d2;
cout << d1 << d2;
//fun(d1);
/*Date d1(2020, 4, 15);
d1 += 300;
int i = 0;
++i; i是整形变量,++不会去调Date类型的++,因为类型不匹配
++d1;*/
/*d1.print();*/
/*Date d2 = d1 + -100;*/
/*Date d2 = d1--;
d2.print();*/
/*Date d1(2020, 4, 15);
Date d2(2021, 4, 15);
cout <<d2 - d1 << endl;*/
return 0;
}
C++ 模拟日期类的实现
最新推荐文章于 2022-07-16 10:49:59 发布