第五次編程運算符重載_第1頁
第五次編程運算符重載_第2頁
第五次編程運算符重載_第3頁
第五次編程運算符重載_第4頁
第五次編程運算符重載_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、第六次編程重載05.設計一個日期類 Date,包括年、月、日等私有數(shù)據(jù)成員。要求實現(xiàn)日期的基本運 算,如一日期加上天數(shù)、一日期減去天數(shù)、兩日期相差的天數(shù)。解:在 Date 類中設計如下重載運算符函數(shù):Date operator+(加天數(shù)得到的日期Date operator-(減去天數(shù)得到的日期days);返回一日期days);返回一日期Date operator+();一天數(shù)得到的日期返回一日期加上Dateoperator();返回一日期減去一天數(shù)得到的日期operator-(Date &b);相差的天數(shù)返回兩日期在實現(xiàn)這些重載運算符函數(shù)調用以下私有成員函數(shù):leap();判斷指定的年份是否為

2、閏年dton(Date &);將指定日期轉換為從 0年 0 月 0 日起的天數(shù)ntod();將指定的 0 年 0 月 0 日起的天數(shù)轉換為對應的日期本題程序如下:#includeday_tab212=31,28,31,30,31,30,31,31,30, 31,30,31,31,29,31,30,31,31,30,31,30,31;/day_tab二維數(shù)組存放各月天數(shù),第一行對應非閏年,第二行對應閏年class Dateyear,month,day;leap();dton(Date &);Date ntod( public:Date()Date();y,m,d)year=y;month=m;d

3、ay=d;void setday( void setmonth( void setyear(d)day=d;m)month=m;y)year=y;getday()return day;getmonth()return month; getyear()return year;Date operator+(days)sic Date date;number=dton(*this)+days;date=ntod(number);return date;Date operator-(days)sic Date date;number=dton(*this);number-=days;date=ntod

4、(number);return date;operator-(Date &b)days=dton(*this)-dton(b)-1;return days;void disp()coutyear.month.dayendl;Date:leap(year)if(year%4=0&year%100!=0|year%400=0)/ 是閏年return 1;else/不是閏年return 0;Date:dton(Date &d)y,m,days=0; for(y=1;y=d.year;y+)if(leap(y) days+=366;elsedays+=365; for(m=0;md.month-1;m

5、+)if(leap(d.year) days+=day_tab1m;elsedays+=day_tab0m; days+=d.day;return days;Date Date:ntod(n)y=1,m=1,d,rest=n,lp; while(1)if(leap(y)if(rest=366) break;elserest-=366;elseif(restday_tab1m-1) rest-=day_tab1m-1;elsebreak;elseif(restday_tab0m-1) rest-=day_tab0m-1;elsebreak; m+;d=rest;return Date(y,m,d);void main()Date now(2002,6,12),then(2003,2,10);coutnow:; now.disp();coutthen:; then.disp();cout相差天數(shù):(then-now)endl; Date d1=now+100,d2=now-100;coutnow+100:; d1.disp();coutnow-100:; d2.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論