欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

在實現(xiàn)前,我們要先把類寫好,類中包含成員函數(shù)和成員變量。
對于日期類來說,拷貝構(gòu)造和賦值運算符可以不寫,但是我在類中寫了,也沒關(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;
 }
//賦值運算符
 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. 日期類的實現(xiàn)

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

2.1 日期+=天數(shù)

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

詳細(xì)請看下面:

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ù),自己沒變,所以要創(chuàng)個臨時變量。

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 前置++

對于前置和后置,在函數(shù)命名的時候,后置的參數(shù)列表多一個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 >運算符重載

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 ==運算符重載

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

}

2.11 >=運算符重載

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

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

2.12 <運算符重載

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

}

2.13 <=運算符重載

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

2.14 !=運算符重載

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++類與對象之日期類的實現(xiàn)的文章就介紹到這了,更多相關(guān)C++日期類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論