目录
利用赋值运算符的重载,我们可以实现日期类的运算。本博文先分块实现每一个功能,最后汇总给出代码。
一、一些基础函数的实现
这一部分的编写能极大地简化我们下面的函数代码。接下来的其他代码都放在我们下面的public的部分。
class Date
{
public:
// 友元函数 -- 这个函数内部可以使用Date对象访问私有保护成员
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
//析构函数的实现
~Date(){
cout<<"date已被删除"<<endl;
};
//用于判断是不是闰年
bool IsLeapYears(int year)
{
if(year%400==0)
return true;
if(((year%100)!=0)&&((year%4)==0))
return true;
return false;
}
//用于判断当前年的天数
int countYear(int year)
{
if(IsLeapYears(year))
{
return 366;
}
else
{
return 365;
}
}
//用于获得某一年的某一个月的天数
int GetMonthDay(int year,int month)
{
static int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(month==2&&IsLeapYears(year))
{
return 29;
}
else
{
return days[month];
}
return days[month];
}
bool CheckDate()
{
if (_year >= 1
&& _month > 0 && _month < 13
&& _day > 0 && _day <= GetMonthDay(_year, _month))
{
return true;
}
else
{
return false;
}
}
// 构造日期类
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
if (!CheckDate())
{
Print();
cout << "刚构造的日期非法" << endl;
}
assert(CheckDate());
}
//用于打印我们的对象的日期,方便我们下面代码的验证
void Print()
{
cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
private:
int _year;
int _month;
int _day;
};
void Test () {
Date d1(2018, 9, 26);
d1.Print();
Date d3=(d1-246);
d3.Print();
cout<<d1-d3<<endl;
}
// 流插入重载
inline ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
// 流提取重载
inline istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
assert(d.CheckDate());
return in;
}
int main()
{
Test();
}
二、加减天数、赋值方法重载、拷贝构造的实现
这里我们需要实现一个日期类加上或减去一定天数之后的结果。注意,我们这里的是加减,而不是+=,或者-=,所以我们需要创建一个临时的对象ret来作为我们返回的结果,而我们自身this的日期值是不变的。
赋值运算符重载格式
参数类型:const T&,传递引用可以提高传参效率
返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
检测是否自己给自己赋值
返回*this :要复合连续赋值的含义
赋值运算符只能重载成类的成员函数不能重载成全局函数
赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数。
用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。
//运算符+天数的实现
Date operator+(const int day)
{
Date ret(*this);
ret._day+=day;
while(ret._day> GetMonthDay(ret._year,ret._month))
{
ret._day-=GetMonthDay(ret._year,ret._month);
++ret._month;
if(ret._month==13)
{
ret._month=1;
ret._year++;
}
}
return ret;
/*方法2复用+=
//+=的代码在“三”部分
//这里是拷贝构造
//赋值是两个已经存在的对象进行赋值
//由于+的时候不能改变自身,所以要用一个拷贝来记录加之后的值并且返回。
Date ret=*this;
ret+=day;
return ret;
*/
}
//运算符-天数的实现(注意)
Date operator -(int day)
{
Date ret(*this);
ret._day-=day;
while(ret._day<=0)
{
--ret._month;
if(ret._month==0)
{
ret._month=12;
ret._year--;
}
ret._day+=GetMonthDay(ret._year,ret._month);
}
return ret;
/*方法2复用-=
//-=的代码在“三”部分
//由于-的时候不能改变自身,所以要用一个拷贝来记录加之后的值并且返回。
Date ret=*this;
ret-=day;
return ret;
*/
}
//拷贝构造d1(d2)
Date(const Date& d)
{
_year=d._year;
_month=d._month;
_day=d._day;
}
//赋值方法的重载
//a=b=c
//置于这里我们为什么要有返回值Date类型,是因为要支持连续赋值的情况,
Date &operator=(const Date&d)
{
//如果是自己跟自己拷贝就别拷贝了。
if(this !=&d)
{
_year=d._year;
_month=d._month;
_day=d._day;
}
return(*this);
}
以下为针对这一部分的测试代码
oid Test () {
Date d1(2018, 9, 26);
d1.Print();
Date d2=(d1+300);
d2.Print();
Date d3=(d1-246);
d3.Print();
}
int main()
{
Test();
}
三、实现+=与-=
由上面“二”中所指,+=与-=需要改变当前的对象的日期
Date& operator+=(int day)
{
this->_day+=day;
while(this->_day> GetMonthDay(this->_year,this->_month))
{
this->_day-=GetMonthDay(this->_year,this->_month);
++this->_month;
if(this->_month==13)
{
this->_month=1;
this->_year++;
}
}
return *this;
/*
方法二:+=复用+的方法
注意+或者+=至少要实现一个,才能复用
这个写法的缺陷在于+=复用+,而+中有两次拷贝,所以降低了效率。为了减少拷贝,尽量不使用+=复用+的方法。
*this=*this+day;
return *this;
*/
}
//运算符-=的实现,
Date& operator -=(int day)
{
this->_day-=day;
while(this->_day<=0)
{
--this->_month;
if(this->_month==0)
{
this->_month=12;
this->_year--;
}
this->_day+=GetMonthDay(this->_year,this->_month);
}
return *this;
}
以下针对这一部分的测试代码
void Test () {
Date d1(2018, 9, 26);
d1.Print();
d1-=200;
d1.Print();
d1+=200;
d1.Print();
}
int main()
{
Test();
}
四、前后置++或--
这里我们特别要注意前后置++或者--要返回的是加减之前还是之后的结果。
如何区分前置++和后置++
直接按特性重载,无法区分
特殊处理,使用重载区分,后置++的重载增加一个int参数,跟前置构成函数重载进行区分
++d1; //d1.operator++();//前置
d1++;//d1.operator++(int);//后置
//前置++
Date& operator++()
{
return *this += 1;
}
//后置++
Date operator ++(int)
{
Date ret(*this);
*this+=1;
return ret;
}
//后置--
Date operator --(int)
{
Date ret(*this);
*this-=1;
return ret;
}
//前置--
Date& operator--()
{
return *this -= 1;
}
以下为针对这一部分的测试代码
void Test () {
Date d1(2018, 9, 26);
d1.Print();
Date d2=d1--;
d1.Print();
d2.Print();
Date d3=--d1;
d1.Print();
d3.Print();
d3=d1++;
d3.Print();
d1.Print();
d3=--d1;
d3.Print();
d1.Print();
}
int main()
{
Test();
}
五、比较运算符
比较运算符的话我们要注意我们最后的测试都是以d1(d2)来测试的,就是说只要传一个参数就可以了,自身的参数可以通过this指针来得到。并且最好加上const来方式d2被改变。
尽量不要拷贝,而是去复用。
//任何一个类只需要写一个> ==重载,或者< ==的重载,剩下的比较运算符复用即可。
//>运算符重载
bool operator>(const Date& d)
{
if(this->_year>d._year)
{
return true;
}
else if(this->_year==d._year&&this->_month>d._month)
{
return true;
}
else if(this->_year==d._year&&this->_month==d._month&&this->_day>d._day)
{
return true;
}
else
{
return false;
}
}
//==运算符重载
bool operator==(const Date& d2)
{
return _year == d2._year
&& _month == d2._month
&& _day == d2._day;
}
//>=运算符重载
bool operator>=(const Date& d)
{
if(this->_year>=d._year)
{
if(this->_month>=d._month)
{
if(this->_day>=d._day)
{
return true;
}
}
}
return false;
}
//<运算符重载
bool operator<(const Date& d)
{
return !(*this>=d);
// if(this->_year<d._year)
// {
// return true;
// }
// else if(this->_year==d._year&&this->_month<d._month)
// {
// return true;
// }
// else if(this->_year==d._year&&this->_month==d._month&&this->_day<d._day)
// {
// return true;
// }
// else
// {
// return false;
// }
}
//>=运算符重载
bool operator<=(const Date& d)
{
return (*this>d)||(*this==d);
// if(this->_year<=d._year)
// {
// if(this->_month<=d._month)
// {
// if(this->_day<=d._day)
// {
// return true;
// }
// }
// }
// return false;
}
// !=运算符重载
bool operator != (const Date& d)
{
return !(*this==d);
// if(this->_year!=d._year)
//{
//return true;
//}
//if(this->_month!=d._month)
//{
//return true;
//}
//if(this->_day!=d._day)
//{
//return true;
//}
//return false;
}
这里我们在测试的时候因为<<的优先级比较高,所以我们需要将我们比较的部分用括号括在一起,否则会报错。
void Test () {
Date d1(2018, 9, 26);
Date d2(2022,10,31);
cout<<(d1>d2)<<endl;
cout<<(d1>=d2)<<endl;
cout<<(d1<d2)<<endl;
cout<<(d1<=d2)<<endl;
cout<<(d1==d2)<<endl;
cout<<(d1!=d2)<<endl;
}
int main()
{
Test();
}
六、日期之间的相减得到间隔天数
// 日期-日期 返回天数
int operator-(const Date& d)
{
int count1=0,count2=0,count3=0,sum=0;
if(*this<d)
{
return -1;
}
count1+=this->_day;
for(int i=1;i<=this->_month-1;i++)
{
count1+= GetMonthDay(this->_year,i);
}
count2+=d._day;
for(int i=1;i<=d._month-1;i++)
{
count2+= GetMonthDay(d._year,i);
}
int gap=this->_year-d._year;
for(int i=0;i<gap;i++)
{
count3+= countYear(i+d._year);
}
sum=count1+count3-count2+1;
return sum;
}
以下为针对这一部分的测试代码
void Test () {
Date d1(2018, 9, 26);
Date d2(2022,10,31);
cout<<(d2-d1)<<endl;
}
int main()
{
Test();
}
七、代码的汇总
测试代码就用上面的测试代码,就不汇总了哈,所以只需要修改test函数中的具体的调用就可以实现测试。
#include<iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
// 友元函数 -- 这个函数内部可以使用Date对象访问私有保护成员
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
~Date(){
cout<<"date已被删除"<<endl;
};
bool IsLeapYears(int year)
{
if(year%400==0)
return true;
if(((year%100)!=0)&&((year%4)==0))
return true;
return false;
}
int countYear(int year)
{
if(IsLeapYears(year))
{
return 366;
}
else
{
return 365;
}
}
int GetMonthDay(int year,int month)
{
static int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(month==2&&IsLeapYears(year))
{
return 29;
}
else
{
return days[month];
}
return days[month];
}
bool CheckDate()
{
if (_year >= 1
&& _month > 0 && _month < 13
&& _day > 0 && _day <= GetMonthDay(_year, _month))
{
return true;
}
else
{
return false;
}
}
// 构造date类
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
if (!CheckDate())
{
Print();
cout << "刚构造的日期非法" << endl;
}
assert(CheckDate());
}
void Print()
{
cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
Date operator+(const int day)
{
Date ret(*this);
ret._day+=day;
while(ret._day> GetMonthDay(ret._year,ret._month))
{
ret._day-=GetMonthDay(ret._year,ret._month);
++ret._month;
if(ret._month==13)
{
ret._month=1;
ret._year++;
}
}
return ret;
}
Date operator -(int day)
{
Date ret(*this);
ret._day-=day;
while(ret._day<=0)
{
--ret._month;
if(ret._month==0)
{
ret._month=12;
ret._year--;
}
ret._day+=GetMonthDay(ret._year,ret._month);
}
return ret;
}
Date& operator -=(int day)
{
this->_day-=day;
while(this->_day<=0)
{
--this->_month;
if(this->_month==0)
{
this->_month=12;
this->_year--;
}
this->_day+=GetMonthDay(this->_year,this->_month);
}
return *this;
}
//前置++
Date& operator++()
{
return *this += 1;
}
//后置++
Date operator ++(int)
{
Date ret(*this);
*this+=1;
return ret;
}
//后置--
Date operator --(int)
{
Date ret(*this);
*this-=1;
return ret;
}
//前置--
Date& operator--()
{
return *this -= 1;
}
Date(const Date& d)
{
_year=d._year;
_month=d._month;
_day=d._day;
}
//赋值方法的重载
Date &operator=(const Date&d)
{
if(this !=&d)
{
_year=d._year;
_month=d._month;
_day=d._day;
}
return(*this);
}
Date& operator+=(int day)
{
this->_day+=day;
while(this->_day> GetMonthDay(this->_year,this->_month))
{
this->_day-=GetMonthDay(this->_year,this->_month);
++this->_month;
if(this->_month==13)
{
this->_month=1;
this->_year++;
}
}
return *this;
}
//>运算符重载
bool operator>(const Date& d)
{
if(this->_year>d._year)
{
return true;
}
else if(this->_year==d._year&&this->_month>d._month)
{
return true;
}
else if(this->_year==d._year&&this->_month==d._month&&this->_day>d._day)
{
return true;
}
else
{
return false;
}
}
//==运算符重载
bool operator==(const Date& d2)
{
return _year == d2._year
&& _month == d2._month
&& _day == d2._day;
}
//>=运算符重载
bool operator>=(const Date& d)
{
if(this->_year>=d._year)
{
if(this->_month>=d._month)
{
if(this->_day>=d._day)
{
return true;
}
}
}
return false;
}
//<运算符重载
bool operator<(const Date& d)
{
return !(*this>=d);
}
//>=运算符重载
bool operator<=(const Date& d)
{
return (*this>d)||(*this==d);
}
// !=运算符重载
bool operator != (const Date& d)
{
return !(*this==d);
}
// 日期-日期 返回天数
int operator-(const Date& d)
{
int count1=0,count2=0,count3=0,sum=0;
if(*this<d)
{
return -1;
}
count1+=this->_day;
for(int i=1;i<=this->_month-1;i++)
{
count1+= GetMonthDay(this->_year,i);
}
count2+=d._day;
for(int i=1;i<=d._month-1;i++)
{
count2+= GetMonthDay(d._year,i);
}
int gap=this->_year-d._year;
for(int i=0;i<gap;i++)
{
count3+= countYear(i+d._year);
}
sum=count1+count3-count2+1;
return sum;
}
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& in, Date& d)
{
in >> d._year >> d._month >> d._day;
assert(d.CheckDate());
return in;
}
void Test () {
Date d1(2018, 9, 26);
Date d2(2022,10,31);
cout<<d1<<d2<<endl;
}
int main()
{
Test();
}