C++类和对象实战之Date类的实现方法_第1页
C++类和对象实战之Date类的实现方法_第2页
C++类和对象实战之Date类的实现方法_第3页
C++类和对象实战之Date类的实现方法_第4页
C++类和对象实战之Date类的实现方法_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

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

文档简介

第C++类和对象实战之Date类的实现方法目录零、前言一、Date类相关接口二、具体接口函数实现1、获取月份天数2、Date打印3、Date构造函数4、Date析构函数5、Date拷贝构造函数6、Date赋值重载函数7、Date+=天数8、Date+天数9、Date-=天数10、Date-天数11、++Date12、Date++13、–Date14、Date–15、日期比较16、Date相减17、日期输入\日期输出总结

零、前言

在学了C++类和对象基本知识以及六个默认成员函数后,我们可以上手实现一个Date类出来,检验学习的效果。

一、Date类相关接口

接口展示:

classDate

//输出操作符重载

friendostreamoperator(ostream_cout,constDated);

//输出操作符重载

friendistreamoperator(istream_cin,Dated);

public:

//获取某年某月的天数

intGetMonthDay(intyear,intmonth);

//全缺省的构造函数

Date(intyear=1988,intmonth=1,intday=1);

//拷贝构造函数

Date(constDated);

//赋值运算符重载

Dateoperator=(constDated);

//日期+=天数

Dateoperator+=(intday);

//日期+天数

Dateoperator+(intday);

//日期-天数

Dateoperator-(intday);

//日期-=天数

Dateoperator-=(intday);

//前置++

Dateoperator++();

//后置++

Dateoperator++(int);

//后置--

Dateoperator--(int);

//前置--

Dateoperator--();

//运算符重载

booloperator(constDated);

//==运算符重载

booloperator==(constDated);

//=运算符重载

booloperator=(constDated);

//运算符重载

booloperator(constDated);

//=运算符重载

booloperator=(constDated);

//!=运算符重载

booloperator!=(constDated);

//日期-日期返回两个日期之间相隔的具体天数

intoperator-(constDated);

//日期展示

voidprint()

cout_year""_month""_dayendl;

private:

int_year;

int_month;

int_day;

二、具体接口函数实现

注意:

因为对于定义在类里面的函数会自动设成内联函数,而只有一些简单的函数才建议设成内联函数,所以实现函数时我们是声明和定义分离(在类里面声明,类外定义)

在类外实现函数接口需要加上类域名称

1、获取月份天数

注意:

闰年二月与平年二月的天数不同

实现代码:

//获取月份天数

intDate::GetMonthDay(intyear,intmonth)

//设置平年月天数数组

staticintmonthdays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//设置成静态避免重复创建

intday=monthdays[month];

//对闰年二月的处理

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

day=29;

returnday;

2、Date打印

注:打印函数比较简单,设成内联函数很适合,可以直接在类里定义

实现代码:

voidDate::Print()

cout_year"年"_month"月"_day"日"endl;

3、Date构造函数

注意:

对于构造函数建议写成全缺省函数(便于无参数初始化),但是只能定义和声明其中一个写缺省

考虑初始化的日期是否合理

实现代码:

//构造函数

//类里声明

Date(intyear=0,intmonth=1,intday=1);

Date::Date(intyear,intmonth,intday)

//检查日期的合法性

if(year=0

month0month13

day0day=GetMonthDay(year,month))

_year=year;

_month=month;

_day=day;

else

//严格来说抛异常更好

cout"非法日期"endl;

coutyear"年"month"月"day"日"endl;

exit(-1);

4、Date析构函数

注:对于像Date一样的类来说,析构函数(没有需要清理的空间资源),拷贝函数和赋值重载函数(能够完成成员变量浅拷贝)都不用自己写,编译器默认生成的已经足够使用

实现代码:

//析构函数

Date::~Date()

_year=1;

_month=0;

_day=0;

5、Date拷贝构造函数

实现代码:

//拷贝构造

Date::Date(constDated)

_year=d._year;

_month=d._month;

_day=d._day;

