日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++日期類運算符重載方式_C 語言

作者:安河橋畔 ? 更新時間: 2023-04-07 編程語言

構造函數

定義一個全缺省的構造函數,函數體內判斷當前日期是否合法,如果是非法日期,則初始化為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

欄目分類
最近更新