C++類和對(duì)象實(shí)戰(zhàn)之Date類的實(shí)現(xiàn)方法
零、前言
在學(xué)了C++類和對(duì)象基本知識(shí)以及六個(gè)默認(rèn)成員函數(shù)后,我們可以上手實(shí)現(xiàn)一個(gè)Date類出來(lái),檢驗(yàn)學(xué)習(xí)的效果。
一、Date類相關(guān)接口
接口展示:
class Date
{
//輸出操作符重載
friend ostream& operator<<(ostream& _cout, const Date& d);
//輸出操作符重載
friend istream& operator>>(istream& _cin, Date& d);
public:
// 獲取某年某月的天數(shù)
int GetMonthDay(int year, int month);
// 全缺省的構(gòu)造函數(shù)
Date(int year=1988, int month=1, int day=1);
// 拷貝構(gòu)造函數(shù)
Date(const Date& d);
// 賦值運(yùn)算符重載
Date& operator=(const Date& d);
// 日期+=天數(shù)
Date& operator+=(int day);
// 日期+天數(shù)
Date operator+(int day);
// 日期-天數(shù)
Date operator-(int day);
// 日期-=天數(shù)
Date& operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date& operator++(int);
// 后置--
Date& operator--(int);
// 前置--
Date& operator--();
// >運(yùn)算符重載
bool operator>(const Date& d);
// ==運(yùn)算符重載
bool operator==(const Date& d);
// >=運(yùn)算符重載
bool operator>=(const Date& d);
// <運(yùn)算符重載
bool operator<(const Date& d);
// <=運(yùn)算符重載
bool operator<=(const Date& d);
// !=運(yùn)算符重載
bool operator!=(const Date& d);
// 日期-日期 返回兩個(gè)日期之間相隔的具體天數(shù)
int operator-(const Date& d);
//日期展示
void print()
{
cout << _year << " " << _month << " " << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
二、具體接口函數(shù)實(shí)現(xiàn)
注意:
因?yàn)閷?duì)于定義在類里面的函數(shù)會(huì)自動(dòng)設(shè)成內(nèi)聯(lián)函數(shù),而只有一些簡(jiǎn)單的函數(shù)才建議設(shè)成內(nèi)聯(lián)函數(shù),所以實(shí)現(xiàn)函數(shù)時(shí)我們是聲明和定義分離(在類里面聲明,類外定義)
在類外實(shí)現(xiàn)函數(shù)接口需要加上類域名稱
1、獲取月份天數(shù)
注意:
閏年二月與平年二月的天數(shù)不同
實(shí)現(xiàn)代碼:
//獲取月份天數(shù)
int Date::GetMonthDay(int year, int month)
{
//設(shè)置平年月天數(shù)數(shù)組
static int monthdays[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//設(shè)置成靜態(tài)避免重復(fù)創(chuàng)建
int day = monthdays[month];
//對(duì)閏年二月的處理
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
day = 29;
}
return day;
}
2、Date打印
注:打印函數(shù)比較簡(jiǎn)單,設(shè)成內(nèi)聯(lián)函數(shù)很適合,可以直接在類里定義
實(shí)現(xiàn)代碼:
void Date::Print()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
3、Date構(gòu)造函數(shù)
注意:
對(duì)于構(gòu)造函數(shù)建議寫成全缺省函數(shù)(便于無(wú)參數(shù)初始化),但是只能定義和聲明其中一個(gè)寫缺省
考慮初始化的日期是否合理
實(shí)現(xiàn)代碼:
//構(gòu)造函數(shù)
//類里聲明
Date(int year = 0, int month = 1, int day = 1);
//定義
Date::Date(int year, int month, int day)
{
// 檢查日期的合法性
if (year >= 0
&& month > 0 && month < 13
&& day > 0 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
// 嚴(yán)格來(lái)說(shuō)拋異常更好
cout << "非法日期" << endl;
cout << year << "年" << month << "月" << day << "日" << endl;
exit(-1);
}
}
4、Date析構(gòu)函數(shù)
注:對(duì)于像Date一樣的類來(lái)說(shuō),析構(gòu)函數(shù)(沒(méi)有需要清理的空間資源),拷貝函數(shù)和賦值重載函數(shù)(能夠完成成員變量淺拷貝)都不用自己寫,編譯器默認(rèn)生成的已經(jīng)足夠使用
實(shí)現(xiàn)代碼:
//析構(gòu)函數(shù)
Date::~Date()
{
_year = 1;
_month = 0;
_day = 0;
}
5、Date拷貝構(gòu)造函數(shù)
實(shí)現(xiàn)代碼:
//拷貝構(gòu)造
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day= d._day;
}
6、Date賦值重載函數(shù)
注意:
對(duì)于賦值操作符來(lái)說(shuō),是需要能支持連續(xù)賦值的操作,這里我們返回Date本身來(lái)進(jìn)行接下來(lái)的繼續(xù)賦值
實(shí)現(xiàn)代碼:
//賦值運(yùn)算符重載
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
效果圖:

