1.運算符重載的意義
C++為了增強代碼的可讀性引入了運算符重載,運算符重載是具有特殊函數(shù)名的函數(shù),也具有其返回值類型,函數(shù)名字以及參數(shù)列表,其返回值類型與參數(shù)列表與普通的函數(shù)類似。
函數(shù)名字為:關鍵字operator后面接需要重載的運算符符號。
函數(shù)原型:返回值類型 operator操作符(參數(shù)列表)
當您調用一個重載函數(shù)或重載運算符時,編譯器通過把您所使用的參數(shù)類型與定義中的參數(shù)類型進行比較,決定選用最合適的定義。選擇最合適的重載函數(shù)或重載運算符的過程,稱為重載決策。
在同一個作用域內,可以聲明幾個功能類似的同名函數(shù),但是這些同名函數(shù)的形式參數(shù)(指參數(shù)的個數(shù)、類型或者順序)必須不同。您不能僅通過返回類型的不同來重載函數(shù)。
注意:
- 不能通過連接其他符號來創(chuàng)建新的操作符:比如operator@
- 重載操作符必須有一個類類型參數(shù)
- 用于內置類型的運算符,其含義不能改變,例如:內置的整型+,不 能改變其含義
- 作為類成員函數(shù)重載時,其形參看起來比操作數(shù)數(shù)目少1,因為成員函數(shù)的第一個參數(shù)為隱藏的this
- .* sizeof ?: . ::?#注意以上6個運算符不能重載。切記
2.運算符重載的使用
通過實現(xiàn)一個日期類函數(shù),學習運算符重載的使用
先來寫一個類用來實現(xiàn)日期類的創(chuàng)建
class Date
{
//友元函數(shù)
friend ostream& operator<<(ostream& out, const Date& date);
friend ostream& operator>>(istream& in, Date& date);
public:
Date(int year = 0, int month = 0, int day = 0)
{
_year = year;
_month = month;
_day = day;
cout << "創(chuàng)建" << _year << endl;
}
~Date()
{
cout << "銷毀" << _year << endl;
}
//拷貝構造函數(shù)
Date(Date& date)
{
cout << "1" << endl;
_year = date._year;
_month = date._month;
_day = date._day;
}
//void Print();
//判斷兩個日期是否相等
bool operator==(const Date& days);
//判斷前一個日期是否大于等于后一個日期
bool operator>=(const Date& days);
//判斷前一個日期是否小于等于后一個日期
bool operator<=(const Date& days);
//判斷前一個日期是否大于后一個日期
bool operator>(const Date& days);
//判斷前一個日期是否小于后一個日期
bool operator<(const Date& days);
//判斷兩個日期是否不相等
bool operator!=(const Date& days);
//日期加一個天數(shù),返回一個日期,改變原日期
Date& operator+=(int days);
//日期加一個天數(shù),返回另一個日期,原日期不變
Date operator+(int days);
//日期減天數(shù),返回一個日期,原日期改變
Date& operator-=(int days);
//日期減日期,返回一個天數(shù)
int operator-=(Date& days);
Date& operator--();
Date& operator--(int) ;
Date& operator++();
Date& operator++(int) ;
private:
int _year;
int _month;
int _day;
};
//重載流提取
ostream& operator<<(ostream& out, const Date& date);
//重載流插入
ostream& operator>>(istream& in, Date& date);
3.友元函數(shù):(臨時插入)
類的友元函數(shù)是定義在類外部,但有權訪問類的所有私有(private)成員和保護(protected)成員。盡管友元函數(shù)的原型有在類的定義中出現(xiàn)過,但是友元函數(shù)并不是成員函數(shù)。
友元可以是一個函數(shù),該函數(shù)被稱為友元函數(shù);友元也可以是一個類,該類被稱為友元類,在這種情況下,整個類及其所有成員都是友元。
如果要聲明函數(shù)為一個類的友元,需要在類定義中該函數(shù)原型前使用關鍵字?friend。
注意:友元提供了一種突破封裝的方式,有時提供了便利。但是友元會增加耦合度,破壞了封裝,所以友元不宜多用。
說明:
- 友元函數(shù)可訪問類的私有和保護成員,但不是類的成員函數(shù)
- 友元函數(shù)不能用const修飾
- 友元函數(shù)可以在類定義的任何地方聲明,不受類訪問限定符限制
- 一個函數(shù)可以是多個類的友元函數(shù)
- 友元函數(shù)的調用與普通函數(shù)的調用原理相同
?
友元類下次再說
4.日期類函數(shù)的實現(xiàn)
4.1打印函數(shù)
/打印函數(shù),用下面的流插入流體取實現(xiàn)
//void Date::Print()
//{
// cout << _year << " "
// << _month << " "
// << _day << endl;
//}
4.2 比較運算符的實現(xiàn)
//相等
bool Date::operator==(const Date& days) const
{
return _year == days._year &&
_month == days._month &&
_day == days._day;
}
//大于
bool Date::operator>(const Date& days) const
{
if (_year > days._year)
{
return true;
}
else if (_year == days._year &&
_month > days._month)
{
return true;
}
else if (_year == days._year &&
_month == days._month &&
_day > days._day)
{
return true;
}
return false;
}
//小于
bool Date::operator<(const Date& days) const
{
return !(*this > days && *this == days);
}
//大于等于
bool Date::operator>=(const Date& days) const
{
return (*this > days)|| (*this == days);
}
//小于等于
bool Date::operator<=(const Date& days) const
{
return !(*this > days);
}
//不等于
bool Date::operator!=(const Date& days) const
{
return !(*this == days);
}
4.3加減法的實現(xiàn)
//判斷一個月的天數(shù)
int GetMonthDay(int _year, int _month)
{
static int monthday[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 monthday[_month];
}
//日期加一個天數(shù)
Date& Date::operator+=(const int days)
{
_day += days;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
//日期加天數(shù),本身不變
Date Date::operator+(const int days)
{
Date tmp(*this);
tmp += days;
return tmp;
}
//日期減天數(shù),本身改變
Date& Date::operator-=(int days)
{
if (days < _day)
{
_day -= days;
return *this;
}
else if (days == _day)
{
_month -= 1;
if (_month == 0)
{
_month = 12;
_year--;
}
_day = GetMonthDay(_year, _month);
return *this;
}
else
{
days -= _day;
_month -= 1;
if (_month == 0)
{
_month = 12;
_year -= 1;
}
while (days > GetMonthDay(_year, _month))
{
_month -= 1;
if (_month == 0)
{
_month = 12;
_year -= 1;
}
}
return *this;
}
}
Date Date::operator-(int days)
{
Date tmp(*this);
tmp -= 1;
return tmp;
}
//日期減日期
int Date::operator-=(Date& days)
{
//int days = 0;
if (*this > days)
{
if(_day>days._day)
return _day - days._day;
else
{
return _day + GetMonthDay(_year, _month - 1) - days._day;
}
}
else if (*this == days)
{
return 0;
}
else
{
if (_day < days._day)
return days._day-_day;
else
{
return days._day+ GetMonthDay(_year, _month - 1) - _day;
}
}
}
//前置日期減減
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//后置減減,后置加加需要傳一個int,語法規(guī)定,后置加加也一樣
Date& Date::operator--(int)
{
Date tmp(*this);
tmp -= 1;
return *this;
}
//前置加加
Date& Date::operator++()
{
*this += 1;
return *this;
}
//后置加加
Date& Date::operator++(int)
{
Date tmp(*this);
tmp += 1;
return *this;
}
4.4流提取和流插入
//流插入 ostream& operator<<(ostream& out, const Date& date) { out << date._year << "年" << date._month << "月" << date._day << "日" << endl; return out; } //流提取 ostream& operator>>(istream& in, Date& date) { in >> date._year >> date._month >> date._day; return cout<<date; }
這是運算符重載和重載函數(shù)的實現(xiàn),有興趣的可以自己敲一下。文章來源:http://www.zghlxwxcb.cn/news/detail-458126.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-458126.html
到了這里,關于運算符重載和重載函數(shù)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!