最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++類與對(duì)象之日期類的實(shí)現(xiàn)

 更新時(shí)間:2021年09月09日 09:49:24   作者:世_生  
這篇文章主要介紹如何實(shí)現(xiàn)C++中的日期類相關(guān)資料,需要的朋友可以參考下面文章的具體內(nèi)容

1. 實(shí)現(xiàn)前的鋪墊

在實(shí)現(xiàn)前,我們要先把類寫好,類中包含成員函數(shù)和成員變量。
對(duì)于日期類來說,拷貝構(gòu)造和賦值運(yùn)算符可以不寫,但是我在類中寫了,也沒關(guān)系哦。

#include<iostream>

using std::cout;
using std::endl;
using std::cin;

class Date
{

public:
//構(gòu)造函數(shù)
 Date(int year = 0, int month = 1, int day = 1)
 {
  if (year >= 0 && month > 0 && month<13 && day>0 && day <= GetMonthDay(year, month))
  {
   _year = year;
   _month = month;
   _day = day;
  }
  else
  {
   cout << "輸入日期不和法" << endl;
  }
 }
//拷貝構(gòu)造
 Date(const Date& d)
 {
  _year = d._year;
  _month = d._month;
  _day = d._day;
 }
//賦值運(yùn)算符
 Date& operator=(const Date&d)
 {
  if (this != &d)
  {
   _year = d._year;
   _month = d._month;
   _day = d._day;
  }
  return *this;
 }
 //判斷合法性
 int GetMonthDay(int year, int month);
 //輸出日期
 void prit();
 //日期+=
 Date& operator+=(int day);
 //日期+
 Date operator+(int day);
 //日期-=
 Date& operator-=(int day);
 //日期-
 Date operator-(int day);
 //日期==
 bool operator==(const Date& d);
 //日期>
 bool operator>(const Date& d);
 //日期!=
 bool operator != (const Date& d);
 //<
 bool operator < (const Date& d);
 //>=
 inline bool operator >= (const Date& d);
 //<=
 bool operator <= (const Date& d);
 //++d
 Date& operator++();
 //d++
 Date& operator++(int);
 //--d
 Date& operator--();
 //d--
 Date& operator--(int);
 //日期-日期
 int operator-(const Date& d);

private:
 int _year;
 int _month;
 int _day;
};
//給出當(dāng)年當(dāng)月的天數(shù)
inline int Date::GetMonthDay(int year, int month)
{
 static int dayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 dayArray[2] = 28;
 if (month == 2 && ((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0)
 {
  dayArray[2] = 29;
 }
 return dayArray[month];
}

2. 日期類的實(shí)現(xiàn)

在實(shí)現(xiàn)前我們先要利用復(fù)用,復(fù)用就是把已經(jīng)寫好的函數(shù)來完成要完成的函數(shù)。

2.1 日期+=天數(shù)

在這里,就復(fù)用了(日期-=天數(shù)的)函數(shù)
對(duì)于+=,當(dāng)加一個(gè)天數(shù)是為正的時(shí)候(如:100),
但當(dāng)加的天數(shù)為負(fù)數(shù)時(shí)(如:-100)
這里就要先判斷day是否為正負(fù)數(shù)
如果為正數(shù)就正常加,如果是負(fù)數(shù)就復(fù)用-=操作符重載函數(shù)

詳細(xì)請(qǐng)看下面:

Date& Date::operator+=(int day)
{
 if (day > 0)
 {
  _day += day;
  while (_day > GetMonthDay(_year, _month))
  {
   _day -= GetMonthDay(_year, _month);
   _month++;
   if (_month > 12)
   {
    _year++;
    _month = 1;
   }
  }
 }
 else
 {
  *this -= (-day);
 }

 return *this;
}

2.2 日期-=天數(shù)

這里也和+=操作符重載函數(shù)一樣,都要判斷day是否為正負(fù)值

Date& Date::operator-=(int day)
{
 if (day > 0)
 {
  _day -= day;
  while (_day <= 0)
  {
   _month--;
   _day += GetMonthDay(_year, _month);
   if (_month == 0)
   {
    _year--;
    _month = 12;
   }
  }
 }
 else
 {
  *this += (-day);
 }
 return *this;
}

2.3 日期-天數(shù)

減天數(shù),自己沒變,所以要?jiǎng)?chuàng)個(gè)臨時(shí)變量。

Date Date::operator-(int day)
{
 Date ret(*this);
 ret -= day;
 return *ret;
}

2.4 日期+天數(shù)

與減天數(shù)同理

Date Date::operator+(int day)
{
 Date ret(*this);
 ret += day;
 return ret;
}

2.5 前置++

對(duì)于前置和后置,在函數(shù)命名的時(shí)候,后置的參數(shù)列表多一個(gè)int來占位。

Date& Date::operator++()
{
 *this += 1;
 return *this;
}

2.6 后置++

Date& Date::operator++(int)
{
 Date tmp(*this);
 tmp += 1;
 return *this;
}

2.7 前置–

Date& Date::operator--()
{
 *this -= 1;
 return *this;
}

2.8 后置–