7、Date+=天數(shù)
注意:
- +=表示會(huì)修改Date本身的數(shù)據(jù)
- 處理傳入負(fù)數(shù)天數(shù)
- 處理好天數(shù)進(jìn)位,月份進(jìn)位
實(shí)現(xiàn)代碼:
//日期+=天數(shù)
Date& Date::operator+=(int day)
{
if (day < 0)//處理特殊情況
{
*this -= -day;//復(fù)用Date-=天數(shù)
}
else
{
_day += day;
while (_day > GetMonthDay(_year, _month))//處理數(shù)據(jù)合理性
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
}
return *this;//返回引用,即對(duì)象本身
}
8、Date+天數(shù)
注意:
+天數(shù)表示不會(huì)修改Date本身的數(shù)據(jù)(使用const修飾,避免修改)
邏輯與Date+=天數(shù)基本一致,可以進(jìn)行復(fù)用
實(shí)現(xiàn)代碼:
Date Date::operator+(int day) const
{
Date tmp = *this;//賦值重載
tmp += day;//復(fù)用+=重載
return tmp;//返回值(拷貝構(gòu)造)
}
9、Date-=天數(shù)
注意:
- +=表示會(huì)修改Date本身的數(shù)據(jù)
- 處理傳入負(fù)數(shù)天數(shù)
- 考慮日期的借位,月份的借位
實(shí)現(xiàn)代碼:
//日期-=天數(shù)
Date& Date::operator-=(int day)
{
if (day < 0)
{
*this += -day;//復(fù)用Date+=天數(shù)
}
else
{
_day -= day;
while (_day <= 0)//處理數(shù)據(jù)合理性
{
_month--;
if (_month <= 0)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
}
return *this;
}
10、Date-天數(shù)
注意:
- -天數(shù)不會(huì)修改Date本身的數(shù)據(jù)(使用const修飾,避免修改)
- 邏輯與Date-=天數(shù)基本一致,可以進(jìn)行復(fù)用
實(shí)現(xiàn)代碼:
Date Date::operator-(int day) const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
11、++Date
注意:
前置++表示,Date先增后使用
實(shí)現(xiàn)代碼:
//++Date
Date& Date::operator++()
{
*this += 1;//復(fù)用Date+=天數(shù)
return *this;
}
12、Date++
注意:
語(yǔ)法規(guī)定,因?yàn)榕c前置命名相同的緣故,這里的后置函數(shù)多一個(gè)參數(shù)來(lái)與前置函數(shù)形成重載
后置++表示先使用后自增
實(shí)現(xiàn)代碼:
//Date++
Date Date::operator++(int)
{
Date tmp = *this;//保存一份日期
*this += 1;//自增當(dāng)前日期
return tmp;//返回自增前的日期
}
13、–Date
實(shí)現(xiàn)代碼:
//--Date
Date& Date::operator--()
{
*this -= 1;
return *this;
}
14、Date–
實(shí)現(xiàn)代碼:
//Date--
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
15、日期比較
注:可以多次復(fù)用
實(shí)現(xiàn)代碼:
//日期比較
bool Date::operator>(const Date& d) const
{
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;
}
bool Date::operator==(const Date& d) const
{
return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator<(const Date& d) const
{
return !(*this >= d);
}
bool Date::operator>=(const Date& d) const
{
return *this > d || *this == d;
}
bool Date::operator<=(const Date& d) const
{
return !(*this > d);
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
16、Date相減
實(shí)現(xiàn)代碼:
//日期減日期
int Date::operator-(const Date& d) const
{
//確定日期的大小
Date max = *this;
Date min = d;
if (*this < d)//復(fù)用日期比較
{
max = d;
min = *this;
}
int day = 0;
while (max != min)
{
++min;
++day;
}
return day;
}
17、日期輸入\日期輸出
注意:
- 對(duì)于輸入操作符,我們習(xí)慣是cin>>date,而這樣的用法表示做操作數(shù)是cin,右操作數(shù)為日期對(duì)象,但是對(duì)于類成員函數(shù)來(lái)說(shuō),存在著隱含參數(shù)this指針(占據(jù)和第一個(gè)參數(shù)位置,即日期對(duì)象是左操作數(shù))
- 雖然定義成類外函數(shù)能修改參數(shù)位置,但是無(wú)法訪問(wèn)類里的私有成員變量,這里我們使用友元函數(shù)來(lái)解決,即在類里聲明函數(shù)前加上friend,便可以訪問(wèn)成員
實(shí)現(xiàn)代碼:
//輸出操作符重載
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "年" << d._month << "月" << d._day << "日" ;
return _cout;
}
//輸出操作符重載
istream& operator>>(istream& _cin, Date& d)
> {
_cin >> d._year >> d._month >> d._day;
return _cin;
}
效果圖:
- date,而這樣的用法表示做操作數(shù)是cin,右操作數(shù)為日期對(duì)象,但是對(duì)于類成員函數(shù)來(lái)說(shuō),存在著隱含參數(shù)this指針(占據(jù)和第一個(gè)參數(shù)位置,即日期對(duì)象是左操作數(shù))
- 雖然定義成類外函數(shù)能修改參數(shù)位置,但是無(wú)法訪問(wèn)類里的私有成員變量,這里我們使用友元函數(shù)來(lái)解決,即在類里聲明函數(shù)前加上friend,便可以訪問(wèn)成員
實(shí)現(xiàn)代碼:
//輸出操作符重載
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "年" << d._month << "月" << d._day << "日" ;
return _cout;
}
//輸出操作符重載
istream& operator>>(istream& _cin, Date& d)
{
_cin >> d._year >> d._month >> d._day;
return _cin;
}
效果圖:

