網(wǎng)站首頁 編程語言 正文
零、前言
在學(xué)了C++類和對(duì)象基本知識(shí)以及六個(gè)默認(rèn)成員函數(shù)后,我們可以上手實(shí)現(xiàn)一個(gè)Date類出來,檢驗(yàn)學(xué)習(xí)的效果。
一、Date類相關(guān)接口
接口展示:
class Date { //輸出操作符重載 friend ostream& operator<<(ostream& _cout, const Date& d); //輸出操作符重載 friend istream& operator>>(istream& _cin, Date& d); public: // 獲取某年某月的天數(shù) int GetMonthDay(int year, int month); // 全缺省的構(gòu)造函數(shù) Date(int year=1988, int month=1, int day=1); // 拷貝構(gòu)造函數(shù) Date(const Date& d); // 賦值運(yùn)算符重載 Date& operator=(const Date& d); // 日期+=天數(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); // 日期-日期 返回兩個(gè)日期之間相隔的具體天數(shù) int operator-(const Date& d); //日期展示 void print() { cout << _year << " " << _month << " " << _day << endl; } private: int _year; int _month; int _day; };
二、具體接口函數(shù)實(shí)現(xiàn)
注意:
因?yàn)閷?duì)于定義在類里面的函數(shù)會(huì)自動(dòng)設(shè)成內(nèi)聯(lián)函數(shù),而只有一些簡(jiǎn)單的函數(shù)才建議設(shè)成內(nèi)聯(lián)函數(shù),所以實(shí)現(xiàn)函數(shù)時(shí)我們是聲明和定義分離(在類里面聲明,類外定義)
在類外實(shí)現(xiàn)函數(shù)接口需要加上類域名稱
1、獲取月份天數(shù)
注意:
閏年二月與平年二月的天數(shù)不同
實(shí)現(xiàn)代碼:
//獲取月份天數(shù) int Date::GetMonthDay(int year, int month) { //設(shè)置平年月天數(shù)數(shù)組 static int monthdays[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//設(shè)置成靜態(tài)避免重復(fù)創(chuàng)建 int day = monthdays[month]; //對(duì)閏年二月的處理 if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) { day = 29; } return day; }
2、Date打印
注:打印函數(shù)比較簡(jiǎn)單,設(shè)成內(nèi)聯(lián)函數(shù)很適合,可以直接在類里定義
實(shí)現(xiàn)代碼:
void Date::Print() { cout << _year << "年" << _month << "月" << _day << "日" << endl; }
3、Date構(gòu)造函數(shù)
注意:
對(duì)于構(gòu)造函數(shù)建議寫成全缺省函數(shù)(便于無參數(shù)初始化),但是只能定義和聲明其中一個(gè)寫缺省
考慮初始化的日期是否合理
實(shí)現(xiàn)代碼:
//構(gòu)造函數(shù) //類里聲明 Date(int year = 0, int month = 1, int day = 1); //定義 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 { // 嚴(yán)格來說拋異常更好 cout << "非法日期" << endl; cout << year << "年" << month << "月" << day << "日" << endl; exit(-1); } }
4、Date析構(gòu)函數(shù)
注:對(duì)于像Date一樣的類來說,析構(gòu)函數(shù)(沒有需要清理的空間資源),拷貝函數(shù)和賦值重載函數(shù)(能夠完成成員變量淺拷貝)都不用自己寫,編譯器默認(rèn)生成的已經(jīng)足夠使用
實(shí)現(xiàn)代碼:
//析構(gòu)函數(shù) Date::~Date() { _year = 1; _month = 0; _day = 0; }
5、Date拷貝構(gòu)造函數(shù)
實(shí)現(xiàn)代碼:
//拷貝構(gòu)造 Date::Date(const Date& d) { _year = d._year; _month = d._month; _day= d._day; }
6、Date賦值重載函數(shù)
注意:
對(duì)于賦值操作符來說,是需要能支持連續(xù)賦值的操作,這里我們返回Date本身來進(jìn)行接下來的繼續(xù)賦值
實(shí)現(xiàn)代碼:
//賦值運(yùn)算符重載 Date& Date::operator=(const Date& d) { _year = d._year; _month = d._month; _day = d._day; return *this; }
效果圖:
7、Date+=天數(shù)
注意:
- +=表示會(huì)修改Date本身的數(shù)據(jù)
- 處理傳入負(fù)數(shù)天數(shù)
- 處理好天數(shù)進(jìn)位,月份進(jìn)位
實(shí)現(xiàn)代碼:
//日期+=天數(shù) Date& Date::operator+=(int day) { if (day < 0)//處理特殊情況 { *this -= -day;//復(fù)用Date-=天數(shù) } else { _day += day; while (_day > GetMonthDay(_year, _month))//處理數(shù)據(jù)合理性 { _day -= GetMonthDay(_year, _month); _month++; if (_month > 12) { _year++; _month = 1; } } } return *this;//返回引用,即對(duì)象本身 }
8、Date+天數(shù)
注意:
+天數(shù)表示不會(huì)修改Date本身的數(shù)據(jù)(使用const修飾,避免修改)
邏輯與Date+=天數(shù)基本一致,可以進(jìn)行復(fù)用
實(shí)現(xiàn)代碼:
Date Date::operator+(int day) const { Date tmp = *this;//賦值重載 tmp += day;//復(fù)用+=重載 return tmp;//返回值(拷貝構(gòu)造) }
9、Date-=天數(shù)
注意:
- +=表示會(huì)修改Date本身的數(shù)據(jù)
- 處理傳入負(fù)數(shù)天數(shù)
- 考慮日期的借位,月份的借位
實(shí)現(xiàn)代碼:
//日期-=天數(shù) Date& Date::operator-=(int day) { if (day < 0) { *this += -day;//復(fù)用Date+=天數(shù) } else { _day -= day; while (_day <= 0)//處理數(shù)據(jù)合理性 { _month--; if (_month <= 0) { _year--; _month = 12; } _day += GetMonthDay(_year, _month); } } return *this; }
10、Date-天數(shù)
注意:
- -天數(shù)不會(huì)修改Date本身的數(shù)據(jù)(使用const修飾,避免修改)
- 邏輯與Date-=天數(shù)基本一致,可以進(jìn)行復(fù)用
實(shí)現(xiàn)代碼:
Date Date::operator-(int day) const { Date tmp = *this; tmp -= day; return tmp; }
11、++Date
注意:
前置++表示,Date先增后使用
實(shí)現(xiàn)代碼:
//++Date Date& Date::operator++() { *this += 1;//復(fù)用Date+=天數(shù) return *this; }
12、Date++
注意:
語法規(guī)定,因?yàn)榕c前置命名相同的緣故,這里的后置函數(shù)多一個(gè)參數(shù)來與前置函數(shù)形成重載
后置++表示先使用后自增
實(shí)現(xiàn)代碼:
//Date++ Date Date::operator++(int) { Date tmp = *this;//保存一份日期 *this += 1;//自增當(dāng)前日期 return tmp;//返回自增前的日期 }
13、–Date
實(shí)現(xiàn)代碼:
//--Date Date& Date::operator--() { *this -= 1; return *this; }
14、Date–
實(shí)現(xiàn)代碼:
//Date-- Date Date::operator--(int) { Date tmp = *this; *this -= 1; return tmp; }
15、日期比較
注:可以多次復(fù)用
實(shí)現(xiàn)代碼:
//日期比較 bool Date::operator>(const Date& d) const { if (_year > d._year) { return true; } else if(_year == d._year) { if (_month > d._month) { return true; } else if(_month == d._month) { if (_day > d._day) { return true; } } }> return false; } bool Date::operator==(const Date& d) const { return _year == d._year && _month == d._month && _day == d._day; } bool Date::operator<(const Date& d) const { return !(*this >= d); } bool Date::operator>=(const Date& d) const { return *this > d || *this == d; } bool Date::operator<=(const Date& d) const { return !(*this > d); } bool Date::operator!=(const Date& d) const { return !(*this == d); }
16、Date相減
實(shí)現(xiàn)代碼:
//日期減日期 int Date::operator-(const Date& d) const { //確定日期的大小 Date max = *this; Date min = d; if (*this < d)//復(fù)用日期比較 { max = d; min = *this; } int day = 0; while (max != min) { ++min; ++day; } return day; }
17、日期輸入\日期輸出
注意:
- 對(duì)于輸入操作符,我們習(xí)慣是cin>>date,而這樣的用法表示做操作數(shù)是cin,右操作數(shù)為日期對(duì)象,但是對(duì)于類成員函數(shù)來說,存在著隱含參數(shù)this指針(占據(jù)和第一個(gè)參數(shù)位置,即日期對(duì)象是左操作數(shù))
- 雖然定義成類外函數(shù)能修改參數(shù)位置,但是無法訪問類里的私有成員變量,這里我們使用友元函數(shù)來解決,即在類里聲明函數(shù)前加上friend,便可以訪問成員
實(shí)現(xiàn)代碼:
//輸出操作符重載 ostream& operator<<(ostream& _cout, const Date& d) { _cout << d._year << "年" << d._month << "月" << d._day << "日" ; return _cout; } //輸出操作符重載 istream& operator>>(istream& _cin, Date& d) > { _cin >> d._year >> d._month >> d._day; return _cin; }
效果圖:
- date,而這樣的用法表示做操作數(shù)是cin,右操作數(shù)為日期對(duì)象,但是對(duì)于類成員函數(shù)來說,存在著隱含參數(shù)this指針(占據(jù)和第一個(gè)參數(shù)位置,即日期對(duì)象是左操作數(shù))
- 雖然定義成類外函數(shù)能修改參數(shù)位置,但是無法訪問類里的私有成員變量,這里我們使用友元函數(shù)來解決,即在類里聲明函數(shù)前加上friend,便可以訪問成員
實(shí)現(xiàn)代碼:
//輸出操作符重載 ostream& operator<<(ostream& _cout, const Date& d) { _cout << d._year << "年" << d._month << "月" << d._day << "日" ; return _cout; } //輸出操作符重載 istream& operator>>(istream& _cin, Date& d) { _cin >> d._year >> d._month >> d._day; return _cin; }
效果圖:
總結(jié)
原文鏈接:https://blog.csdn.net/CS_z_jun/article/details/121878915
相關(guān)推薦
- 2022-04-12 python?獲取list?長(zhǎng)度_python
- 2024-03-09 【Redis】Redis中的布隆過濾器
- 2022-09-30 python語言中pandas字符串分割str.split()函數(shù)_python
- 2023-01-31 MongoDB?聚合查詢?cè)斀鈅MongoDB
- 2022-04-17 小程序中元素滾動(dòng)到元素的底部, 一直保持在底部
- 2022-06-15 GO語言字符串處理Strings包的函數(shù)使用示例講解_Golang
- 2022-05-10 MAC m1使用homebrew安裝redis報(bào)錯(cuò)
- 2022-10-25 記一次生產(chǎn)環(huán)境死鎖問題分析
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支