C++日期類(Date)實現(xiàn)的示例代碼
更新時間:2022年07月11日 15:22:55 作者:繁華的夢境
這篇文章主要為大家詳細介紹了如何利用C++語言實現(xiàn)日期類(Date),可以實現(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); // 全缺省的構造函數(shù) Date(int year = 0, int month = 1, int day = 1); void Print(); //析構,拷貝構造,賦值重載函數(shù)可以不用寫,默認生成的就夠用 //日期+=天數(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--(); // >運算符重載 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); // !=運算符重載 bool operator != (const Date& d); // 日期-日期 返回天數(shù) int operator-(const Date& d); private: int _year; int _month; int _day; };
確定某年某月有多少天
//因為閏年和平年二月份的天數(shù)不一樣,隨便加加減減會有問題,這個函數(shù)可以根據(jù)閏年和平年給出對應的天數(shù) //因為后面需要頻繁調用它,設置成內聯(lián)函數(shù) inline int Date::GetMonthDay(int year, int month) { //設置成靜態(tài)變量,不用每次調用這個函數(shù)都開辟一個數(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; }
構造函數(shù)
Date::Date(int year, int month, int day) { //防止給的日期有錯誤 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ù)大于月份對應的最大天數(shù),就先減去這個月天數(shù),然后月份++,然后再判斷月份對應的天數(shù)對不對,往復循環(huán) while (_day > GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); ++_month; if (_month > 12) { ++_year; _month = 1; } } //如果是傳值返回的話傳的是他的一個拷貝臨時變量,又需要調用拷貝構造函數(shù),因為出了函數(shù)作用域對象還在,所以傳引用更好。 } return *this; }
日期+天數(shù)
Date Date::operator+(int day) { Date ret(*this); //復用operator+=函數(shù) ret += day; //ret是一個局部變量,出了作用域就銷毀了,傳不了引用 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; }
前置++
//返回的是++后的值,可以復用+=函數(shù),邏輯是一樣的,把day換成1即可 Date& Date::operator++() { *this += 1; return *this; }
后置++
//返回的是++前的值,不能傳引用返回 //為了和前置++賦值重載函數(shù)構成函數(shù)重載,需要加個int ,不需要傳實參,因為沒用,如果不加參數(shù)那么前置++和后置++函數(shù)就一樣了,們在進行前置++或者后置++操作時,編譯器就不知道調用哪一個了 Date Date::operator++(int) { Date ret(*this); *this += 1; return ret; }
后置–
//返回原對象,然后對象再-- Date Date::operator--(int) { Date ret = *this; *this -= 1; return ret; }
前置–
返回--后的對象
Date& Date::operator--() { *this -= 1; return *this; }
>運算符重載
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; }
==運算符重載
bool Date::operator==(const Date& d) { return _year == d._year && _month == d._month && _day == d._day; }
>=運算符重載
bool Date::operator >= (const Date& d) { return *this > d || *this == d; }
<運算符重載
bool Date::operator < (const Date& d) { return !(*this >= d); }
<=運算符重載
bool Date::operator <= (const Date& d) { return !(*this > d); }
!=運算符重載
bool Date::operator != (const Date& d) { return !(*this == d); }
計算兩個日期之間的間隔天數(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; }
到此這篇關于C++日期類(Date)實現(xiàn)的示例代碼的文章就介紹到這了,更多相關C++日期類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C/C++ 運用Npcap發(fā)送UDP數(shù)據(jù)包的完美過程
UDP 是一種無連接、輕量級的傳輸層協(xié)議,與 TCP 相比,它不提供可靠性、流控制和錯誤恢復機制,但卻更加簡單且具有較低的開銷,這篇文章主要介紹了C/C++ 運用Npcap發(fā)送UDP數(shù)據(jù)包,需要的朋友可以參考下2023-11-11C++實現(xiàn)數(shù)字轉換為十六進制字符串的方法
這篇文章主要介紹了C++實現(xiàn)數(shù)字轉換為十六進制字符串的方法,涉及C++操作數(shù)字與字符串轉換的相關技巧,需要的朋友可以參考下2015-06-06Qt6遠程連接MySQL數(shù)據(jù)庫的簡單易上手版
在Qt應用程序里,可實現(xiàn)遠程MySQL服務器的連接操作,本文就來介紹一下Qt6遠程連接MySQL數(shù)據(jù)庫,具有一定的參考價值,感興趣的可以了解一下2023-11-11C++ LeeCode題目:比特位計數(shù)和買賣股票的最佳時機
這篇文章主要介紹了基于C語言計算比特位計數(shù)和買賣股票的最佳時機,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2021-07-07