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