怎么在網(wǎng)上做公司網(wǎng)站如何創(chuàng)建自己的網(wǎng)址
目錄
- 前言
- ostream和istream
- 自定義類型的流插入重載
- 自定義類型的流提取重載
- 解決私有問題
- 日期類總接口

前言
??我們在上一節(jié)實(shí)現(xiàn)日期類時(shí),在輸入和輸出打印時(shí),經(jīng)常會調(diào)用兩個(gè)函數(shù):
void Insert()//輸入函數(shù){cin >> _year;cin >> _month;cin >> _day;if (!CheakDay())cout << "輸入錯(cuò)誤,請重新輸入" << endl;}
void Print()//輸出函數(shù){cout << _year << "年" << _month << "月" << _day << "日" << endl;}
??我們經(jīng)常調(diào)用這兩個(gè)函數(shù)進(jìn)行輸入輸出日期,我會覺得麻煩,那我可不可以直接使用cout和cin來輸出輸入呢,這就用到我們流插入和流提取運(yùn)算符的重載。
ostream和istream
??在cplusplus網(wǎng)站中,就有詳細(xì)介紹:
其實(shí),cout是ostream類型的全局對象,cin是一個(gè)istream類型的全局對象。這些都是在C++的標(biāo)準(zhǔn)庫中寫好了,它們被包含在iostream這個(gè)頭文件里面。
??ostream和istream里面都寫了很多函數(shù),想要輸入或者輸出帶精度的都可以進(jìn)行調(diào)用。
??我們都知道,我們可以直接調(diào)用cout來輸出內(nèi)置類型,是因?yàn)樗呀?jīng)在庫中寫好了重載函數(shù),如下圖所示:
例如:
int i=1;
cout<<i ——》等價(jià)于cout.operator<<(i)
double d=1.1
cout<<d ——》等價(jià)于cout.operator<<(d)
cout能自動識別類型,本質(zhì)上是因?yàn)檫@些流插入重載自動構(gòu)成函數(shù)重載。
cin也一樣。
??當(dāng)我們想要cout一個(gè)自定義類型,即cout<<d1,發(fā)現(xiàn)代碼會報(bào)錯(cuò),因?yàn)閹炖锩鏇]有對應(yīng)寫自定義類型的輸出,我們要自己重載寫一個(gè)。
自定義類型的流插入重載
??雖然上圖中函數(shù)的參數(shù)只有一個(gè),但我們要知道的是operator<<是寫在ostream這個(gè)類里面的,所以這個(gè)函數(shù)應(yīng)該是有兩個(gè)參數(shù),一個(gè)是隱藏的this指針,所以實(shí)際上庫里聲明定義的重載應(yīng)該為:
ostream& operator<<(ostream& this, int val);
??當(dāng)我們仿照其寫自定義類型的流插入重載函數(shù)時(shí),ostream& this,這個(gè)參數(shù)是不能省略的。
??明白了這個(gè),現(xiàn)在我們在日期類中類中聲明流插入重載函數(shù):
void operator<<(ostream& out);
//必須用引用傳參是因?yàn)閛stream類型不支持拷貝構(gòu)造
//(傳參時(shí)如果傳的自定義類型會調(diào)用它的拷貝構(gòu)造)
??在類外定義這個(gè)函數(shù)時(shí):
void Date::operator<<(ostream& out)
{out << _year << "年" << _month << "月" << _day << "日" << endl;
}
??我們重載的是自定義類型,但自定義類型內(nèi)部最終還是內(nèi)置類型。
out << _year << “年” << _month << “月” << _day << “日” << endl;
這一行其實(shí)是多個(gè)函數(shù)的調(diào)用,
先執(zhí)行out<<_year,
它會調(diào)用庫里的函數(shù):ostream& operator<< (int val);輸出
ostream&也就是out作為返回值又變成:out << “年” << _month << “月” << _day << “日” << endl;然后再輸出。
到此刻,我們調(diào)用cout<<d1時(shí)發(fā)現(xiàn)會報(bào)錯(cuò),這是為什么呢?
我們將調(diào)用
void Date::operator<<(ostream& out)
這個(gè)函數(shù)的式子寫出來其實(shí)是:d1.operator<<(cout);
寫成這樣就可以正常調(diào)用。
即d1<<cout。
??其實(shí)原因很簡單:在運(yùn)算符重載過程中,參數(shù)順序和操作數(shù)順序必須保持一致。
我們實(shí)際想要寫成:cout<<d1,
則參數(shù)順序則應(yīng)該為:ostream Date
但是存在一個(gè)問題,這個(gè)函數(shù)是Date類中的成員函數(shù),它有隱含一個(gè)this指針,把第一個(gè)參數(shù)占用了,ostream則不能作為第一個(gè)參數(shù)。此時(shí)我們只能將其重載成全局函數(shù)。
總結(jié)起來就是:operator<<想重載為成員函數(shù),可以,但是使用時(shí)d1<<cout不符合習(xí)慣,建議重載成全局函數(shù)。
如下:
void operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
}
??此時(shí)會出現(xiàn)一個(gè)私有不可訪問的問題,為方便使用,我們先將私有成員變量設(shè)為公有。此時(shí)可以正常使用,但當(dāng)我們想連續(xù)輸出時(shí),如:
cout << d1 << d2;
??此時(shí)編譯器又會繼續(xù)報(bào)錯(cuò)。因?yàn)楹瘮?shù)調(diào)用會先調(diào)用cout << d1,此時(shí)沒有返回值,所以會報(bào)錯(cuò),我們應(yīng)該有個(gè)返回值,且這個(gè)返回值應(yīng)該是cout,才能使得表達(dá)式繼續(xù)執(zhí)行,變?yōu)閏out << d2,就可以連續(xù)輸出,又因?yàn)閛ut是cout的引用,即out是cout的別名,只要返回out即可,所以函數(shù)可改為:
ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}
自定義類型的流提取重載
??與自定義類型的流插入重載一致,就不再細(xì)說,直接得代碼:
//函數(shù)的聲明
istream & operator>>(istream & in, Date & d);
//函數(shù)的定義
istream& operator>>(istream& in, Date& d)
{cout << "請依次輸入年月日:" << endl;in >> d._year >> d._month >> d._day;return in;
}
??這時(shí)我們也可以將上節(jié)課寫的日期檢查寫入:
istream& operator>>(istream& in, Date& d)
{cout << "請依次輸入年月日:" << endl;in >> d._year >> d._month >> d._day;if (!d.CheakDay()){cout << "日期非法" << endl;}return in;
}
解決私有問題
??將函數(shù)在類中聲明為友元函數(shù),這在類和對象終章會講到。
??代碼如下:
class Date
{//聲明友元函數(shù)friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
private:int _year;int _month;int _day;
};
ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}
istream& operator>>(istream& in, Date& d)
{cout << "請依次輸入年月日:" << endl;in >> d._year >> d._month >> d._day;if (!d.CheakDay()){cout << "日期非法" << endl;}return in;
}
??聲明函數(shù)是類的朋友,則函數(shù)可以訪問類中的所有成員,友元的語法就是這么簡單。
日期類總接口
??補(bǔ)充了這兩個(gè)流的重載,我們可以將Date.h完善:
class Date
{friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public:Date(int year, int month, int day):_year(year), _month(month), _day(day){if (!CheakDay()){cout << "日期非法" << endl;}}int GetMonthDay(int year, int month){assert(month > 0 && month < 13);static int MonthDayArr[] = { 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;elsereturn MonthDayArr[month];}bool CheakDay(){if (_year <= 0 || _month <= 0 || _month > 12 || _day<0 || _day>GetMonthDay(_year, _month))return false;elsereturn true;}bool operator<(const Date& d)const;bool operator<=(const Date& d)const;bool operator>(const Date& d)const;bool operator>=(const Date& d)const;bool operator==(const Date& d)const;bool operator!=(const Date& d)const;Date& operator+=(int n);Date operator+(int n);Date& operator-=(int n);Date operator-(int n);Date& operator++();Date& operator--();Date operator++(int);Date operator--(int);int operator-(const Date& d)const;
private:int _year;int _month;int _day;
};