C++日期類(lèi)運(yùn)算符重載方式
構(gòu)造函數(shù)
定義一個(gè)全缺省的構(gòu)造函數(shù),函數(shù)體內(nèi)判斷當(dāng)前日期是否合法,如果是非法日期,則初始化為2022,1,1
Date(int year=2022, int month=1, int day=1) ?? ??? ?:_year(year) ?? ??? ?, _month(month) ?? ??? ?, _day(day) ?? ?{ ?? ??? ?if (year <1 || month < 1 || month>12 || day<1 || day>GetDayOfMonth(year,month)) ?? ??? ?{ ?? ??? ??? ?_year = 2022; ?? ??? ??? ?_month = 1; ?? ??? ??? ?_day = 1; ?? ??? ?} ?? ?}
拷貝構(gòu)造
?? ?Date(const Date& d) ?? ?{ ?? ??? ?_year = d._year; ?? ??? ?_month = d._month; ?? ??? ?_day = d._day; ?? ?}
賦值運(yùn)算符重載
賦值運(yùn)算符編譯器會(huì)默認(rèn)生成,對(duì)于日期類(lèi)可以不用自己定義,自己定義需要注意三點(diǎn):
- 參數(shù)和返回值都是類(lèi)類(lèi)型的引用
- 判斷是否為自己給自己賦值,因?yàn)閮蓪?duì)象不能直接比較,所以判斷時(shí)用地址
- 為了支持連續(xù)賦值,必須返回*this
?? ?Date& operator=(const Date& d) ?? ?{ ?? ??? ?//不能用對(duì)象直接比較,只能比較地址 ?? ??? ?//if (d != (*this)) ?? ??? ?if(&d!=this) ?? ??? ?{ ?? ??? ??? ?_year = d._year; ?? ??? ??? ?_month = d._month; ?? ??? ??? ?_day = d._day; ?? ??? ?} ?? ??? ?return *this; ?? ?}
“+” 日期+天數(shù)
"+“與”+=“區(qū)分,”+"不改變?cè)兞康闹?,只是通過(guò)返回值返回,所以這里要定義一個(gè)臨時(shí)變量temp,由于temp是保存在棧上的臨時(shí)變量,所以返回值為Date類(lèi)型,不能返回引用
先判斷正負(fù),如果加的數(shù)是負(fù)數(shù),則復(fù)用"-"的重載
加完后判斷_day的合法性,如果_day非法,_day減去當(dāng)前月份的天數(shù),月份增加,直到日期合法
? ? //日期+天數(shù) ?? ?Date operator+(int day) ?? ?{ ?? ??? ?//不能改變?cè)腥掌冢砸x臨時(shí)變量 ?? ??? ?//臨時(shí)變量不能返回引用 ?? ??? ?if (day < 0) ?? ??? ?{ ?? ??? ??? ?day = -day; ?? ??? ??? ?return *this - day; ?? ??? ?} ?? ??? ?Date temp=*this; ?? ??? ?temp._day += day; ?? ??? ?while (temp._day > GetDayOfMonth(_year, _month)) ?? ??? ?{ ?? ??? ??? ?temp._day -= GetDayOfMonth(temp._year,temp._month); ?? ??? ??? ?temp._month++; ?? ??? ??? ?if (temp._month > 12) ?? ??? ??? ?{ ?? ??? ??? ??? ?temp._month = 1; ?? ??? ??? ??? ?temp._year++; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?return temp; ?? ?}
“-” 日期-天數(shù)
與"+"同原理,如果_day減為0或者負(fù)數(shù),先將月份下調(diào)一個(gè)月,再將日加上上調(diào)后月份的天數(shù),如8月0日表示7月31,8月-1日,則表示7月30日。
?? ?//日期-天數(shù) ?? ?Date operator-(int day) ?? ?{ ?? ??? ?if (day < 0) ?? ??? ?{ ?? ??? ??? ?day = -day; ?? ??? ??? ?return *this + day; ?? ??? ?} ?? ??? ?Date temp = *this; ?? ??? ?temp._day -= day; ?? ??? ?while (temp._day <= 0) ?? ??? ?{ ?? ??? ??? ?temp._month--; ?? ??? ??? ?if (temp._month < 1) ?? ??? ??? ?{ ?? ??? ??? ??? ?temp._year--; ?? ??? ??? ??? ?temp._month = 12; ?? ??? ??? ?} ?? ??? ??? ?temp._day += GetDayOfMonth(temp._year ,temp._month); ?? ??? ?} ?? ??? ?return temp; ?? ?}
“+=” 日期+=天數(shù)
"+=“與”+“的區(qū)別是,加后要個(gè)自己賦值,所以返回類(lèi)類(lèi)型對(duì)象的引用,可以復(fù)用”=“和”+"的重載
?? ?//日期+=天數(shù) ?? ?Date& operator+=(int days) ?? ?{ ?? ??? ?//復(fù)用=和+ ?? ??? ?//這里要先將加的和賦值給*this,再返回 ?? ??? ?//非常量左值引用只能引用非常量左值 ?? ??? ?//如果要直接return(*this + days),返回值類(lèi)型應(yīng)為Date&& ?? ??? ?*this = *this + days; ?? ??? ?return * this; ?? ?}
“-=” 日期-=天數(shù)
?? ?//日期-=天數(shù) ?? ?Date& operator-=(int days) ?? ?{ ?? ??? ?*this = *this - days; ?? ??? ?return *this; ?? ?}
前置"++"
前置"++"返回的是加一后的值,可以直接給*this加一后返回
?? ?//前置++ ?? ?Date& operator++() ?? ?{ ?? ??? ?*this += 1; ?? ??? ?return *this; ?? ?}
后置"++"
C++中,后置"++“參數(shù)加一個(gè)int,無(wú)實(shí)際意義,區(qū)分前置與后置”++“,由于后置”++"要返回的是自增之前的值,所以要定義一個(gè)臨時(shí)變量,自增后返回該臨時(shí)變量的值
?? ?//后置++ ?? ?// ?? ?Date operator++(int) ?? ?{ ?? ??? ?//返回的是自增之前的值 ?? ??? ?Date temp = *this; ?? ??? ?*this += 1; ?? ??? ?return temp; ?? ?}
前置"–"
與前置"++"原理相同
?? ?//前置-- ?? ?Date& operator--() ?? ?{ ?? ??? ?*this -= 1; ?? ??? ?return *this; ?? ?}
后置"- -"
與后置"++"原理相同
?? ?//后置-- ?? ?Date operator--(int) ?? ?{ ?? ??? ?Date temp = *this; ?? ??? ?*this-= 1; ?? ??? ?return temp; ?? ?}
“==”
判斷每變量是否相等
?? ?//== ?? ?bool operator==(Date& d)const ?? ?{ ?? ??? ?return d._year == _year && ?? ??? ??? ?d._month == _month && ?? ??? ??? ?d._day == _day; ?? ?}
“>”
從年到月日依次判斷
?? ?//> ?? ?bool operator>(Date& d)const ?? ?{ ?? ??? ?if (_year > d._year) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else if (_year == d._year && _month > d._month) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else if (_year == d._year && _month == d._month && _day > d._day) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?return false; ?? ??? ?} ?? ?}
“<”
?? ?//< ?? ?bool operator<(Date& d)const ?? ?{ ?? ??? ?if (_year < d._year) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else if (_year == d._year && _month < d._month) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else if (_year == d._year && _month == d._month && _day < d._day) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?return false; ?? ??? ?} ?? ?}
“-” 日期-日期
定義一個(gè)計(jì)數(shù)器統(tǒng)計(jì)從小的日期到大的日期需要自增多少次
?? ?int operator-(const Date& d)const ?? ?{ ?? ??? ?int count = 0; ?? ??? ?Date start(*this); ?? ??? ?Date end(d); ?? ??? ?//保證大的日期減小的日期 ?? ??? ?if (start>end) ?? ??? ?{ ?? ??? ??? ?Date temp = end; ?? ??? ??? ?end = start; ?? ??? ??? ?start = temp; ?? ??? ?} ?? ??? ?while(end>start) ?? ??? ?{ ?? ??? ??? ?start++; ?? ??? ??? ?count++; ?? ??? ?} ?? ??? ?return count; ?? ?}
設(shè)置年、月、日
對(duì)外部提供修改對(duì)象值的接口
?? ?void SetYear(int year) ?? ?{ ?? ??? ?_year = year; ?? ?} ?? ?void SetMonth(int month) ?? ?{ ?? ??? ?_month = month; ?? ?} ?? ?void SetDay(int day) ?? ?{ ?? ??? ?_day = day; ?? ?}
獲取年、月、日
向外部提供獲取類(lèi)成員變量值的接口
?? ?int GetYear()const ?? ?{ ?? ??? ?return _year; ?? ?} ?? ?int GetMonth()const ?? ?{ ?? ??? ?return _month; ?? ?} ?? ? ?? ?int GetDay()const ?? ?{ ?? ??? ?return _day; ?? ?}
判斷閏年
判斷閏年,可以定義為private,不用暴露給外部
?? ?bool IsLeapYear(int year) ?? ?{ ?? ??? ?if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?return false; ?? ?}
獲取當(dāng)前月天數(shù)
定義一個(gè)數(shù)組保存每月的天數(shù),根據(jù)傳進(jìn)來(lái)的年和月返回當(dāng)前月份的天數(shù)
?? ?//獲取當(dāng)前月份的天數(shù) ?? ?int GetDayOfMonth(int year, int month) ?? ?{ ?? ??? ?int arr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; ?? ??? ?if (IsLeapYear(year)) ?? ??? ?{ ?? ??? ??? ?arr[2] = 29; ?? ??? ?} ?? ??? ?return arr[month]; ?? ?}
日期類(lèi)完整代碼
class Date { public: ?? ?//全缺省構(gòu)造函數(shù) ?? ?Date(int year=2022, int month=2, int day=2) ?? ??? ?:_year(year) ?? ??? ?, _month(month) ?? ??? ?, _day(day) ?? ?{ ?? ??? ?if (year <1 || month < 1 || month>12 || day<1 || day>GetDayOfMonth(year,month)) ?? ??? ?{ ?? ??? ??? ?_year = 2022; ?? ??? ??? ?_month = 1; ?? ??? ??? ?_day = 1; ?? ??? ?} ?? ?} ?? ?//拷貝構(gòu)造 ?? ?Date(const Date& d) ?? ?{ ?? ??? ?_year = d._year; ?? ??? ?_month = d._month; ?? ??? ?_day = d._day; ?? ?} ?? ?//析構(gòu)——無(wú)實(shí)際意義 ?? ?~Date() ?? ?{ ?? ??? ?cout << "~Date()" << endl; ?? ?} ?? ?//賦值運(yùn)算符重載(不寫(xiě)也可以,編譯器會(huì)默認(rèn)生成) ?? ?Date& operator=(const Date& d) ?? ?{ ?? ??? ?//不能用對(duì)象直接比較,只能比較地址 ?? ??? ?//if (d != (*this)) ?? ??? ?if(&d!=this) ?? ??? ?{ ?? ??? ??? ?_year = d._year; ?? ??? ??? ?_month = d._month; ?? ??? ??? ?_day = d._day; ?? ??? ?} ?? ??? ?return *this; ?? ?} ?? ?//日期+天數(shù) ?? ?Date operator+(int day) ?? ?{ ?? ??? ?//不能改變?cè)腥掌?,所以要定義臨時(shí)變量 ?? ??? ?//臨時(shí)變量不能返回引用 ?? ??? ?if (day < 0) ?? ??? ?{ ?? ??? ??? ?day = -day; ?? ??? ??? ?return *this - day; ?? ??? ?} ?? ??? ?Date temp=*this; ?? ??? ?temp._day += day; ?? ??? ?while (temp._day > GetDayOfMonth(_year, _month)) ?? ??? ?{ ?? ??? ??? ?temp._day -= GetDayOfMonth(temp._year,temp._month); ?? ??? ??? ?temp._month++; ?? ??? ??? ?if (temp._month > 12) ?? ??? ??? ?{ ?? ??? ??? ??? ?temp._month = 1; ?? ??? ??? ??? ?temp._year++; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?return temp; ?? ?} ?? ?//日期-天數(shù) ?? ?Date operator-(int day) ?? ?{ ?? ??? ?if (day < 0) ?? ??? ?{ ?? ??? ??? ?day = -day; ?? ??? ??? ?return *this + day; ?? ??? ?} ?? ??? ?Date temp = *this; ?? ??? ?temp._day -= day; ?? ??? ?while (temp._day <= 0) ?? ??? ?{ ?? ??? ??? ?temp._month--; ?? ??? ??? ?if (temp._month < 1) ?? ??? ??? ?{ ?? ??? ??? ??? ?temp._year--; ?? ??? ??? ??? ?temp._month = 12; ?? ??? ??? ?} ?? ??? ??? ?temp._day += GetDayOfMonth(temp._year ,temp._month); ?? ??? ?} ?? ??? ?return temp; ?? ?} ?? ?//日期+=天數(shù) ?? ?Date& operator+=(int days) ?? ?{ ?? ??? ?//復(fù)用=和+ ?? ??? ?//這里要先將加的和賦值給*this,再返回 ?? ??? ?//非常量左值引用只能引用非常量左值 ?? ??? ?//如果要直接return(*this + days),返回值類(lèi)型應(yīng)為Date&& ?? ??? ?*this = *this + days; ?? ??? ?return * this; ?? ?} ?? ?//日期-=天數(shù) ?? ?Date& operator-=(int days) ?? ?{ ?? ??? ?*this = *this - days; ?? ??? ?return *this; ?? ?} ?? ?//前置++ ?? ?Date& operator++() ?? ?{ ?? ??? ?*this += 1; ?? ??? ?return *this; ?? ?} ?? ?//后置++ ?? ?//C++語(yǔ)法,后置++參數(shù)加一個(gè)int,無(wú)實(shí)際意義,區(qū)分前置與后置++ ?? ?Date operator++(int) ?? ?{ ?? ??? ?//返回的是自增之前的值 ?? ??? ?Date temp = *this; ?? ??? ?*this += 1; ?? ??? ?return temp; ?? ?} ?? ?//前置-- ?? ?Date& operator--() ?? ?{ ?? ??? ?*this -= 1; ?? ??? ?return *this; ?? ?} ?? ?//后置-- ?? ?Date operator--(int) ?? ?{ ?? ??? ?Date temp = *this; ?? ??? ?*this-= 1; ?? ??? ?return temp; ?? ?} ?? ?//== ?? ?bool operator==(Date& d)const ?? ?{ ?? ??? ?return d._year == _year && ?? ??? ??? ?d._month == _month && ?? ??? ??? ?d._day == _day; ?? ?} ?? ?//> ?? ?bool operator>(Date& d)const ?? ?{ ?? ??? ?if (_year > d._year) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else if (_year == d._year && _month > d._month) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else if (_year == d._year && _month == d._month && _day > d._day) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?return false; ?? ??? ?} ?? ?} ?? ?//< ?? ?bool operator<(Date& d)const ?? ?{ ?? ??? ?if (_year < d._year) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else if (_year == d._year && _month < d._month) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else if (_year == d._year && _month == d._month && _day < d._day) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?return false; ?? ??? ?} ?? ?} ?? ?//日期-日期 即還有多少天到某一天? ?? ?int operator-(const Date& d)const ?? ?{ ?? ??? ?int count = 0; ?? ??? ?Date start(*this); ?? ??? ?Date end(d); ?? ??? ?//保證大的日期減小的日期 ?? ??? ?if (start>end) ?? ??? ?{ ?? ??? ??? ?Date temp = end; ?? ??? ??? ?end = start; ?? ??? ??? ?start = temp; ?? ??? ?} ?? ??? ?while(end>start) ?? ??? ?{ ?? ??? ??? ?start++; ?? ??? ??? ?count++; ?? ??? ?} ?? ??? ?return count; ?? ?} ?? ?//設(shè)置年、月、日接口 ?? ?void SetYear(int year) ?? ?{ ?? ??? ?_year = year; ?? ?} ?? ?void SetMonth(int month) ?? ?{ ?? ??? ?_month = month; ?? ?} ?? ?void SetDay(int day) ?? ?{ ?? ??? ?_day = day; ?? ?} ?? ?//獲取年、月、日接口 ?? ?int GetYear()const ?? ?{ ?? ??? ?return _year; ?? ?} ?? ?int GetMonth()const ?? ?{ ?? ??? ?return _month; ?? ?} ?? ? ?? ?int GetDay()const ?? ?{ ?? ??? ?return _day; ?? ?} private: ?? ?int _year; ?? ?int _month; ?? ?int _day; ?? ?//判斷閏年 ?? ?bool IsLeapYear(int year) ?? ?{ ?? ??? ?if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ?? ??? ?{ ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?return false; ?? ?} ?? ?//獲取當(dāng)前月份的天數(shù) ?? ?int GetDayOfMonth(int year, int month) ?? ?{ ?? ??? ?int arr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; ?? ??? ?if (IsLeapYear(year)) ?? ??? ?{ ?? ??? ??? ?arr[2] = 29; ?? ??? ?} ?? ??? ?return arr[month]; ?? ?} };
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Qt實(shí)現(xiàn)轉(zhuǎn)動(dòng)輪播圖
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)轉(zhuǎn)動(dòng)輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06用C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單五子棋小游戲
這篇文章主要為大家詳細(xì)介紹了用C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07C語(yǔ)言編程PAT乙級(jí)學(xué)習(xí)筆記示例分享
這篇文章主要為大家介紹了C語(yǔ)言編程PAT乙級(jí)學(xué)習(xí)筆記實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05解決codeblocks斷點(diǎn)不停無(wú)效的問(wèn)題
今天小編就為大家分享一篇解決codeblocks斷點(diǎn)不停無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12C++類(lèi)靜態(tài)成員與類(lèi)靜態(tài)成員函數(shù)詳解
靜態(tài)成員不可在類(lèi)體內(nèi)進(jìn)行賦值,因?yàn)樗潜凰性擃?lèi)的對(duì)象所共享的。你在一個(gè)對(duì)象里給它賦值,其他對(duì)象里的該成員也會(huì)發(fā)生變化。為了避免混亂,所以不可在類(lèi)體內(nèi)進(jìn)行賦值2013-09-09Assert(斷言實(shí)現(xiàn)機(jī)制深入剖析)
言前后最好空一格[編程風(fēng)格的問(wèn)題,按你自已的喜好,適合自已就最好]。斷言只是用來(lái)檢查程序的邏輯正確性,不能代替條件替換。斷言比printf語(yǔ)句這種形式的打印好使2013-09-09