前言
💬 hello! 各位铁子们大家好哇。 今日更新了类与对象日期计算器的内容 🎉 欢迎大家关注🔍点赞👍收藏⭐️留言📝
头文件
#include<iostream> #include<assert.h> using namespace std;
class Date
{
public:
bool CheckInvalid() const;
Date(int year = 1, int month = 1, int day = 1);
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;Date& operator+=(int day); Date operator+(int day) const; Date operator-(int day) const; Date& operator-=(int day); //++d1 Date& operator++(); //为了跟前置++区分,强行增加一个int形参,构成重载区分 //d1++ Date operator++(int); //日期-日期 int operator-(const Date& d) const; //如果不声明和定义分离,本质就是内联 int GetMonthDay(int year, int month) const { 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]; } void Print() const { cout << _year << "/" << _month << "/" << _day << endl; } //void operator<<(ostream& out) //{ // out << _year << "年" << _month << "月" << _day << "日" << endl; //} //友元声明 friend ostream& operator<<(ostream& out, const Date& d); friend istream& operator>>(istream& in, Date& d);
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
声明流插入<<和流提取>>时,应在类外面声明,不然this指针会占据第一个形参,Date就只能是左操作数了,打印时就会变成d1<<cout; ,不符合常规写法。在类外面声明时,为了不把private打开,可以进行友元声明,就可以在不打开private的情况下,访问private。
函数实现
#include"Date.h"
Date::Date(int year , int month, int day )
{
_year = year;
_month = month;
_day = day;if (!CheckInvalid()) { cout << "构造日期非法" << endl; }
}
bool Date::operator<(const Date& d) const
{
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) 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 _year == d._year
&& _month == d._month
&& _day == d._day;
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}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) const
{
//Date tmp(*this);
Date tmp = *this; //拷贝构造
tmp += day;return tmp;
}
//Date Date::operator+(int day)
//{
// //Date tmp(*this);
// Date tmp = *this; //拷贝构造
//
// tmp._day += day;
// while (tmp._day > GetMonthDay(tmp._year, tmp._month))
// {
// tmp._day -= GetMonthDay(tmp._year, tmp._month);
// ++tmp._month;
// if (tmp._month == 13)
// {
// ++tmp._year;
// tmp._month = 1;
// }
// }
// return tmp; //不能用引用返回,因为他是局部对象,出了作用域就不在了
//}
//
//
//Date& Date::operator+=(int day)
//{
// *this = *this + day;
// return *this;
//}Date Date::operator-(int day) const
{
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;
}//++d ->d.operator++() 编译器自动转换
Date& Date::operator++()
{
*this += 1;
return *this;
}//d++ ->d.operator(0) C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递
Date Date::operator++(int) //规定只能是int,形参名字可以不写
{
Date tmp = *this;
*this += 1;
return tmp;
}int Date::operator-(const Date& d) const
{
int flag = 1;
Date max = *this;
Date min = d;if (*this < d) { int flag = -1; max = d; min = *this; } int n = 0; while (min != max) { ++min; ++n; } return n * flag;
}
bool Date::CheckInvalid() const
{
if (_year <= 0
|| _month < 1 || _month>12
|| _day<1 || _day>GetMonthDay(_year, _month))
{
return false;
}
else
{
return true;
}
}ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}istream& operator>>(istream& in, Date& d)
{
while (1)
{
cout << "请输入年月日:" << endl;
in >> d._year >> d._month >> d._day;if (!d.CheckInvalid()) { cout << "输入了无效日期,请重新输入" << endl; } else { break; } } return in;
}
上方实现时,有日期+天数和日期+=天数。二者实现其中一个即可复用另一个。但是要先实现哪一个更好呢?