C++實(shí)現(xiàn)日期類(lèi)的方法詳解
一、日期類(lèi)的實(shí)現(xiàn)
1.1日期類(lèi)功能
頭文件中是我們要實(shí)現(xiàn)日期類(lèi)功能的函數(shù)聲明。這里我們要注意拷貝函數(shù),只能在函數(shù)聲明時(shí)寫(xiě)缺省值,防止我們?cè)诼暶骱投x是給的缺省值不一樣。
#include <iostream> #include <assert.h> using namespace std; class Date { public: Date(int year = 1, int month = 1, int day = 1); void Print(); int GetMonthDay(int year, int month); bool operator==(const Date& y); bool operator!=(const Date& y); bool operator>(const Date& y); bool operator<(const Date& y); bool operator>=(const Date& y); bool operator<=(const Date& y); int operator-(const Date& d); Date& operator+=(int day); Date operator+(int day); Date& operator-=(int day); Date operator-(int day); Date& operator++(); Date operator++(int); Date& operator--(); Date operator--(int); private: int _year; int _month; int _day; };
1.2拷貝日期
有時(shí)輸入的日期可能是非法的,例如:月份大于12,日期大于31,還有閏2月天數(shù)等。所以我們要對(duì)輸入的日期進(jìn)行判斷,因?yàn)槊總€(gè)月的天數(shù)不同,所以要用到GetMonthDay函數(shù)。
Date::Date(int year, int month, int day) { _year = year; _month = month; _day = day; if (_year < 1 || _month < 1 || _month>12 || _day < 1 || _day > GetMonthDay(_year, _month)) { //assert(false); Print(); cout << "日期非法" << endl; } } int Date::GetMonthDay(int year, int month) { assert(year >= 1 && month >= 1 && month <= 12); int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 }; if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) return 29; return monthArray[month]; }
小Tips:我們先判斷是否是二月,在判斷是否是閏年可以提高效率。
1.3重載關(guān)系運(yùn)算符
關(guān)系運(yùn)算符有:<、>、==、<=、>=、!=,我們?cè)趯?xiě)實(shí)現(xiàn)這些功能的代碼時(shí),會(huì)發(fā)現(xiàn)它們邏輯相同,會(huì)復(fù)制粘貼,但這樣的代碼看著十分冗余,我們可以通過(guò)復(fù)用來(lái)實(shí)現(xiàn),讓代碼更簡(jiǎn)單。
重載==
bool Date::operator==(const Date& y) { return _year == y._year && _month == y._month && _day == y._day; }
重載!=
bool Date::operator!=(const Date& y) { return !(*this == y); }
重載<
bool Date::operator<(const Date& d) { 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 Date::operator<=(const Date& y) { return *this < y || *this == y; }
重載>
bool Date::operator>(const Date& y) { return !(*this <= y); }
重載>=
bool Date::operator>=(const Date& y) { return !(*this < y); }
1.4重載+、+=
我們想知道50天之后的日期,就可以通過(guò)重載+、+=來(lái)實(shí)現(xiàn)。在加天數(shù)的時(shí)候,由于每個(gè)月的天數(shù)不一樣,所以進(jìn)位就不同,我們要得到每個(gè)月份的天數(shù)。
獲得月份的天數(shù)
int Date::GetMonthDay(int year, int month) { assert(year >= 1 && month >= 1 && month <= 12); int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 }; if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) return 29; return monthArray[month]; }
小Tips:把month == 2放在前面判斷可以提高效率,如果不是二月就不需要判斷是否是閏年,如果是二月在判斷。
重載+=
Date& Date::operator+=(int day) { //天數(shù)為負(fù)數(shù)時(shí),復(fù)用-= if (day < 0) { return *this -= (-day); } _day += day; while (_day > GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); _month++; if (_month == 13) { _year++; _month = 1; } } return *this; }
重載+
Date Date::operator+(int day) { if(x < 0) { return *this - (-day); } Date tmp(*this); tmp._day = _day + x; while (tmp._day > GetDay(tmp._year, tmp._month)) { tmp._day = tmp._day - GetDay(tmp._year, tmp._month); tmp._month++; if (tmp._month == 13) { tmp._year++; tmp._month = 1; } } return tmp; }
小Tips:重載+是原來(lái)的日期不會(huì)改變,所以我們拷貝構(gòu)造了一個(gè)和*this相同的對(duì)象,我們對(duì)tmp對(duì)象修改,不會(huì)改變*this。重載+=是在原來(lái)的日期上直接修改,所以我們直接對(duì)*this指向的日期進(jìn)行修改,然后返回就可以。
為什么重載+=用引用返回?
在重載+=中,*this就是d1,它的作用域是函數(shù)結(jié)束后才銷(xiāo)毀,由于傳值返回會(huì)拷貝一份返回值,所以為了減少返回時(shí)的拷貝,所以使用引用返回。在重載+中,tmp出了operator+函數(shù)就被銷(xiāo)毀,所以只能使用傳值返回。
+
和+=
之間的復(fù)用
+
復(fù)用+=
Date Date::operator+(int day) { Date tmp(*this); tmp += day; return tmp; }
1.5重載 -、-=
有時(shí)我們想知道以前的日期,日期-天數(shù)可以知道多少天以前的日期,日期-日期可以知道兩個(gè)日期直接隔了多少天。兩個(gè)operator-函數(shù)構(gòu)成了函數(shù)重載。
重載-=
Date& operator-=(int x) { //天數(shù)天數(shù)小于0,復(fù)用+= if (x < 0) { return *this += -x; } _day -= x; while (_day <= 0) { _month--; if (_month == 0) { _month = 12; _year--; } _day += GetDay(_year, _month); } return *this; }
重載日期-天數(shù)
Date Date::operator-(int x) { Date tmp(*this); return tmp -= x; }
重載日期-日期
日期-
日期,計(jì)算的結(jié)果是兩個(gè)日期之間的天數(shù),所以返回值是int
,要知道兩個(gè)日期之間相隔的天數(shù),可以設(shè)置一個(gè)計(jì)數(shù)器,讓小日期一直加到大日期,就可以知道兩個(gè)日期之間相隔的天數(shù)。
int operator-(const Date& d) { Date max = *this;//存放大日期 Date min = d;//存放小日期 int flag = 1; if (*this < d) { max = d; min = *this; flag = -1; } int n = 0; while (max != min) { --max; ++n; } return n * flag; }
1.6重載++、--
重載前置++
前置++要返回++之后的值
Date& Date::operator++() { *this += 1;//復(fù)用+= return *this; }
重載前置++
后置++要返回++之前的值
Date Date::operator++(int)//編譯器會(huì)把有int的視為后置++ { Date tmp(*this); *this += 1;//復(fù)用+= return tmp; }
小Tips:這兩個(gè)operator++函數(shù)構(gòu)成了函數(shù)重載,那調(diào)用的時(shí)候怎么區(qū)分前置++和后置++呢?后置重載的時(shí)候多增加一個(gè)int
類(lèi)型的參數(shù),使用后置++時(shí),調(diào)用運(yùn)算符重載函數(shù)時(shí)不用傳遞參數(shù),編譯器自動(dòng)傳遞。
重載前置 --
前置--要返回--之后的值
Date& operator--() { *this -= 1;//復(fù)用-= return *this; }
重載后置 --
后置--要返回--之前的值
Date operator--(int) { Date tmp(*this); *this -= 1;//復(fù)用了-= return tmp; }
結(jié)語(yǔ):
到此這篇關(guān)于C++實(shí)現(xiàn)日期類(lèi)的方法詳解的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)日期類(lèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言入門(mén)篇--變量[定義,初始化賦值,外部聲明]
本篇文章是c語(yǔ)言基礎(chǔ)篇,本文對(duì)初識(shí)c語(yǔ)言的變量、變量的定義、初始化與賦值、變量的分類(lèi)、含義、外部聲明做了簡(jiǎn)要的描述,幫助大家快速入門(mén)c語(yǔ)言的世界,更好的理解c語(yǔ)言2021-08-08簡(jiǎn)單聊聊C++中回調(diào)函數(shù)的實(shí)現(xiàn)
回調(diào)函數(shù)就是一個(gè)通過(guò)函數(shù)指針調(diào)用的函數(shù),如果你把函數(shù)的指針(地址)作為參數(shù)傳遞給另一個(gè)函數(shù),當(dāng)這個(gè)指針被用來(lái)調(diào)用其所指向的函數(shù)時(shí),我們就說(shuō)這是回調(diào)函數(shù),下面這篇文章主要給大家介紹了關(guān)于C++中回調(diào)函數(shù)實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-01-01vc控制臺(tái)程序關(guān)閉事件時(shí)的處理方式及注意點(diǎn)詳解
在本篇文章里小編給大家整理的是一篇關(guān)于vc控制臺(tái)程序關(guān)閉事件時(shí)的正確處理方式的相關(guān)知識(shí)點(diǎn)內(nèi)容,對(duì)此有需求的朋友們可以參閱下。2021-12-12C/C++?for?語(yǔ)句的要點(diǎn)與注意事項(xiàng)小結(jié)
C/C++ 中的?for?語(yǔ)句是一種常用的循環(huán)結(jié)構(gòu),用于重復(fù)執(zhí)行一段代碼,直到滿足某個(gè)條件為止,這篇文章主要介紹了C/C++?for?語(yǔ)句的要點(diǎn)與注意事項(xiàng),需要的朋友可以參考下2024-06-06如何查看進(jìn)程實(shí)際的內(nèi)存占用情況詳解
本篇文章是對(duì)如何查看進(jìn)程實(shí)際的內(nèi)存占用情況進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C語(yǔ)言全面梳理結(jié)構(gòu)體知識(shí)點(diǎn)
結(jié)構(gòu)體是一些值的集合,這些值稱(chēng)為成員變量,結(jié)構(gòu)體的每個(gè)成員可以是不同類(lèi)型的變量。本文將通過(guò)示例為大家詳細(xì)講講C語(yǔ)言中結(jié)構(gòu)體的使用,需要的可以參考一下2022-07-07利用C語(yǔ)言實(shí)現(xiàn)將格式化數(shù)據(jù)和字符串相互轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了2個(gè)函數(shù),分別是sprintf和sscanf,可以用來(lái)實(shí)現(xiàn)將格式化數(shù)據(jù)和字符串相互轉(zhuǎn)換,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03基于QT制作一個(gè)TCPServer與TCPClient的通信
這篇文章主要為大家詳細(xì)介紹了如何基于QT制作一個(gè)TCPServer與TCPClient的通信,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12