日期計算器

2021-08-28 07:50:55 字數 2840 閱讀 9304

class date

return *this; //賦值操作已經完成,為什麼有個返回值?因為賦值運算子的過載支援三個數,i=j=k;k先賦給j後有乙個返回值j,將這個返回值賦給i,返回的是同型別物件,所以型別為date

//但此時如果沒有給函式型別加引用,就是傳值返回,不會直接返回,會建立乙個臨時物件。。會多一次拷貝構造,拷貝乙個臨時物件,再拿這個臨時物件做返回

//傳值返回:返回的是乙個臨時物件

//傳引用返回:返回的物件出了作用域還在

} //d1 < d2 會先去全域性區找有沒有小於的運算子過載,沒有的話就到類中找,找到了,其實就轉化為->d1.operator<(&d1,d2)

//bool operator<(const date& x1) //在類外面我們實現了第一次的operator,對類外進行改進

//

// else if(_year == x1._year)

// // else if(_month == x1._month)

//

// }

// return false;

// }

//d1+10

//date operator+(int day) //自己寫的

// // }

// return *this;*/

//}//date operator+(int day)

// // }

// return ret; //返回乙個臨時變數,所以用傳值返回。

//}d1 += 10

//date& operator+=(int day)

// date& operator+=(int day)

_day += day;

while(_day > getmonthday(_year,_month))

}return *this;

} //d+10

date operator+(int day)

//自己寫的

/*date operator-(int day)

}return ret;

} date& operator-=(int day)

*/ //上課

/*date operator-(int day)

}return ret;

}*/date& operator-=(int day)

_day -= day;

while(_day <= 0)

_day+=getmonthday(_year,_month);

} return *this;

} date operator-(int day)

//d1 -d2

int operator-(const date& d) //不加const d2會被改

int day = 0;

while(min < max)

return day*flag;

} //++d => d.operator++(&d)

date& operator++() //前置 返回值是++後的值

//d++ => operator++(&d,0)

date operator++(int) //後置 int只是為了與前置做乙個區分 返回的是++前的值

date& operator--()

date operator--(int)

bool operator>(const date& d)

else if(_year == d._year)

else if(_month == d._month)

}return false;

} bool operator==(const date& d)

bool operator>=(const date& d)

bool operator<(const date& d)

bool operator<=(const date& d)

bool operator!=(const date& d)

//~date() //這個日期類可以不去清理,但有的類需要清理,比如順序表

// void display()

int getmonthday(int year,int month)

; int day = monthday[month];

if(month == 2

&& ((year%400 == 0) || (year%4 == 0 && year%100 != 0)))

return day;

}private:

int _year;

int _month;

int _day;

};//bool operator<(const date& x1,const date& x2) //要穿引用返回;如果傳值雖然不會無窮遞迴,但傳值代價大,要開闢空間還要調拷貝建構函式。

// //並且比較完不會改變x1x2所以最好加乙個const //引用做引數提高效率,提高效能

// //這個函式之所以完成,是因為吧成員變數定義為了公有,但公有會破壞c++封裝性,可是定義為私有我這個函式就跑不了。。在類外不可訪問,那就放在類中

// //但直接放在類中又有錯誤:operator《的引數太多

//// else if(x1._year == x2._year)

// // else if(x1._month == x2._month)

//

// }

// return false;

//}void test()

日期計算器

define crt secure no warnings include includeusing namespace std class date bool operator const date d 小於運算子的過載 bool operator const date d 等於運算子的過載 bo...

日期計算器

日期計算器 生活中我們可能要計算一些距某乙個時間還有多少天的時候,但是在日曆裡算起來可能要麻煩一下,在學了c 的類之後可以很好的解決這一類的問題。例如距高考還有多少天 100天之後是幾號等。接下來就用日期類完成這些基本的日期運算功能 1.include 2.using namespace std 3...

日期計算器

注意 此日曆只能用於計算1982年10月15日以及以後的日期 1.日期 天數 返回日期 處理 1.如果 乙個負的天數的情況 2.如果加之後,或減之後的日期非法 2.兩個日期之間間隔的天數,日期 日期 返回的天數 處理 返回的天數不能為負 3.列印某年某月本月的日曆 處理 1982年十月份 功能測試 ...