#includeusing namespace std;
//主要函式及其含義
計算當前日期day天之後日期date operator+(const date&d1, int day);
計算當前日期day天之前日期date operator-(const date&d1, int day);
計算兩個日期之間差距int operator-(const date&d1, const date& d2);
過載輸出運算子 friend ostream& operator<
過載賦值運算子date& operator=(const date& d)
class date
int getdayinmonth(int year, int month)
; if (isleapyear(year))
return months[month];
} date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{} //拷貝建構函式
date(const date& d)
: _year(d._year)
, _month(d._month)
, _day(d._day)
{} date& operator=(const date& d)
return *this;
} // 計算當前日期day天之後日期
friend date operator+(const date&d1, int day);
// 計算當前日期day天之前日期
friend date operator-(const date&d1,int day);
// 計算兩個日期之間差距
friend int operator-(const date&d1,const date& d2);
//前置++ 返回新值
date& operator++()
_day = 1;
} return *this;
} // 後置++ 返回舊值
date operator++(int)
//前置-- 返回新值
date& operator--()
else
} return *this;
} // 後置-- 返回舊值
date operator--(int)
/*一般情況下不會過載成類的成員函式,因為不符合輸出習慣
void operator<
*/bool operator>(const date& d)
return false;
} bool operator
temp._day -= temp.getdayinmonth(temp._year, temp._month);
} return temp;
} void test()
{ date d1(2017, 1, 31);
date d2(2017, 1, 1);
date d3(2017, 10, 27);
賦值運算子過載的測試**
d1 = d2;
<< 運算子過載的測試**
//d1<> >>endl;而且不能連續輸出
改進cout << d1 << " " << d2 << endl;
前置++和後置++的過載
cout << ++d1 << endl;
cout << d1++ << endl;
前置--和後置--的過載
cout << --d2 << endl;
cout << d2-- << endl;
計算當前日期day天之前日期測試**
cout << d3 - 27 << endl;
計算當前日期day天之後日期測試**
//返回值為bool型別判斷兩個日期a是否大於b
//以及返回值為bool型別判斷兩個日期a是否小於b
//以及返回值為bool型別判斷兩個日期a是否等於b
//以及返回值為bool型別判斷兩個日期a是否不等於b
cout << d3 + 5 << endl;
cout << "d1-" << d1 << endl;
cout << "d2-" << d2 << endl;
cout << "d3-" << d3 << endl;
cout << (d1>d2) << endl;
cout << (d1
test結果
test1結果
C 之日期類
學完前面的東西,現在來具體應用一下 寫乙個日期類,具體功能如下 bool leapyear int year 判斷是不是閏年 int monthday int year,int month 每月天數的判斷 date operator 前置 date operator int 後置 date oper...
c 之日期類的實現
date h pragma once include using namespace std class date date const date d date operator const date d public bool operator const date d bool operator...
c 之日期時間
c 標準庫沒有提供所謂的日期型別。c 繼承了 c 語言用於日期和時間操作的結構和函式。為了使用日期和時間相關的函式和結構,需要在 c 程式中引用 標頭檔案。有四個與時間相關的型別 clock t time t size t和tm。型別 clock t size t 和 time t 能夠把系統時間和...