




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第C++類和對象實(shí)戰(zhàn)之Date類的實(shí)現(xiàn)方法目錄零、前言一、Date類相關(guān)接口二、具體接口函數(shù)實(shí)現(xiàn)1、獲取月份天數(shù)2、Date打印3、Date構(gòu)造函數(shù)4、Date析構(gòu)函數(shù)5、Date拷貝構(gòu)造函數(shù)6、Date賦值重載函數(shù)7、Date+=天數(shù)8、Date+天數(shù)9、Date-=天數(shù)10、Date-天數(shù)11、++Date12、Date++13、–Date14、Date–15、日期比較16、Date相減17、日期輸入\日期輸出總結(jié)
零、前言
在學(xué)了C++類和對象基本知識以及六個(gè)默認(rèn)成員函數(shù)后,我們可以上手實(shí)現(xiàn)一個(gè)Date類出來,檢驗(yàn)學(xué)習(xí)的效果。
一、Date類相關(guān)接口
接口展示:
classDate
//輸出操作符重載
friendostreamoperator(ostream_cout,constDated);
//輸出操作符重載
friendistreamoperator(istream_cin,Dated);
public:
//獲取某年某月的天數(shù)
intGetMonthDay(intyear,intmonth);
//全缺省的構(gòu)造函數(shù)
Date(intyear=1988,intmonth=1,intday=1);
//拷貝構(gòu)造函數(shù)
Date(constDated);
//賦值運(yùn)算符重載
Dateoperator=(constDated);
//日期+=天數(shù)
Dateoperator+=(intday);
//日期+天數(shù)
Dateoperator+(intday);
//日期-天數(shù)
Dateoperator-(intday);
//日期-=天數(shù)
Dateoperator-=(intday);
//前置++
Dateoperator++();
//后置++
Dateoperator++(int);
//后置--
Dateoperator--(int);
//前置--
Dateoperator--();
//運(yùn)算符重載
booloperator(constDated);
//==運(yùn)算符重載
booloperator==(constDated);
//=運(yùn)算符重載
booloperator=(constDated);
//運(yùn)算符重載
booloperator(constDated);
//=運(yùn)算符重載
booloperator=(constDated);
//!=運(yùn)算符重載
booloperator!=(constDated);
//日期-日期返回兩個(gè)日期之間相隔的具體天數(shù)
intoperator-(constDated);
//日期展示
voidprint()
cout_year""_month""_dayendl;
private:
int_year;
int_month;
int_day;
二、具體接口函數(shù)實(shí)現(xiàn)
注意:
因?yàn)閷τ诙x在類里面的函數(shù)會自動設(shè)成內(nèi)聯(lián)函數(shù),而只有一些簡單的函數(shù)才建議設(shè)成內(nèi)聯(lián)函數(shù),所以實(shí)現(xiàn)函數(shù)時(shí)我們是聲明和定義分離(在類里面聲明,類外定義)
在類外實(shí)現(xiàn)函數(shù)接口需要加上類域名稱
1、獲取月份天數(shù)
注意:
閏年二月與平年二月的天數(shù)不同
實(shí)現(xiàn)代碼:
//獲取月份天數(shù)
intDate::GetMonthDay(intyear,intmonth)
//設(shè)置平年月天數(shù)數(shù)組
staticintmonthdays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//設(shè)置成靜態(tài)避免重復(fù)創(chuàng)建
intday=monthdays[month];
//對閏年二月的處理
if(month==2((year%4==0year%100!=0)||year%400==0))
day=29;
returnday;
2、Date打印
注:打印函數(shù)比較簡單,設(shè)成內(nèi)聯(lián)函數(shù)很適合,可以直接在類里定義
實(shí)現(xiàn)代碼:
voidDate::Print()
cout_year"年"_month"月"_day"日"endl;
3、Date構(gòu)造函數(shù)
注意:
對于構(gòu)造函數(shù)建議寫成全缺省函數(shù)(便于無參數(shù)初始化),但是只能定義和聲明其中一個(gè)寫缺省
考慮初始化的日期是否合理
實(shí)現(xiàn)代碼:
//構(gòu)造函數(shù)
//類里聲明
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
//嚴(yán)格來說拋異常更好
cout"非法日期"endl;
coutyear"年"month"月"day"日"endl;
exit(-1);
4、Date析構(gòu)函數(shù)
注:對于像Date一樣的類來說,析構(gòu)函數(shù)(沒有需要清理的空間資源),拷貝函數(shù)和賦值重載函數(shù)(能夠完成成員變量淺拷貝)都不用自己寫,編譯器默認(rèn)生成的已經(jīng)足夠使用
實(shí)現(xiàn)代碼:
//析構(gòu)函數(shù)
Date::~Date()
_year=1;
_month=0;
_day=0;
5、Date拷貝構(gòu)造函數(shù)
實(shí)現(xiàn)代碼:
//拷貝構(gòu)造
Date::Date(constDated)
_year=d._year;
_month=d._month;
_day=d._day;
6、Date賦值重載函數(shù)
注意:
對于賦值操作符來說,是需要能支持連續(xù)賦值的操作,這里我們返回Date本身來進(jìn)行接下來的繼續(xù)賦值
實(shí)現(xiàn)代碼:
//賦值運(yùn)算符重載
DateDate::operator=(constDated)
_year=d._year;
_month=d._month;
_day=d._day;
return*this;
效果圖:
7、Date+=天數(shù)
注意:
+=表示會修改Date本身的數(shù)據(jù)
處理傳入負(fù)數(shù)天數(shù)
處理好天數(shù)進(jìn)位,月份進(jìn)位
實(shí)現(xiàn)代碼:
//日期+=天數(shù)
DateDate::operator+=(intday)
if(day0)//處理特殊情況
*this-=-day;//復(fù)用Date-=天數(shù)
else
_day+=day;
while(_dayGetMonthDay(_year,_month))//處理數(shù)據(jù)合理性
_day-=GetMonthDay(_year,_month);
_month++;
if(_month12)
_year++;
_month=1;
return*this;//返回引用,即對象本身
8、Date+天數(shù)
注意:
+天數(shù)表示不會修改Date本身的數(shù)據(jù)(使用const修飾,避免修改)
邏輯與Date+=天數(shù)基本一致,可以進(jìn)行復(fù)用
實(shí)現(xiàn)代碼:
DateDate::operator+(intday)const
Datetmp=*this;//賦值重載
tmp+=day;//復(fù)用+=重載
returntmp;//返回值(拷貝構(gòu)造)
9、Date-=天數(shù)
注意:
+=表示會修改Date本身的數(shù)據(jù)
處理傳入負(fù)數(shù)天數(shù)
考慮日期的借位,月份的借位
實(shí)現(xiàn)代碼:
//日期-=天數(shù)
DateDate::operator-=(intday)
if(day0)
*this+=-day;//復(fù)用Date+=天數(shù)
else
_day-=day;
while(_day=0)//處理數(shù)據(jù)合理性
_month--;
if(_month=0)
_year--;
_month=12;
_day+=GetMonthDay(_year,_month);
return*this;
10、Date-天數(shù)
注意:
-天數(shù)不會修改Date本身的數(shù)據(jù)(使用const修飾,避免修改)
邏輯與Date-=天數(shù)基本一致,可以進(jìn)行復(fù)用
實(shí)現(xiàn)代碼:
DateDate::operator-(intday)const
Datetmp=*this;
tmp-=day;
returntmp;
11、++Date
注意:
前置++表示,Date先增后使用
實(shí)現(xiàn)代碼:
//++Date
DateDate::operator++()
*this+=1;//復(fù)用Date+=天數(shù)
return*this;
12、Date++
注意:
語法規(guī)定,因?yàn)榕c前置命名相同的緣故,這里的后置函數(shù)多一個(gè)參數(shù)來與前置函數(shù)形成重載
后置++表示先使用后自增
實(shí)現(xiàn)代碼:
//Date++
DateDate::operator++(int)
Datetmp=*this;//保存一份日期
*this+=1;//自增當(dāng)前日期
returntmp;//返回自增前的日期
13、–Date
實(shí)現(xiàn)代碼:
//--Date
DateDate::operator--()
*this-=1;
return*this;
14、Date–
實(shí)現(xiàn)代碼:
//Date--
DateDate::operator--(int)
Datetmp=*this;
*this-=1;
returntmp;
15、日期比較
注:可以多次復(fù)用
實(shí)現(xiàn)代碼:
//日期比較
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相減
實(shí)現(xiàn)代碼:
//日期減日期
intDate::operator-(constDated)const
//確定日期的大小
Datemax=*this;
Datemin=d;
if(*thisd)//復(fù)用日期比較
max=d;
min=*this;
intday=0;
while(max!=min)
++min;
++day;
returnday;
17、日期輸入\日期輸出
注意:
對于輸入操作符,我們習(xí)慣是cindate,而這樣的用法表示做操作數(shù)是cin,右操作數(shù)為日期對象,但是對于類成員函數(shù)來說,存在著隱含參數(shù)this指針(占據(jù)和第一個(gè)參數(shù)位置,即日期對象是左操作數(shù))
雖然定義成類外函數(shù)能修改參數(shù)位置,但是無法訪問類里的私有成員變量,這里我們使用友元函數(shù)來解決,即在類里聲明函數(shù)前加上friend,便可以訪問成員
實(shí)現(xiàn)代碼:
//輸
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國配方肥料項(xiàng)目投資計(jì)劃書
- 犬貓的傳染病(結(jié)核病)-寵物醫(yī)生
- 市場租場地合同協(xié)議書
- 2025年中國耐候性涂料行業(yè)市場調(diào)查及投資戰(zhàn)略預(yù)測報(bào)告
- 2025年中國桑蠶專用消毒劑項(xiàng)目投資計(jì)劃書
- 合伙開店活動策劃方案模板
- 三方協(xié)議合同解約協(xié)議書
- 規(guī)?;i場后備母豬的營養(yǎng)與飼養(yǎng)
- 一起豬鏈球菌病的診斷與治療
- 鋼管租賃合同終止協(xié)議書
- 硅酸鈣板、含鋯型硅酸鋁纖維棉、高鋁型硅酸鋁纖維棉技術(shù)規(guī)格
- 小學(xué)二年級下冊道德與法治《小水滴的訴說》教學(xué)教案
- GB∕T 15762-2020 蒸壓加氣混凝土板
- 護(hù)士分層級培訓(xùn)與管理課件
- 廣州版五年級英語下冊期末知識點(diǎn)復(fù)習(xí)ppt課件
- 照明電氣安裝工程施工方案及工藝方法要求
- 計(jì)算方法全書課件完整版ppt整本書電子教案最全教學(xué)教程ppt課件
- 公路工程施工安全技術(shù)規(guī)范-JTG-F90-2015
- 單代號網(wǎng)絡(luò)圖
- Q∕GDW 11958-2020 國家電網(wǎng)有限公司應(yīng)急預(yù)案編制規(guī)范
- 城垃圾填埋場垃圾滲濾液處理成本核算
評論
0/150
提交評論