C 模擬實現日期類

2021-08-20 23:13:44 字數 1692 閱讀 2616

本篇部落格完善了日期類:實現了以下操作

class date 

;

實現:

class date

} //也可以不用給

date(const date& d)

:_year(d._year)

, _month(d._month)

, _day(d._day)

{} // 當前日期days天後是什麼日期?

date operator+(int days)

daysofmonths = getdaysofmonths(tmp._year, tmp._month);

}}

return tmp;

} // 當前日期days天前是什麼日期?

date operator-(int days)

else

int daysofmonth = getdaysofmonths(tmp._year, tmp._month);

tmp._day += daysofmonth;

}}

return tmp;

} // 兩個日期之間差了多少天?

int operator-(const date& d)

int _count = 0;

while (mindate != maxdate)

return _count;

} // 日期比大小

//大於

bool operator>(const date& d)

//小於

bool operator<(const date& d)

//等於

bool operator==(const date& d)

//不等於

bool operator!=(const date& d)

//實現連續賦值

date& operator=(const date& d)

return *this;

} // 過載取位址符號

date* operator&()

//後置++

date operator++(int)

//前置++

date& operator++()

// 前置--

date& operator--()

// 後置--

date operator--(int)

//沒必要給,因為沒有資源,不需要清理

~date()

{} //輸出《的過載,輸出日期

friend ostream& operator<<(ostream& _cout, const date& d)

private:

//獲取乙個月裡有幾天

int getdaysofmonths(int year, int month)

; if (month == 2 && isleap(year))

days[2] += 1;

return days[month];

} //判斷是否是閏年

bool isleap(int year)

private:

int _year;

int _month;

int _day;

};

模擬實現日期類

日期類好久沒寫了,重溫一下以前的知識。寫日期類需要注意的有 1 日期減日期的計算 2 關於輸出輸入過載的友元函式宣告 3 建構函式的條件判斷 4 拷貝建構函式的自我賦值判斷 實現 如下 include using namespace std class date else 拷貝建構函式 date c...

模擬實現string類 c

include include using namespace std class string string const string m 拷貝建構函式 string 析構函式 string operator string m1 賦值運算子過載 s new char strlen m1.s 1 s...

模擬實現String類(C )

以下就是string類的模擬實現 測試環境為vs2013 define crt secure no warnings 1 include include include using namespace std class string 拷貝建構函式 string const string s str...