Date& Date::operator--(int)
{
 Date tmp(*this);
 tmp -= 1;
 return *this;

2.9 >運(yùn)算符重載

bool Date::operator>(const Date& d)
{
 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;
}

2.10 ==運(yùn)算符重載

bool Date::operator==(const Date& d)
{
 return _year == d._year
  &&_month == d._month
  &&_day == d._day;

}

2.11 >=運(yùn)算符重載

這里就開始復(fù)用>和==的操作符重載函數(shù)

bool Date::operator >= (const Date& d)
{
 return (*this > d) || (*this == d);
}

2.12 <運(yùn)算符重載

bool Date::operator < (const Date& d)
{
 return !(*this>d);

}

2.13 <=運(yùn)算符重載

bool Date::operator <= (const Date& d)
{
 return (*this < d) || (*this == d);
}

2.14 !=運(yùn)算符重載

bool Date::operator != (const Date& d)
{
 return !(*this == d );
}

2.15 日期-日期 返回天數(shù)

int Date::operator-(const Date& d)
{
 Date max = *this;
 Date min = d;
 int flag = 1;
 if (max < min)
 {
  max = d;
  min = *this;
  flag = -1;
 }
 int m = 0;
 while (max!=min)
 {
  ++min;
  ++m;
 }

 return m*flag;
}

2.16 輸出

void Date::prit()
{
 cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

到此這篇關(guān)于C++類與對(duì)象之日期類的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C++日期類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用UDP協(xié)議實(shí)現(xiàn)單詞翻譯服務(wù)器

    使用UDP協(xié)議實(shí)現(xiàn)單詞翻譯服務(wù)器

    這篇文章主要為大家詳細(xì)介紹了如何使用UDP協(xié)議實(shí)現(xiàn)英文單詞翻譯服務(wù)器,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下
    2023-08-08
  • C語(yǔ)言編程中的聯(lián)合體union入門學(xué)習(xí)教程

    C語(yǔ)言編程中的聯(lián)合體union入門學(xué)習(xí)教程

    這篇文章主要介紹了C語(yǔ)言編程中的聯(lián)合體union入門學(xué)習(xí)教程,也是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-12-12
  • QT已有項(xiàng)目導(dǎo)入工程時(shí)注意事項(xiàng)圖文詳解

    QT已有項(xiàng)目導(dǎo)入工程時(shí)注意事項(xiàng)圖文詳解

    QT開發(fā)這幾年大大小小項(xiàng)目做了不少,花了點(diǎn)時(shí)間對(duì)知識(shí)點(diǎn)總結(jié)整合了一部分,下面這篇文章主要給大家介紹了關(guān)于QT已有項(xiàng)目導(dǎo)入工程時(shí)注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • C語(yǔ)言如何寫類實(shí)現(xiàn)教程示例

    C語(yǔ)言如何寫類實(shí)現(xiàn)教程示例

    這篇文章主要為大家介紹了C語(yǔ)言如何寫類的實(shí)現(xiàn)教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Qt如何自定義滑動(dòng)條

    Qt如何自定義滑動(dòng)條

    這篇文章主要介紹了Qt如何自定義滑動(dòng)條問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C++用兩個(gè)棧實(shí)現(xiàn)一個(gè)隊(duì)列(面試官的小結(jié))

    C++用兩個(gè)棧實(shí)現(xiàn)一個(gè)隊(duì)列(面試官的小結(jié))

    這篇文章主要給大家介紹了關(guān)于C++用兩個(gè)棧實(shí)現(xiàn)一個(gè)隊(duì)列的相關(guān)資料,這是來自一名面試官的小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • C語(yǔ)言文件操作 fopen, fclose, mkdir詳解

    C語(yǔ)言文件操作 fopen, fclose, mkdir詳解

    本文給大家詳細(xì)介紹了下C語(yǔ)言的文件操作函數(shù)fopen, fclose, mkdir的用法及示例,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。
    2016-03-03
  • C語(yǔ)言中魔性的float浮點(diǎn)數(shù)精度問題

    C語(yǔ)言中魔性的float浮點(diǎn)數(shù)精度問題

    這篇文章主要介紹了魔性的float浮點(diǎn)數(shù)精度問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C++入門之vector的底層實(shí)現(xiàn)詳解

    C++入門之vector的底層實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了C++入門之vector的底層實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • C語(yǔ)言行優(yōu)先和列優(yōu)先的問題深入分析

    C語(yǔ)言行優(yōu)先和列優(yōu)先的問題深入分析

    這篇文章主要介紹了C語(yǔ)言行優(yōu)先和列優(yōu)先的問題深入分析的相關(guān)資料,需要的朋友可以參考下
    2017-01-01

最新評(píng)論

丘北县| 巴彦淖尔市| 东光县| 红原县| 金寨县| 肇东市| 儋州市| 改则县| 正蓝旗| 阳城县| 临沭县| 彭州市| 尉犁县| 东丽区| 仁布县| 平湖市| 岳阳市| 辰溪县| 襄垣县| 惠安县| 松原市| 称多县| 子长县| 新干县| 宁德市| 红原县| 汨罗市| 商河县| 蒙山县| 乌兰浩特市| 阿鲁科尔沁旗| 钟祥市| 乌兰浩特市| 大港区| 嘉义县| 弋阳县| 自贡市| 延长县| 社会| 汾阳市| 酒泉市|