6、Date赋值重载函数

注意:

对于赋值操作符来说,是需要能支持连续赋值的操作,这里我们返回Date本身来进行接下来的继续赋值

实现代码:

//赋值运算符重载

DateDate::operator=(constDated)

_year=d._year;

_month=d._month;

_day=d._day;

return*this;

效果图:

7、Date+=天数

注意:

+=表示会修改Date本身的数据

处理传入负数天数

处理好天数进位,月份进位

实现代码:

//日期+=天数

DateDate::operator+=(intday)

if(day0)//处理特殊情况

*this-=-day;//复用Date-=天数

else

_day+=day;

while(_dayGetMonthDay(_year,_month))//处理数据合理性

_day-=GetMonthDay(_year,_month);

_month++;

if(_month12)

_year++;

_month=1;

return*this;//返回引用,即对象本身

8、Date+天数

注意:

+天数表示不会修改Date本身的数据(使用const修饰,避免修改)

逻辑与Date+=天数基本一致,可以进行复用

实现代码:

DateDate::operator+(intday)const

Datetmp=*this;//赋值重载

tmp+=day;//复用+=重载

returntmp;//返回值(拷贝构造)

9、Date-=天数

注意:

+=表示会修改Date本身的数据

处理传入负数天数

考虑日期的借位,月份的借位

实现代码:

//日期-=天数

DateDate::operator-=(intday)

if(day0)

*this+=-day;//复用Date+=天数

else

_day-=day;

while(_day=0)//处理数据合理性

_month--;

if(_month=0)

_year--;

_month=12;

_day+=GetMonthDay(_year,_month);

return*this;

10、Date-天数

注意:

-天数不会修改Date本身的数据(使用const修饰,避免修改)

逻辑与Date-=天数基本一致,可以进行复用

实现代码:

DateDate::operator-(intday)const

Datetmp=*this;

tmp-=day;

returntmp;

11、++Date

注意:

前置++表示,Date先增后使用

实现代码:

//++Date

DateDate::operator++()

*this+=1;//复用Date+=天数

return*this;

12、Date++

注意:

语法规定,因为与前置命名相同的缘故,这里的后置函数多一个参数来与前置函数形成重载

后置++表示先使用后自增

实现代码:

//Date++

DateDate::operator++(int)

Datetmp=*this;//保存一份日期

*this+=1;//自增当前日期

returntmp;//返回自增前的日期

13、–Date

实现代码:

//--Date

DateDate::operator--()

*this-=1;

return*this;

14、Date–

实现代码:

//Date--

DateDate::operator--(int)

Datetmp=*this;

*this-=1;

returntmp;

15、日期比较

注:可以多次复用

实现代码:

//日期比较

boolDate::operator(constDated)const

if(_yeard._year)

returntrue;

elseif(_year==d._year)

if(_monthd._month)

returntrue;

elseif(_month==d._month)

if(_dayd._day)

returntrue;

}returnfalse;

boolDate::operator==(constDated)const

return_year==d._year_month==d._month_day==d._day;

boolDate::operator(constDated)const

return!(*this=d);

boolDate::operator=(constDated)const

return*thisd||*this==d;

boolDate::operator=(constDated)const

return!(*this

boolDate::operator!=(constDated)const

return!(*this==d);

16、Date相减

实现代码:

//日期减日期

intDate::operator-(constDated)const

//确定日期的大小

Datemax=*this;

Datemin=d;

if(*thisd)//复用日期比较

max=d;

min=*this;

intday=0;

while(max!=min)

++min;

++day;

returnday;

17、日期输入\日期输出

注意:

对于输入操作符,我们习惯是cindate,而这样的用法表示做操作数是cin,右操作数为日期对象,但是对于类成员函数来说,存在着隐含参数this指针(占据和第一个参数位置,即日期对象是左操作数)

虽然定义成类外函数能修改参数位置,但是无法访问类里的私有成员变量,这里我们使用友元函数来解决,即在类里声明函数前加上friend,便可以访问成员

实现代码:

//输

温馨提示

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

评论

0/150

提交评论