剛剛開始學c++,一般入門都會了解到日期類,對於日期類,我們首先是要了解。不過,如果我們能夠更加的了解它,對於後面我們學習其他類的話,會有乙個很大的幫助。
現在在這兒,簡單實現一下日期類的一些介面:
#include using namespace std;
class date
;
#include "date.h"
void date::print()
/*date(int year = 1900, int month = 0, int day = 0)
*/int date::getmonthday(int year, int month)
; if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return montharray[month];
}date::date(int year, int month, int day)
else }
//~date();
date::date(const date& d)
//賦值拷貝構造的話,現有乙個物件,直接賦值就行,如果現構造的話,就是直接拷貝構造了,沒有賦值
date& date::operator=(const date& d)
//cout << "date& operator=(const date& d)" << endl;
return *this;
}//+=的話,加了之後,自身也變了,結果出來的也加一了
date& date::operator+=(int days)
} return ret;*/
if (days < 0)
_day += days;
while (_day > getmonthday(_year, _month)) }
return *this;
}//+的話,只是結果加了一,自身沒有變
date date::operator+(int days)
//-=的話,自身減一了,結果是減一之後的結果
date& date::operator-=(int days)
_day -= days;
while (_day <= 0)
_day += getmonthday(_year, _month);
} return *this;
}//-的話,結果是減一之後的,自身沒有變
date date::operator-(int days)
//日期減日期,減出來的是乙個整型,而且大的減小的話,減出來的是整數,小的減大的的話,減出來的的話是負數
int date::operator-(const date& d)
int n = 0;
while (min != max)
return n*flag;
}//++d -> d.operator++(&d) 前置加加,減之後,留下來的自身直接變了
date& date::operator++()
//d++ -> d.operator++(&d, 1) 後置加加,減之後,留下來的自身直接沒變
date date::operator++(int)
//d-- -> d.operator--(&d) 前置減減,減之後,留下來的自身直接變了
date& date::operator--()
//d-- -> d.operator--(&d, 1) 後置減減,減之後,留下來的自身直接沒變
date date::operator--(int)
bool date::operator==(const date& d)
bool date::operator<(const date& d)
bool date::operator<=(const date& d)
bool date::operator>(const date& d)
bool date::operator>=(const date& d)
bool date::operator!=(const date& d)
int main()
日期類的簡單實現
日期類的簡單實現 1 對於一些運算子的過載 以下是我們建立乙個日期類及最基本的函式宣告。includeusing namespace std class date date operator const date date date operator date operator int date o...
日期類的簡單實現
1.要考慮到日期的合法性,如果不合法,置成1990.1.1 2.由於在日期類,沒有用到資源的開闢,所以我們可以使用編譯器自動合成的拷貝構造,賦值運算子過載等。3.實現乙個日期加上n天後,日期為多少 4.實現乙個日期減去n天後,日期為多少 5.求兩個日期之間相隔多少天 如下 include using...
日期類的簡單實現
這篇部落格簡單實現乙個日期類,直接發 class date if month 2 year 4 0 year 100 0 year 400 0 else 這個函式的功能是輸入年和月來判斷這個月的天數 date int year 0,int month 1,int day 1 else 判斷日期是不是...