C++實現(xiàn)日期類的方法詳解
一、日期類的實現(xiàn)
1.1日期類功能
頭文件中是我們要實現(xiàn)日期類功能的函數(shù)聲明。這里我們要注意拷貝函數(shù),只能在函數(shù)聲明時寫缺省值,防止我們在聲明和定義是給的缺省值不一樣。
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1);
void Print();
int GetMonthDay(int year, int month);
bool operator==(const Date& y);
bool operator!=(const Date& y);
bool operator>(const Date& y);
bool operator<(const Date& y);
bool operator>=(const Date& y);
bool operator<=(const Date& y);
int 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--();
Date operator--(int);
private:
int _year;
int _month;
int _day;
};1.2拷貝日期
有時輸入的日期可能是非法的,例如:月份大于12,日期大于31,還有閏2月天數(shù)等。所以我們要對輸入的日期進行判斷,因為每個月的天數(shù)不同,所以要用到GetMonthDay函數(shù)。
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (_year < 1 ||
_month < 1 || _month>12 ||
_day < 1 || _day > GetMonthDay(_year, _month))
{
//assert(false);
Print();
cout << "日期非法" << endl;
}
}
int Date::GetMonthDay(int year, int month)
{
assert(year >= 1 && month >= 1 && month <= 12);
int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 };
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
return 29;
return monthArray[month];
}小Tips:我們先判斷是否是二月,在判斷是否是閏年可以提高效率。
1.3重載關(guān)系運算符
關(guān)系運算符有:<、>、==、<=、>=、!=,我們在寫實現(xiàn)這些功能的代碼時,會發(fā)現(xiàn)它們邏輯相同,會復(fù)制粘貼,但這樣的代碼看著十分冗余,我們可以通過復(fù)用來實現(xiàn),讓代碼更簡單。
重載==
bool Date::operator==(const Date& y)
{
return _year == y._year
&& _month == y._month
&& _day == y._day;
}重載!=
bool Date::operator!=(const Date& y)
{
return !(*this == y);
}重載<
bool Date::operator<(const Date& d)
{
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 Date::operator<=(const Date& y)
{
return *this < y || *this == y;
}重載>
bool Date::operator>(const Date& y)
{
return !(*this <= y);
}重載>=
bool Date::operator>=(const Date& y)
{
return !(*this < y);
}1.4重載+、+=
我們想知道50天之后的日期,就可以通過重載+、+=來實現(xiàn)。在加天數(shù)的時候,由于每個月的天數(shù)不一樣,所以進位就不同,我們要得到每個月份的天數(shù)。
獲得月份的天數(shù)
int Date::GetMonthDay(int year, int month)
{
assert(year >= 1 && month >= 1 && month <= 12);
int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 };
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
return 29;
return monthArray[month];
}小Tips:把month == 2放在前面判斷可以提高效率,如果不是二月就不需要判斷是否是閏年,如果是二月在判斷。
重載+=
Date& Date::operator+=(int day)
{
//天數(shù)為負(fù)數(shù)時,復(fù)用-=
if (day < 0)
{
return *this -= (-day);
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}重載+
Date Date::operator+(int day)
{
if(x < 0)
{
return *this - (-day);
}
Date tmp(*this);
tmp._day = _day + x;
while (tmp._day > GetDay(tmp._year, tmp._month))
{
tmp._day = tmp._day - GetDay(tmp._year, tmp._month);
tmp._month++;
if (tmp._month == 13)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}小Tips:重載+是原來的日期不會改變,所以我們拷貝構(gòu)造了一個和*this相同的對象,我們對tmp對象修改,不會改變*this。重載+=是在原來的日期上直接修改,所以我們直接對*this指向的日期進行修改,然后返回就可以。
為什么重載+=用引用返回?
在重載+=中,*this就是d1,它的作用域是函數(shù)結(jié)束后才銷毀,由于傳值返回會拷貝一份返回值,所以為了減少返回時的拷貝,所以使用引用返回。在重載+中,tmp出了operator+函數(shù)就被銷毀,所以只能使用傳值返回。
+和+=之間的復(fù)用
+復(fù)用+=
Date Date::operator+(int day)
{
Date tmp(*this);
tmp += day;
return tmp;
}1.5重載 -、-=
有時我們想知道以前的日期,日期-天數(shù)可以知道多少天以前的日期,日期-日期可以知道兩個日期直接隔了多少天。兩個operator-函數(shù)構(gòu)成了函數(shù)重載。
重載-=
Date& operator-=(int x)
{
//天數(shù)天數(shù)小于0,復(fù)用+=
if (x < 0)
{
return *this += -x;
}
_day -= x;
while (_day <= 0)
{
_month--;
if (_month == 0)
{
_month = 12;
_year--;
}
_day += GetDay(_year, _month);
}
return *this;
}重載日期-天數(shù)
Date Date::operator-(int x)
{
Date tmp(*this);
return tmp -= x;
}重載日期-日期
日期-日期,計算的結(jié)果是兩個日期之間的天數(shù),所以返回值是int,要知道兩個日期之間相隔的天數(shù),可以設(shè)置一個計數(shù)器,讓小日期一直加到大日期,就可以知道兩個日期之間相隔的天數(shù)。
int operator-(const Date& d)
{
Date max = *this;//存放大日期
Date min = d;//存放小日期
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (max != min)
{
--max;
++n;
}
return n * flag;
}1.6重載++、--
重載前置++
前置++要返回++之后的值
Date& Date::operator++()
{
*this += 1;//復(fù)用+=
return *this;
}重載前置++
后置++要返回++之前的值
Date Date::operator++(int)//編譯器會把有int的視為后置++
{
Date tmp(*this);
*this += 1;//復(fù)用+=
return tmp;
}小Tips:這兩個operator++函數(shù)構(gòu)成了函數(shù)重載,那調(diào)用的時候怎么區(qū)分前置++和后置++呢?后置重載的時候多增加一個int類型的參數(shù),使用后置++時,調(diào)用運算符重載函數(shù)時不用傳遞參數(shù),編譯器自動傳遞。
重載前置 --
前置--要返回--之后的值
Date& operator--()
{
*this -= 1;//復(fù)用-=
return *this;
}重載后置 --
后置--要返回--之前的值
Date operator--(int)
{
Date tmp(*this);
*this -= 1;//復(fù)用了-=
return tmp;
}結(jié)語:
到此這篇關(guān)于C++實現(xiàn)日期類的方法詳解的文章就介紹到這了,更多相關(guān)C++實現(xiàn)日期類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡單聊聊C++中回調(diào)函數(shù)的實現(xiàn)
回調(diào)函數(shù)就是一個通過函數(shù)指針調(diào)用的函數(shù),如果你把函數(shù)的指針(地址)作為參數(shù)傳遞給另一個函數(shù),當(dāng)這個指針被用來調(diào)用其所指向的函數(shù)時,我們就說這是回調(diào)函數(shù),下面這篇文章主要給大家介紹了關(guān)于C++中回調(diào)函數(shù)實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-01-01
利用C語言實現(xiàn)將格式化數(shù)據(jù)和字符串相互轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了2個函數(shù),分別是sprintf和sscanf,可以用來實現(xiàn)將格式化數(shù)據(jù)和字符串相互轉(zhuǎn)換,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
基于QT制作一個TCPServer與TCPClient的通信
這篇文章主要為大家詳細(xì)介紹了如何基于QT制作一個TCPServer與TCPClient的通信,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12

