C++日期類(Date)實(shí)現(xiàn)的示例代碼
類的定義
#pragma once #include < iostream> using std::cout; using std::cin; using std::endl; class Date { public: // 獲取某年某月的天數(shù) int GetMonthDay(int year, int month); // 全缺省的構(gòu)造函數(shù) Date(int year = 0, int month = 1, int day = 1); void Print(); //析構(gòu),拷貝構(gòu)造,賦值重載函數(shù)可以不用寫,默認(rèn)生成的就夠用 //日期+=天數(shù) Date& operator+=(int day); // 日+天數(shù) Date operator+(int day); // 日期-天數(shù) Date operator-(int day); // 日期-=天數(shù) Date& operator-=(int day); // 前置++ Date& operator++(); // 后置++ Date operator++(int); //后置-- Date operator--(int); // 前置-- Date& operator--(); // >運(yùn)算符重載 bool operator>(const Date& d); // ==運(yùn)算符重載 bool operator==(const Date& d); // >=運(yùn)算符重載 bool operator >= (const Date& d); // <運(yùn)算符重載 bool operator < (const Date& d); // <=運(yùn)算符重載 bool operator <= (const Date& d); // !=運(yùn)算符重載 bool operator != (const Date& d); // 日期-日期 返回天數(shù) int operator-(const Date& d); private: int _year; int _month; int _day; };
確定某年某月有多少天
//因?yàn)殚c年和平年二月份的天數(shù)不一樣,隨便加加減減會(huì)有問(wèn)題,這個(gè)函數(shù)可以根據(jù)閏年和平年給出對(duì)應(yīng)的天數(shù) //因?yàn)楹竺嫘枰l繁調(diào)用它,設(shè)置成內(nèi)聯(lián)函數(shù) inline int Date::GetMonthDay(int year, int month) { //設(shè)置成靜態(tài)變量,不用每次調(diào)用這個(gè)函數(shù)都開辟一個(gè)數(shù)組 static int arr[13] = {0,31,28,31,30,31,30,31,31,30,31, 30, 31}; int day = arr[month]; if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) day = 29; return day; }
構(gòu)造函數(shù)
Date::Date(int year, int month, int day) { //防止給的日期有錯(cuò)誤 if (year >= 0 && month > 0 && month < 13 && day >0 && day <= GetMonthDay(year, month)) { _year = year; _month = month; _day = day; } else { cout << "非法日期" << endl; cout << year << "年" << month << "月" << day << "日" << endl; } }
打印日期
void Date::Print() { cout << _year << "年" << _month << "月" << _day << "日" << endl; }
日期+=天數(shù)
Date& Date::operator+=(int day) { if (day < 0){ *this -= (-day); } else { _day += day; //如果天數(shù)大于月份對(duì)應(yīng)的最大天數(shù),就先減去這個(gè)月天數(shù),然后月份++,然后再判斷月份對(duì)應(yīng)的天數(shù)對(duì)不對(duì),往復(fù)循環(huán) while (_day > GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); ++_month; if (_month > 12) { ++_year; _month = 1; } } //如果是傳值返回的話傳的是他的一個(gè)拷貝臨時(shí)變量,又需要調(diào)用拷貝構(gòu)造函數(shù),因?yàn)槌隽撕瘮?shù)作用域?qū)ο筮€在,所以傳引用更好。 } return *this; }
日期+天數(shù)
Date Date::operator+(int day) { Date ret(*this); //復(fù)用operator+=函數(shù) ret += day; //ret是一個(gè)局部變量,出了作用域就銷毀了,傳不了引用 return ret; }
日期-=天數(shù)
Date& Date::operator-=(int day) { if (day < 0) { *this += (-day); } else { _day -= day; while (_day <= 0) { --_month; if (_month < 1) { --_year; _month = 12; } _day += GetMonthDay(_year, _month); } } return *this; }
日期-天數(shù)
Date Date::operator-(int day) { Date ret = *this; ret -= day; return ret; }
前置++
//返回的是++后的值,可以復(fù)用+=函數(shù),邏輯是一樣的,把day換成1即可 Date& Date::operator++() { *this += 1; return *this; }
后置++
//返回的是++前的值,不能傳引用返回 //為了和前置++賦值重載函數(shù)構(gòu)成函數(shù)重載,需要加個(gè)int ,不需要傳實(shí)參,因?yàn)闆](méi)用,如果不加參數(shù)那么前置++和后置++函數(shù)就一樣了,們?cè)谶M(jìn)行前置++或者后置++操作時(shí),編譯器就不知道調(diào)用哪一個(gè)了 Date Date::operator++(int) { Date ret(*this); *this += 1; return ret; }
后置–
//返回原對(duì)象,然后對(duì)象再-- Date Date::operator--(int) { Date ret = *this; *this -= 1; return ret; }
前置–
返回--后的對(duì)象
Date& Date::operator--() { *this -= 1; return *this; }
>運(yùn)算符重載
d1 > d2 -> d1.operator>(&d1, d2) bool Date::operator>(const Date& d) { if (_year >= d._year) { if (_year > d._year) return true; else { if (_month >= d._month) { if (_month > d._month) return true; else { if (_day > d._day) return true; } } } } return false; }
==運(yùn)算符重載
bool Date::operator==(const Date& d) { return _year == d._year && _month == d._month && _day == d._day; }
>=運(yùn)算符重載
bool Date::operator >= (const Date& d) { return *this > d || *this == d; }
<運(yùn)算符重載
bool Date::operator < (const Date& d) { return !(*this >= d); }
<=運(yùn)算符重載
bool Date::operator <= (const Date& d) { return !(*this > d); }
!=運(yùn)算符重載
bool Date::operator != (const Date& d) { return !(*this == d); }
計(jì)算兩個(gè)日期之間的間隔天數(shù),日期減去日期
int Date::operator-(const Date& d) { Date min = *this; Date max = d; int flag = -1; int daycount = 0; if (*this > d) { min = d; max = *this; flag = 1; } while (min != max) { ++min; daycount++; } return flag * daycount; }
到此這篇關(guān)于C++日期類(Date)實(shí)現(xiàn)的示例代碼的文章就介紹到這了,更多相關(guān)C++日期類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++ 運(yùn)用Npcap發(fā)送UDP數(shù)據(jù)包的完美過(guò)程
UDP 是一種無(wú)連接、輕量級(jí)的傳輸層協(xié)議,與 TCP 相比,它不提供可靠性、流控制和錯(cuò)誤恢復(fù)機(jī)制,但卻更加簡(jiǎn)單且具有較低的開銷,這篇文章主要介紹了C/C++ 運(yùn)用Npcap發(fā)送UDP數(shù)據(jù)包,需要的朋友可以參考下2023-11-11C++實(shí)現(xiàn)職工工資管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單的職工工資管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C++實(shí)現(xiàn)數(shù)字轉(zhuǎn)換為十六進(jìn)制字符串的方法
這篇文章主要介紹了C++實(shí)現(xiàn)數(shù)字轉(zhuǎn)換為十六進(jìn)制字符串的方法,涉及C++操作數(shù)字與字符串轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-06-06C語(yǔ)言進(jìn)階教程之字符串&內(nèi)存函數(shù)
對(duì)于字符,在計(jì)算機(jī)內(nèi)部都是用數(shù)字(字符編碼)來(lái)表示的,而字符串是“字符連續(xù)排列”的一種表現(xiàn),這篇文章主要給大家介紹了關(guān)于C語(yǔ)言進(jìn)階教程之字符串&內(nèi)存函數(shù)的相關(guān)資料,需要的朋友可以參考下2021-09-09Qt6遠(yuǎn)程連接MySQL數(shù)據(jù)庫(kù)的簡(jiǎn)單易上手版
在Qt應(yīng)用程序里,可實(shí)現(xiàn)遠(yuǎn)程MySQL服務(wù)器的連接操作,本文就來(lái)介紹一下Qt6遠(yuǎn)程連接MySQL數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11C++ LeeCode題目:比特位計(jì)數(shù)和買賣股票的最佳時(shí)機(jī)
這篇文章主要介紹了基于C語(yǔ)言計(jì)算比特位計(jì)數(shù)和買賣股票的最佳時(shí)機(jī),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-07-07