總結(jié)
到此這篇關(guān)于C++類和對(duì)象實(shí)戰(zhàn)之Date類實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C++?Date類的實(shí)現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++類與對(duì)象深入之引用與內(nèi)聯(lián)函數(shù)與auto關(guān)鍵字及for循環(huán)詳解
朋友們好,這篇播客我們繼續(xù)C++的初階學(xué)習(xí),現(xiàn)在對(duì)一些C++的入門知識(shí)做了些總結(jié),整理出來(lái)一篇博客供我們一起復(fù)習(xí)和學(xué)習(xí),如果文章中有理解不當(dāng)?shù)牡胤?還希望朋友們?cè)谠u(píng)論區(qū)指出,我們相互學(xué)習(xí),共同進(jìn)步2022-06-06
C++使用htslib庫(kù)讀入和寫出bam文件的實(shí)例
下面小編就為大家分享一篇C++使用htslib庫(kù)讀入和寫出bam文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
詳細(xì)談?wù)凜語(yǔ)言中動(dòng)態(tài)內(nèi)存
在C語(yǔ)言中,編寫程序的時(shí)候不能確定內(nèi)存的大小,希望程序在運(yùn)行的過(guò)程中根據(jù)數(shù)據(jù)量的大小動(dòng)態(tài)的分配內(nèi)存,這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中動(dòng)態(tài)內(nèi)存的相關(guān)資料,需要的朋友可以參考下2022-03-03

