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

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

總結
到此這篇關于C++類和對象實戰(zhàn)之Date類實現(xiàn)的文章就介紹到這了,更多相關C++?Date類的實現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++類與對象深入之引用與內(nèi)聯(lián)函數(shù)與auto關鍵字及for循環(huán)詳解
朋友們好,這篇播客我們繼續(xù)C++的初階學習,現(xiàn)在對一些C++的入門知識做了些總結,整理出來一篇博客供我們一起復習和學習,如果文章中有理解不當?shù)牡胤?還希望朋友們在評論區(qū)指出,我們相互學習,共同進步2022-06-06

