版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
第C++類和對象實戰(zhàn)之Date類的實現(xiàn)方法目錄零、前言一、Date類相關接口二、具體接口函數(shù)實現(xiàn)1、獲取月份天數(shù)2、Date打印3、Date構造函數(shù)4、Date析構函數(shù)5、Date拷貝構造函數(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、日期輸入\日期輸出總結
零、前言
在學了C++類和對象基本知識以及六個默認成員函數(shù)后,我們可以上手實現(xiàn)一個Date類出來,檢驗學習的效果。
一、Date類相關接口
接口展示:
classDate
//輸出操作符重載
friendostreamoperator(ostream_cout,constDated);
//輸出操作符重載
friendistreamoperator(istream_cin,Dated);
public:
//獲取某年某月的天數(shù)
intGetMonthDay(intyear,intmonth);
//全缺省的構造函數(shù)
Date(intyear=1988,intmonth=1,intday=1);
//拷貝構造函數(shù)
Date(constDated);
//賦值運算符重載
Dateoperator=(constDated);
//日期+=天數(shù)
Dateoperator+=(intday);
//日期+天數(shù)
Dateoperator+(intday);
//日期-天數(shù)
Dateoperator-(intday);
//日期-=天數(shù)
Dateoperator-=(intday);
//前置++
Dateoperator++();
//后置++
Dateoperator++(int);
//后置--
Dateoperator--(int);
//前置--
Dateoperator--();
//運算符重載
booloperator(constDated);
//==運算符重載
booloperator==(constDated);
//=運算符重載
booloperator=(constDated);
//運算符重載
booloperator(constDated);
//=運算符重載
booloperator=(constDated);
//!=運算符重載
booloperator!=(constDated);
//日期-日期返回兩個日期之間相隔的具體天數(shù)
intoperator-(constDated);
//日期展示
voidprint()
cout_year""_month""_dayendl;
private:
int_year;
int_month;
int_day;
二、具體接口函數(shù)實現(xiàn)
注意:
因為對于定義在類里面的函數(shù)會自動設成內(nèi)聯(lián)函數(shù),而只有一些簡單的函數(shù)才建議設成內(nèi)聯(lián)函數(shù),所以實現(xiàn)函數(shù)時我們是聲明和定義分離(在類里面聲明,類外定義)
在類外實現(xiàn)函數(shù)接口需要加上類域名稱
1、獲取月份天數(shù)
注意:
閏年二月與平年二月的天數(shù)不同
實現(xiàn)代碼:
//獲取月份天數(shù)
intDate::GetMonthDay(intyear,intmonth)
//設置平年月天數(shù)數(shù)組
staticintmonthdays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//設置成靜態(tài)避免重復創(chuàng)建
intday=monthdays[month];
//對閏年二月的處理
if(month==2((year%4==0year%100!=0)||year%400==0))
day=29;
returnday;
2、Date打印
注:打印函數(shù)比較簡單,設成內(nèi)聯(lián)函數(shù)很適合,可以直接在類里定義
實現(xiàn)代碼:
voidDate::Print()
cout_year"年"_month"月"_day"日"endl;
3、Date構造函數(shù)
注意:
對于構造函數(shù)建議寫成全缺省函數(shù)(便于無參數(shù)初始化),但是只能定義和聲明其中一個寫缺省
考慮初始化的日期是否合理
實現(xiàn)代碼:
//構造函數(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
//嚴格來說拋異常更好
cout"非法日期"endl;
coutyear"年"month"月"day"日"endl;
exit(-1);
4、Date析構函數(shù)
注:對于像Date一樣的類來說,析構函數(shù)(沒有需要清理的空間資源),拷貝函數(shù)和賦值重載函數(shù)(能夠完成成員變量淺拷貝)都不用自己寫,編譯器默認生成的已經(jīng)足夠使用
實現(xiàn)代碼:
//析構函數(shù)
Date::~Date()
_year=1;
_month=0;
_day=0;
5、Date拷貝構造函數(shù)
實現(xiàn)代碼:
//拷貝構造
Date::Date(constDated)
_year=d._year;
_month=d._month;
_day=d._day;
6、Date賦值重載函數(shù)
注意:
對于賦值操作符來說,是需要能支持連續(xù)賦值的操作,這里我們返回Date本身來進行接下來的繼續(xù)賦值
實現(xiàn)代碼:
//賦值運算符重載
DateDate::operator=(constDated)
_year=d._year;
_month=d._month;
_day=d._day;
return*this;
效果圖:
7、Date+=天數(shù)
注意:
+=表示會修改Date本身的數(shù)據(jù)
處理傳入負數(shù)天數(shù)
處理好天數(shù)進位,月份進位
實現(xiàn)代碼:
//日期+=天數(shù)
DateDate::operator+=(intday)
if(day0)//處理特殊情況
*this-=-day;//復用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ù)基本一致,可以進行復用
實現(xiàn)代碼:
DateDate::operator+(intday)const
Datetmp=*this;//賦值重載
tmp+=day;//復用+=重載
returntmp;//返回值(拷貝構造)
9、Date-=天數(shù)
注意:
+=表示會修改Date本身的數(shù)據(jù)
處理傳入負數(shù)天數(shù)
考慮日期的借位,月份的借位
實現(xiàn)代碼:
//日期-=天數(shù)
DateDate::operator-=(intday)
if(day0)
*this+=-day;//復用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ù)基本一致,可以進行復用
實現(xiàn)代碼:
DateDate::operator-(intday)const
Datetmp=*this;
tmp-=day;
returntmp;
11、++Date
注意:
前置++表示,Date先增后使用
實現(xiàn)代碼:
//++Date
DateDate::operator++()
*this+=1;//復用Date+=天數(shù)
return*this;
12、Date++
注意:
語法規(guī)定,因為與前置命名相同的緣故,這里的后置函數(shù)多一個參數(shù)來與前置函數(shù)形成重載
后置++表示先使用后自增
實現(xiàn)代碼:
//Date++
DateDate::operator++(int)
Datetmp=*this;//保存一份日期
*this+=1;//自增當前日期
returntmp;//返回自增前的日期
13、–Date
實現(xiàn)代碼:
//--Date
DateDate::operator--()
*this-=1;
return*this;
14、Date–
實現(xiàn)代碼:
//Date--
DateDate::operator--(int)
Datetmp=*this;
*this-=1;
returntmp;
15、日期比較
注:可以多次復用
實現(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相減
實現(xiàn)代碼:
//日期減日期
intDate::operator-(constDated)const
//確定日期的大小
Datemax=*this;
Datemin=d;
if(*thisd)//復用日期比較
max=d;
min=*this;
intday=0;
while(max!=min)
++min;
++day;
returnday;
17、日期輸入\日期輸出
注意:
對于輸入操作符,我們習慣是cindate,而這樣的用法表示做操作數(shù)是cin,右操作數(shù)為日期對象,但是對于類成員函數(shù)來說,存在著隱含參數(shù)this指針(占據(jù)和第一個參數(shù)位置,即日期對象是左操作數(shù))
雖然定義成類外函數(shù)能修改參數(shù)位置,但是無法訪問類里的私有成員變量,這里我們使用友元函數(shù)來解決,即在類里聲明函數(shù)前加上friend,便可以訪問成員
實現(xiàn)代碼:
//輸
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年醫(yī)療機構信息化建設與網(wǎng)絡安全手冊
- 物流運輸過程監(jiān)控與優(yōu)化指南(標準版)
- 2025年人力資源管理績效考核與激勵手冊
- 消防安全檢查與處置手冊
- 國防培訓班管理制度
- 鄉(xiāng)村國語培訓制度
- 2026年IT企業(yè)技術面試題目
- 鋼化廠安全教育培訓制度
- 幼兒園教職員工培訓制度
- 后愛培訓制度
- 車輛日常安全檢查課件
- 成立合資公司合同范本
- 比亞迪索賠培訓課件
- 民航安全法律法規(guī)課件
- 2026屆四川省瀘州高級中學高一生物第一學期期末經(jīng)典試題含解析
- 山東省濟寧市2026屆第一學期高三質(zhì)量檢測期末考試濟寧一模英語(含答案)
- 2026標準版離婚協(xié)議書-無子女無共同財產(chǎn)債務版
- 光伏電站巡檢培訓課件
- 【期末必刷選擇題100題】(新教材)統(tǒng)編版八年級道德與法治上學期專項練習選擇題100題(含答案與解析)
- 年末節(jié)前安全教育培訓
- GB/T 93-2025緊固件彈簧墊圈標準型
評論
0/150
提交評論