C++无痛实现日期类的示例代码_第1页
C++无痛实现日期类的示例代码_第2页
C++无痛实现日期类的示例代码_第3页
C++无痛实现日期类的示例代码_第4页
C++无痛实现日期类的示例代码_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第C++无痛实现日期类的示例代码目录日期类的实现构造函数析构函数拷贝构造函数打印函数获取天数函数运算符重载区赋值重载整体代码Date.hDate.cpp

日期类的实现

凡是要写类必须要提到六大默认成员(六位大爷):构造函数、析构函数、拷贝构造函数、赋值重载函数、取地址重载函数(包括const对象和普通对象);那么这次的日期类又需要伺候哪几位大爷呢?

日期类的实现中函数与函数之间有较强的耦合性,所以实现的逻辑顺序一定要把握好,不然会晕头转向的!!!下面是我的实现顺序:

构造函数

Date(constDated)//拷贝构造函数

_year=d._year;

_month=d._month;

_day=d._day;

析构函数

~Date()//析构函数

_year=1;

_month=1;

_day=1;

拷贝构造函数

Date(constDated)//拷贝构造函数

_year=d._year;

_month=d._month;

_day=d._day;

打印函数

voidPrint()//打印函数

cout_year"-"_month"-"_dayendl;

这里我们还需要写一个获取月份对应天数的函数

获取天数函数

intGetTrueDay(intyear,intmonth)//得到正确月份天数

staticintmonthday[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

if(month==2((year%4==0year%100!=0)

||(year%400==0)))

return29;

else

returnmonthday[month];

这里就是大体框架了,接下来是各个细节部分

运算符重载区

判断两个日期是否相等(*this==d)

booloperator==(constDated)const;//等于

//---相等为真(返回1);不相同为假(返回0)

boolDate::operator==(constDated)const//等于

{//年相等才判断到月,月相等才判断到年

return_year==d._year

_month==d._month

_day==d._day;

判断前一个日期是否大于后一个日期(*thisd)

booloperator(constDated)const;//大于

//---相等为真(返回1);不相同为假(返回0)

boolDate::operator(constDated)const//大于

{//这里一样的判断顺序依次是年---月---日

if(_yeard._year)

returntrue;

elseif(_monthd._month)

returntrue;

elseif(_day==d._day)

returntrue;

else

returnfalse;

}

判断前一个日期是否大于等于后一个日期(*this=d)

这里直接重载!!!

booloperator=(constDated)const//大于等于

return*thisd||*this==d;

判断前一个日期是否小于后一个日期(*thisd)

这里直接重载!!!

booloperator(constDated)const//判断小于

return!(*this=d);

判断前一个日期是否小于等于后一个日期(*this=d)

这里直接重载!!!

booloperator=(constDated)const//小于等于

return!(*this

赋值重载

前一个日期等于后一个日期(*this=d)可以连续赋值

Dateoperator=(constDated)//赋值重载

if(this!=d)

_year=d._year;

_month=d._month;

_day=d._day;

else

return*this;

对日期减天数-不影响自身-用拷贝构造

Dateoperator-(intday)const;//减天数

DateDate::operator-(intday)const//减天数-不影响本身-不用引用-用拷贝构造函数

Datetmp(*this);

tmp-=day;

returntmp;

对日期加天数-不影响自身-用拷贝构造

Dateoperator+(intday)const;//加天数

DateDate::operator+(intday)const//加天数-不影响本身-不用引用-用拷贝构造函数

Datetmp(*this);

tmp+=day;

returntmp;

日期减等天数-影响自身-用引用

Dateoperator-=(intday);//减等天数

DateDate::operator-=(intday)//减等天数-影响本身-用引用-不加const

if(day0)

return*this+=abs(day);

_day-=day;

while(_day=0)

--_month;

if(_month==0)

--_year;

_month=12;

_day+=GetTrueDay(_year,_month);

return*this;

}

日期加等天数-影响自身-用引用

Dateoperator+=(intday);//加天数

DateDate::operator+=(intday)//加天数-影响本身-用引用-不加const

if(day0)

return*this-=abs(day);

_day+=day;

while(_dayGetTrueDay(_year,_month))

_day-=GetTrueDay(_year,_month);

_month++;

if(_month==13)

_year++;

_month=1;

return*this;

}

日期天数前置++

Dateoperator--(int);//后置--

DateDate::operator--(int)//后置--,不需要改变自身-用构造函数-括号里+int

Datetmp(*this);

*this-=1;

returntmp;

日期减日期(前一个日期减后一个日期-算差距天数)

intoperator-(constDated)const;//日期减日期-算差距天数

intDate::operator-(constDated)const//日期减日期-算差距天数-都不改变自身+const

Datemax=*this;

Datemin=d;

intflag=1;

if(*thisd)

max=d;

min=*this;

flag=-1;//如果*this比d小则减出来是负数,所以要预备flag=-1

intn=0;

while(minmax)//min++,max--,最后相等时,n++得出的就是差距天数

n++;

min++;

returnflag*n;//*this比d小,得出来是负数-乘-1,*this比d大,得正数-乘1

流插入函数

friendostreamoperator(ostreamout,Dated);//流插入友元声明

ostreamoperator(ostreamout,Dated)//流插入

coutd._year"年"d._month"月"d._day"日"endl;

returncout;

流提取函数

friendistreamoperator(istreamin,Dated);//流提取友元声明

istreamoperator(istreamin,Dated)//流提取

ind._year;

ind._month;

ind._day;

returnin;

好啦,以上就是日期类实现各个模块啦,下面是整体代码!

整体代码

Date.h

#pragmaonce

#includeiostream

usingnamespacestd;

classDate

public:

Date(constDated)//拷贝构造函数

_year=d._year;

_month=d._month;

_day=d._day;

~Date()//析构函数

_year=1;

_month=1;

_day=1;

voidPrint()//打印函数

cout_year"-"_month"-"_dayendl;

intGetTrueDay(intyear,intmonth)//得到正确月份天数

staticintmonthday[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

if(month==2((year%4==0year%100!=0)||(year%400==0)))

return29;

else

returnmonthday[month];

Date(intyear=1,intmonth=1,intday=1)//构造函数

:_year(year)

,_month(month)

,_day(day)

booloperator==(constDated)const;//等于

booloperator(constDated)const;//大于

booloperator=(constDated)const//大于等于

return*thisd||*this==d;

booloperator(constDated)const//判断小于

return!(*this=d);

booloperator=(constDated)const//小于等于

return!(*this

Dateoperator=(constDated)//赋值重载

if(this!=d)

_year=d._year;

_month=d._month;

_day=d._day;

else

return*this;

Dateoperator-(intday)const;//减天数

Dateoperator+(intday)const;//加天数

Dateoperator-=(intday);//减等天数

Dateoperator+=(intday);//加天数

Dateoperator++();//天数前置++

Dateoperator++(int);//后置++

Dateoperator--();//前置--

Dat

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论