網站首頁 編程語言 正文
構造函數
定義一個全缺省的構造函數,函數體內判斷當前日期是否合法,如果是非法日期,則初始化為2022,1,1
Date(int year=2022, int month=1, int day=1)
?? ??? ?:_year(year)
?? ??? ?, _month(month)
?? ??? ?, _day(day)
?? ?{
?? ??? ?if (year <1 || month < 1 || month>12 || day<1 || day>GetDayOfMonth(year,month))
?? ??? ?{
?? ??? ??? ?_year = 2022;
?? ??? ??? ?_month = 1;
?? ??? ??? ?_day = 1;
?? ??? ?}
?? ?}
拷貝構造
?? ?Date(const Date& d)
?? ?{
?? ??? ?_year = d._year;
?? ??? ?_month = d._month;
?? ??? ?_day = d._day;
?? ?}
賦值運算符重載
賦值運算符編譯器會默認生成,對于日期類可以不用自己定義,自己定義需要注意三點:
- 參數和返回值都是類類型的引用
- 判斷是否為自己給自己賦值,因為兩對象不能直接比較,所以判斷時用地址
- 為了支持連續賦值,必須返回*this
?? ?Date& operator=(const Date& d)
?? ?{
?? ??? ?//不能用對象直接比較,只能比較地址
?? ??? ?//if (d != (*this))
?? ??? ?if(&d!=this)
?? ??? ?{
?? ??? ??? ?_year = d._year;
?? ??? ??? ?_month = d._month;
?? ??? ??? ?_day = d._day;
?? ??? ?}
?? ??? ?return *this;
?? ?}
“+” 日期+天數
"+“與”+=“區分,”+"不改變原變量的值,只是通過返回值返回,所以這里要定義一個臨時變量temp,由于temp是保存在棧上的臨時變量,所以返回值為Date類型,不能返回引用
先判斷正負,如果加的數是負數,則復用"-"的重載
加完后判斷_day的合法性,如果_day非法,_day減去當前月份的天數,月份增加,直到日期合法
? ? //日期+天數
?? ?Date operator+(int day)
?? ?{
?? ??? ?//不能改變原有日期,所以要定義臨時變量
?? ??? ?//臨時變量不能返回引用
?? ??? ?if (day < 0)
?? ??? ?{
?? ??? ??? ?day = -day;
?? ??? ??? ?return *this - day;
?? ??? ?}
?? ??? ?Date temp=*this;
?? ??? ?temp._day += day;
?? ??? ?while (temp._day > GetDayOfMonth(_year, _month))
?? ??? ?{
?? ??? ??? ?temp._day -= GetDayOfMonth(temp._year,temp._month);
?? ??? ??? ?temp._month++;
?? ??? ??? ?if (temp._month > 12)
?? ??? ??? ?{
?? ??? ??? ??? ?temp._month = 1;
?? ??? ??? ??? ?temp._year++;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return temp;
?? ?}
“-” 日期-天數
與"+"同原理,如果_day減為0或者負數,先將月份下調一個月,再將日加上上調后月份的天數,如8月0日表示7月31,8月-1日,則表示7月30日。
?? ?//日期-天數
?? ?Date operator-(int day)
?? ?{
?? ??? ?if (day < 0)
?? ??? ?{
?? ??? ??? ?day = -day;
?? ??? ??? ?return *this + day;
?? ??? ?}
?? ??? ?Date temp = *this;
?? ??? ?temp._day -= day;
?? ??? ?while (temp._day <= 0)
?? ??? ?{
?? ??? ??? ?temp._month--;
?? ??? ??? ?if (temp._month < 1)
?? ??? ??? ?{
?? ??? ??? ??? ?temp._year--;
?? ??? ??? ??? ?temp._month = 12;
?? ??? ??? ?}
?? ??? ??? ?temp._day += GetDayOfMonth(temp._year ,temp._month);
?? ??? ?}
?? ??? ?return temp;
?? ?}
“+=” 日期+=天數
"+=“與”+“的區別是,加后要個自己賦值,所以返回類類型對象的引用,可以復用”=“和”+"的重載
?? ?//日期+=天數
?? ?Date& operator+=(int days)
?? ?{
?? ??? ?//復用=和+
?? ??? ?//這里要先將加的和賦值給*this,再返回
?? ??? ?//非常量左值引用只能引用非常量左值
?? ??? ?//如果要直接return(*this + days),返回值類型應為Date&&
?? ??? ?*this = *this + days;
?? ??? ?return * this;
?? ?}
“-=” 日期-=天數
?? ?//日期-=天數
?? ?Date& operator-=(int days)
?? ?{
?? ??? ?*this = *this - days;
?? ??? ?return *this;
?? ?}
前置"++"
前置"++"返回的是加一后的值,可以直接給*this加一后返回
?? ?//前置++
?? ?Date& operator++()
?? ?{
?? ??? ?*this += 1;
?? ??? ?return *this;
?? ?}
后置"++"
C++中,后置"++“參數加一個int,無實際意義,區分前置與后置”++“,由于后置”++"要返回的是自增之前的值,所以要定義一個臨時變量,自增后返回該臨時變量的值
?? ?//后置++
?? ?//
?? ?Date operator++(int)
?? ?{
?? ??? ?//返回的是自增之前的值
?? ??? ?Date temp = *this;
?? ??? ?*this += 1;
?? ??? ?return temp;
?? ?}
前置"–"
與前置"++"原理相同
?? ?//前置--
?? ?Date& operator--()
?? ?{
?? ??? ?*this -= 1;
?? ??? ?return *this;
?? ?}
后置"- -"
與后置"++"原理相同
?? ?//后置--
?? ?Date operator--(int)
?? ?{
?? ??? ?Date temp = *this;
?? ??? ?*this-= 1;
?? ??? ?return temp;
?? ?}
“==”
判斷每變量是否相等
?? ?//==
?? ?bool operator==(Date& d)const
?? ?{
?? ??? ?return d._year == _year &&
?? ??? ??? ?d._month == _month &&
?? ??? ??? ?d._day == _day;
?? ?}
“>”
從年到月日依次判斷
?? ?//>
?? ?bool operator>(Date& d)const
?? ?{
?? ??? ?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 operator<(Date& d)const
?? ?{
?? ??? ?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;
?? ??? ?}
?? ?}
“-” 日期-日期
定義一個計數器統計從小的日期到大的日期需要自增多少次
?? ?int operator-(const Date& d)const
?? ?{
?? ??? ?int count = 0;
?? ??? ?Date start(*this);
?? ??? ?Date end(d);
?? ??? ?//保證大的日期減小的日期
?? ??? ?if (start>end)
?? ??? ?{
?? ??? ??? ?Date temp = end;
?? ??? ??? ?end = start;
?? ??? ??? ?start = temp;
?? ??? ?}
?? ??? ?while(end>start)
?? ??? ?{
?? ??? ??? ?start++;
?? ??? ??? ?count++;
?? ??? ?}
?? ??? ?return count;
?? ?}
設置年、月、日
對外部提供修改對象值的接口
?? ?void SetYear(int year)
?? ?{
?? ??? ?_year = year;
?? ?}
?? ?void SetMonth(int month)
?? ?{
?? ??? ?_month = month;
?? ?}
?? ?void SetDay(int day)
?? ?{
?? ??? ?_day = day;
?? ?}
獲取年、月、日
向外部提供獲取類成員變量值的接口
?? ?int GetYear()const
?? ?{
?? ??? ?return _year;
?? ?}
?? ?int GetMonth()const
?? ?{
?? ??? ?return _month;
?? ?}
?? ?
?? ?int GetDay()const
?? ?{
?? ??? ?return _day;
?? ?}
判斷閏年
判斷閏年,可以定義為private,不用暴露給外部
?? ?bool IsLeapYear(int year)
?? ?{
?? ??? ?if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
?? ??? ?{
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
獲取當前月天數
定義一個數組保存每月的天數,根據傳進來的年和月返回當前月份的天數
?? ?//獲取當前月份的天數
?? ?int GetDayOfMonth(int year, int month)
?? ?{
?? ??? ?int arr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
?? ??? ?if (IsLeapYear(year))
?? ??? ?{
?? ??? ??? ?arr[2] = 29;
?? ??? ?}
?? ??? ?return arr[month];
?? ?}
日期類完整代碼
class Date
{
public:
?? ?//全缺省構造函數
?? ?Date(int year=2022, int month=2, int day=2)
?? ??? ?:_year(year)
?? ??? ?, _month(month)
?? ??? ?, _day(day)
?? ?{
?? ??? ?if (year <1 || month < 1 || month>12 || day<1 || day>GetDayOfMonth(year,month))
?? ??? ?{
?? ??? ??? ?_year = 2022;
?? ??? ??? ?_month = 1;
?? ??? ??? ?_day = 1;
?? ??? ?}
?? ?}
?? ?//拷貝構造
?? ?Date(const Date& d)
?? ?{
?? ??? ?_year = d._year;
?? ??? ?_month = d._month;
?? ??? ?_day = d._day;
?? ?}
?? ?//析構——無實際意義
?? ?~Date()
?? ?{
?? ??? ?cout << "~Date()" << endl;
?? ?}
?? ?//賦值運算符重載(不寫也可以,編譯器會默認生成)
?? ?Date& operator=(const Date& d)
?? ?{
?? ??? ?//不能用對象直接比較,只能比較地址
?? ??? ?//if (d != (*this))
?? ??? ?if(&d!=this)
?? ??? ?{
?? ??? ??? ?_year = d._year;
?? ??? ??? ?_month = d._month;
?? ??? ??? ?_day = d._day;
?? ??? ?}
?? ??? ?return *this;
?? ?}
?? ?//日期+天數
?? ?Date operator+(int day)
?? ?{
?? ??? ?//不能改變原有日期,所以要定義臨時變量
?? ??? ?//臨時變量不能返回引用
?? ??? ?if (day < 0)
?? ??? ?{
?? ??? ??? ?day = -day;
?? ??? ??? ?return *this - day;
?? ??? ?}
?? ??? ?Date temp=*this;
?? ??? ?temp._day += day;
?? ??? ?while (temp._day > GetDayOfMonth(_year, _month))
?? ??? ?{
?? ??? ??? ?temp._day -= GetDayOfMonth(temp._year,temp._month);
?? ??? ??? ?temp._month++;
?? ??? ??? ?if (temp._month > 12)
?? ??? ??? ?{
?? ??? ??? ??? ?temp._month = 1;
?? ??? ??? ??? ?temp._year++;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return temp;
?? ?}
?? ?//日期-天數
?? ?Date operator-(int day)
?? ?{
?? ??? ?if (day < 0)
?? ??? ?{
?? ??? ??? ?day = -day;
?? ??? ??? ?return *this + day;
?? ??? ?}
?? ??? ?Date temp = *this;
?? ??? ?temp._day -= day;
?? ??? ?while (temp._day <= 0)
?? ??? ?{
?? ??? ??? ?temp._month--;
?? ??? ??? ?if (temp._month < 1)
?? ??? ??? ?{
?? ??? ??? ??? ?temp._year--;
?? ??? ??? ??? ?temp._month = 12;
?? ??? ??? ?}
?? ??? ??? ?temp._day += GetDayOfMonth(temp._year ,temp._month);
?? ??? ?}
?? ??? ?return temp;
?? ?}
?? ?//日期+=天數
?? ?Date& operator+=(int days)
?? ?{
?? ??? ?//復用=和+
?? ??? ?//這里要先將加的和賦值給*this,再返回
?? ??? ?//非常量左值引用只能引用非常量左值
?? ??? ?//如果要直接return(*this + days),返回值類型應為Date&&
?? ??? ?*this = *this + days;
?? ??? ?return * this;
?? ?}
?? ?//日期-=天數
?? ?Date& operator-=(int days)
?? ?{
?? ??? ?*this = *this - days;
?? ??? ?return *this;
?? ?}
?? ?//前置++
?? ?Date& operator++()
?? ?{
?? ??? ?*this += 1;
?? ??? ?return *this;
?? ?}
?? ?//后置++
?? ?//C++語法,后置++參數加一個int,無實際意義,區分前置與后置++
?? ?Date operator++(int)
?? ?{
?? ??? ?//返回的是自增之前的值
?? ??? ?Date temp = *this;
?? ??? ?*this += 1;
?? ??? ?return temp;
?? ?}
?? ?//前置--
?? ?Date& operator--()
?? ?{
?? ??? ?*this -= 1;
?? ??? ?return *this;
?? ?}
?? ?//后置--
?? ?Date operator--(int)
?? ?{
?? ??? ?Date temp = *this;
?? ??? ?*this-= 1;
?? ??? ?return temp;
?? ?}
?? ?//==
?? ?bool operator==(Date& d)const
?? ?{
?? ??? ?return d._year == _year &&
?? ??? ??? ?d._month == _month &&
?? ??? ??? ?d._day == _day;
?? ?}
?? ?//>
?? ?bool operator>(Date& d)const
?? ?{
?? ??? ?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 operator<(Date& d)const
?? ?{
?? ??? ?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;
?? ??? ?}
?? ?}
?? ?//日期-日期 即還有多少天到某一天?
?? ?int operator-(const Date& d)const
?? ?{
?? ??? ?int count = 0;
?? ??? ?Date start(*this);
?? ??? ?Date end(d);
?? ??? ?//保證大的日期減小的日期
?? ??? ?if (start>end)
?? ??? ?{
?? ??? ??? ?Date temp = end;
?? ??? ??? ?end = start;
?? ??? ??? ?start = temp;
?? ??? ?}
?? ??? ?while(end>start)
?? ??? ?{
?? ??? ??? ?start++;
?? ??? ??? ?count++;
?? ??? ?}
?? ??? ?return count;
?? ?}
?? ?//設置年、月、日接口
?? ?void SetYear(int year)
?? ?{
?? ??? ?_year = year;
?? ?}
?? ?void SetMonth(int month)
?? ?{
?? ??? ?_month = month;
?? ?}
?? ?void SetDay(int day)
?? ?{
?? ??? ?_day = day;
?? ?}
?? ?//獲取年、月、日接口
?? ?int GetYear()const
?? ?{
?? ??? ?return _year;
?? ?}
?? ?int GetMonth()const
?? ?{
?? ??? ?return _month;
?? ?}
?? ?
?? ?int GetDay()const
?? ?{
?? ??? ?return _day;
?? ?}
private:
?? ?int _year;
?? ?int _month;
?? ?int _day;
?? ?//判斷閏年
?? ?bool IsLeapYear(int year)
?? ?{
?? ??? ?if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
?? ??? ?{
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
?? ?//獲取當前月份的天數
?? ?int GetDayOfMonth(int year, int month)
?? ?{
?? ??? ?int arr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
?? ??? ?if (IsLeapYear(year))
?? ??? ?{
?? ??? ??? ?arr[2] = 29;
?? ??? ?}
?? ??? ?return arr[month];
?? ?}
};
總結
原文鏈接:https://blog.csdn.net/qq_44631587/article/details/126084430
相關推薦
- 2023-03-23 Android進階CoordinatorLayout協調者布局實現吸頂效果_Android
- 2022-05-17 Mybatis中報錯:attempted to return null from a method
- 2022-09-17 使用cache加快編譯速度的命令詳解_相關技巧
- 2022-11-06 Git?Commitizen提交規范化自動生成changelog文件_相關技巧
- 2022-12-11 Go?模塊在下游服務抖動恢復后CPU占用無法恢復原因_Golang
- 2022-10-02 C語言數組越界引發的死循環問題解決_C 語言
- 2022-04-04 小程序Arrow function should not return assignment
- 2022-06-18 使用OpenGL創建窗口的示例詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支