国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁(yè) > news >正文

有名網(wǎng)站建設(shè)公司百度網(wǎng)盤(pán)pc端網(wǎng)頁(yè)版

有名網(wǎng)站建設(shè)公司,百度網(wǎng)盤(pán)pc端網(wǎng)頁(yè)版,在線(xiàn)客服功能,直銷(xiāo)可以做網(wǎng)站有效果嗎個(gè)人主頁(yè)點(diǎn)擊直達(dá):小白不是程序媛 C系列專(zhuān)欄:C頭疼記 目錄 前言: 運(yùn)算符重載 運(yùn)算符重載 賦值運(yùn)算符重載 前置和后置重載 const成員 取地址及const取地址操作符重載 使用函數(shù)操作符重載完成日期類(lèi)的實(shí)現(xiàn) 前言: 上篇文…

=========================================================================

個(gè)人主頁(yè)點(diǎn)擊直達(dá):小白不是程序媛

C++系列專(zhuān)欄:C++頭疼記

=========================================================================

目錄?

前言:

運(yùn)算符重載

運(yùn)算符重載?

賦值運(yùn)算符重載

前置++和后置++重載

const成員

取地址及const取地址操作符重載

使用函數(shù)操作符重載完成日期類(lèi)的實(shí)現(xiàn)


前言:

上篇文章介紹了在C++的類(lèi)六個(gè)成員函數(shù)中的三個(gè),分別是構(gòu)造函數(shù)、析構(gòu)函數(shù)、拷貝構(gòu)造函數(shù),不知道大家有沒(méi)有所收獲,今天我們帶來(lái)的是剩下的三個(gè)函數(shù),以及結(jié)合這六個(gè)函數(shù)完成一個(gè)完整的日期類(lèi)的實(shí)現(xiàn),讓我們開(kāi)始今天的征程吧!


運(yùn)算符重載

在C++中有很多的運(yùn)算符,包括 +、- 、* 、/、等等,一個(gè)兩兩結(jié)合的操作符++、--、+=,>=、==等等。

int main()
{int i = 0;cout << ++i << endl;cout << --i << endl;i = 2;cout << i << endl;return 0;
}

對(duì)于內(nèi)置類(lèi)型來(lái)說(shuō)我們可以直接使用,但是對(duì)于我們自己定義的自定義類(lèi)型呢?該如何使用呢?

  • 運(yùn)算符重載?

C++為了增強(qiáng)代碼的可讀性引入了運(yùn)算符重載運(yùn)算符重載是具有特殊函數(shù)名的函數(shù),也具有其
返回值類(lèi)型,函數(shù)名字以及參數(shù)列表,其返回值類(lèi)型與參數(shù)列表與普通的函數(shù)類(lèi)似。

函數(shù)名字為:關(guān)鍵字operator后面接需要重載的運(yùn)算符符號(hào)。
函數(shù)原型:返回值類(lèi)型?operator操作符(參數(shù)列表)?

注意:

  • 不能通過(guò)連接其他符號(hào)來(lái)創(chuàng)建新的操作符:比如operator@
  • 重載操作符必須有一個(gè)類(lèi)類(lèi)型參數(shù)
  • 用于內(nèi)置類(lèi)型的運(yùn)算符,其含義不能改變,例如:內(nèi)置的整型+,不 能改變其含義
  • 作為類(lèi)成員函數(shù)重載時(shí),其形參看起來(lái)比操作數(shù)數(shù)目少1,因?yàn)槌蓡T函數(shù)的第一個(gè)參數(shù)為隱藏的this
  • .*? ::? sizeof? ?:? . 注意以上5個(gè)運(yùn)算符不能重載。這個(gè)經(jīng)常在筆試選擇題中出現(xiàn)。

示例:==運(yùn)算符重載

class Date
{
public:Date(int year = 1, int month = 1,int day=1){_year = year;_month = month;_day = day;}bool operator==(const Date& y){return _year == y._year && _month == y._month && _day == y._day;}
private :int _year;int _month;int _day;
};
  • 賦值運(yùn)算符重載

賦值運(yùn)算符重載格式

參數(shù)類(lèi)型:const T&,傳遞引用可以提高傳參效率
返回值類(lèi)型:T&,返回引用可以提高返回的效率,有返回值目的是為了支持連續(xù)賦值檢測(cè)是否自己給自己賦值
返回*this :要復(fù)合連續(xù)賦值的含義

class Date
{
public:Date(int year = 1, int month = 1,int day=1){_year = year;_month = month;_day = day;}Date& operator=(const Date& d){this->_year = d._year;this->_month = d._month;this->_day = d._day;return *this;}
private :int _year;int _month;int _day;
};

賦值運(yùn)算符只能重載成類(lèi)的成員函數(shù)不能重載成全局函數(shù)

class Date
{
public:Date(int year = 1900, int month = 1, int day = 1){_year = year;_month = month;_day = day;}int _year;int _month;int _day;
};
// 賦值運(yùn)算符重載成全局函數(shù),注意重載成全局函數(shù)時(shí)沒(méi)有this指針了,需要給兩個(gè)參數(shù)
Date& operator=(Date& left, const Date& right)
{if (&left != &right){left._year = right._year;left._month = right._month;left._day = right._day;}return left;
}

?原因:賦值運(yùn)算符如果不顯式實(shí)現(xiàn),編譯器會(huì)生成一個(gè)默認(rèn)的。此時(shí)用戶(hù)再在類(lèi)外自己實(shí)現(xiàn)
一個(gè)全局的賦值運(yùn)算符重載,就和編譯器在類(lèi)中生成的默認(rèn)賦值運(yùn)算符重載沖突了,故賦值
運(yùn)算符重載只能是類(lèi)的成員函數(shù)。

用戶(hù)沒(méi)有顯式實(shí)現(xiàn)時(shí),編譯器會(huì)生成一個(gè)默認(rèn)賦值運(yùn)算符重載,以值的方式逐字節(jié)拷貝。

注意:內(nèi)置類(lèi)型成員變量是直接賦值的,而自定義類(lèi)型成員變量需要調(diào)用對(duì)應(yīng)類(lèi)的賦值運(yùn)算符
重載完成賦值。

class Date
{
public:void Print(){cout << _year << "-" << _month << "-" << _day << endl;}Date(int year = 1, int month = 1,int day=1){_year = year;_month = month;_day = day;}
private :int _year;int _month;int _day;
};
int main()
{Date d1;d1.Print();Date d2 = d1;d2.Print();return 0;
}

這里我們沒(méi)有寫(xiě)賦值重載函數(shù),編譯器會(huì)自動(dòng)生成一個(gè)完成操作,和拷貝構(gòu)造函數(shù)優(yōu)點(diǎn)類(lèi)似。

注意:如果類(lèi)中未涉及到資源管理,賦值運(yùn)算符是否實(shí)現(xiàn)都可以;一旦涉及到資源管理則必
須要實(shí)現(xiàn)。

  • 前置++和后置++重載

class Date
{
public:void Print(){cout << _year << "-" << _month << "-" << _day << endl;}Date(int year = 1, int month = 1,int day=1){_year = year;_month = month;_day = day;}// 前置++:返回+1之后的結(jié)果// 注意:this指向的對(duì)象函數(shù)結(jié)束后不會(huì)銷(xiāo)毀,故以引用方式返回提高效率Date& operator++(){*this += 1;return *this;}// 后置++:// 前置++和后置++都是一元運(yùn)算符,為了讓前置++與后置++形成能正確重載// C++規(guī)定:后置++重載時(shí)多增加一個(gè)int類(lèi)型的參數(shù),但調(diào)用函數(shù)時(shí)該參數(shù)不用傳遞,編譯器自動(dòng)傳遞// 注意:后置++是先使用后+1,因此需要返回+1之前的舊值,故需在實(shí)現(xiàn)時(shí)需要先將this保存一份,然后給this + 1// ???而temp是臨時(shí)對(duì)象,因此只能以值的方式返回,不能返回引用Date operator++(int){Date tmp(*this);*this += 1;return tmp;}
private :int _year;int _month;int _day;
};
int main()
{Date d1;d1++;// 1-1-2++d1;// 1-1-1return 0;
}

const成員

將const修飾的“成員函數(shù)”稱(chēng)之為const成員函數(shù),const修飾類(lèi)成員函數(shù),實(shí)際修飾該成員函數(shù)
隱含的this指針,表明在該成員函數(shù)中不能對(duì)類(lèi)的任何成員進(jìn)行修改。

我們來(lái)看看下面這段代碼?

class Date
{
public://void Print( Date *this)void Print(){cout << _year << "-" << _month << "-" << _day << endl;}
private:int _year = 1;int _month = 1;int _day = 1;
};
int main()
{const Date d1;//d1.Print(&d1)//權(quán)限放大d1.Print();return 0;
}

這段代碼在編譯器上是編譯不通過(guò)的,說(shuō)明語(yǔ)法有問(wèn)題。

因?yàn)槲覀儎?chuàng)建了一個(gè)const Date *的d1,將d1作為參數(shù)傳給函數(shù)時(shí),函數(shù)的的參數(shù)是一個(gè)Date *,兩個(gè)參數(shù)不匹配,權(quán)限會(huì)放大,所以編譯不通過(guò)。

其實(shí)函數(shù)的形參就是this指針,那我們?nèi)绾螌his指針修飾為const呢?

只需要在函數(shù)聲明后面加上const 即可將this指針修飾為const。

class Date
{
public://void Print( Date *this)void Print(){cout << _year << "-" << _month << "-" << _day << endl;}
private:int _year = 1;int _month = 1;int _day = 1;
};
int main()
{const Date d1;//d1.Print(&d1)//權(quán)限放大d1.Print();Date d2;d2.Print();return 0;
}

上面為修飾后的代碼,我們?cè)趧?chuàng)建一個(gè)普通的Date *,能否在const修飾的this指針函數(shù)中跑起來(lái)呢?

答案是可以的,因?yàn)闄?quán)限可以平移、縮小,不能放大。

那么請(qǐng)思考以下問(wèn)題:

const對(duì)象可以調(diào)用非const成員函數(shù)嗎?

????????不可以,權(quán)限放大。
非const對(duì)象可以調(diào)用const成員函數(shù)嗎?

? ? ? ? 可以,權(quán)限縮小。
const成員函數(shù)內(nèi)可以調(diào)用其它的非const成員函數(shù)嗎?

? ? ? ? 不可以,權(quán)限放大。
非const成員函數(shù)內(nèi)可以調(diào)用其它的const成員函數(shù)嗎??

? ? ? ? 可以,權(quán)限縮小。


取地址及const取地址操作符重載

這兩個(gè)默認(rèn)成員函數(shù)一般不用重新定義 ,編譯器默認(rèn)會(huì)生成。

class Date
{
public:Date* operator&(){return this;}const Date* operator&()const{return this;}
private:int _year; // 年int _month; // 月int _day; // 日
};

這兩個(gè)運(yùn)算符一般不需要重載,使用編譯器生成的默認(rèn)取地址的重載即可,只有特殊情況,才需
要重載,比如想讓別人獲取到指定的內(nèi)容!?

這個(gè)函數(shù)基本沒(méi)有什么作用,很少用到作為了解即可!


使用函數(shù)操作符重載完成日期類(lèi)的實(shí)現(xiàn)

void Date::Print()
{cout << _year << "-" << _month << "-" << _day << endl;
}
//獲取天數(shù)
int Date::GetMonthDay(int year, int month)
{int monthArr[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 monthArr[month];
}
//構(gòu)造函數(shù)
Date::Date(int year=1, int month=1, int day=1)
{_year = year;_month = month;_day = day;
}
//拷貝構(gòu)造函數(shù)
Date::Date(const Date& d)
{this->_year = d._year;this->_month = d._month;this->_day = d._day;
}
//賦值運(yùn)算符重載
Date& Date::operator=(const Date& d)
{this->_year = d._year;this->_month = d._month;this->_day = d._day;return *this;
}
//日期+=天數(shù)
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){_year++;_month = 1;}}return *this;
}
//日期+天數(shù)
Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return *this;
}
//日期-=天數(shù)
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){//月小于0時(shí)應(yīng)該先借位--_month;if (_month == 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}
//日期-天數(shù)
Date Date::operator-(int day)
{Date tmp(*this);tmp -= day;return *this;
}
//前置++
Date& Date::operator++()
{*this += 1;return *this;
}
//后置++
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}
//前置--
Date& Date::operator--()
{*this -= 1;return *this;
}
//后置--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}
//>運(yùn)算符重載
bool Date::operator>(const Date& y)
{if (_year > y._year){return true;}else if (_year == y._year && _month > y._month){return true;}else if (_year == y._year && _month == y._month && _day > y._day){return true;}return false;
}
//==運(yùn)算符重載
bool Date::operator==(const Date& y)
{return _year == y._year && _month == y._month && _day == y._day;
}
//>=運(yùn)算符重載
bool Date::operator>=(const Date& y)
{return *this > y || *this == y;
}
//<運(yùn)算符重載
bool Date::operator<(const Date& y)
{return !(*this >= y);
}
//!=運(yùn)算符重載
bool Date::operator!=(const Date& y)
{return !(*this == y);
}
//日期-日期 返回天數(shù)
int Date::operator-(const Date& y)
{Date max = *this;Date min = y;if (*this < y){max = y;min = *this;}int n = 0;while (min != max){min++;n++;}return n;
}

這里我們使用操作符重載,完成了一個(gè)簡(jiǎn)單的日期計(jì)算器,當(dāng)然這其中還有很多沒(méi)有完善的地方。像我們構(gòu)造一些非法的數(shù)據(jù)作為日期等等一些小問(wèn)題,我就留給大家了。

C++類(lèi)和對(duì)象(中)的六個(gè)成員函數(shù)就講完了,大家可以結(jié)合上篇文章細(xì)細(xì)閱讀,慢慢探索C++這六個(gè)成員函數(shù)的奧秘?,總結(jié)收獲出一些自己的東西。也希望大家留言指出我文章中出現(xiàn)的內(nèi)容,同時(shí)也感謝各位看官的三連支持,你們的支持就是我更新的動(dòng)力!!!?


下篇預(yù)告:類(lèi)和對(duì)象(下)

http://m.aloenet.com.cn/news/43724.html

相關(guān)文章:

  • 網(wǎng)站建設(shè)測(cè)試流程圖友情鏈接如何添加
  • 怎么增加網(wǎng)站的外鏈學(xué)校網(wǎng)站建設(shè)
  • 網(wǎng)站設(shè)計(jì)任務(wù)書(shū)范文站長(zhǎng)工具seo綜合查詢(xún)?cè)L問(wèn)
  • 精通網(wǎng)站建設(shè) 全能建站密碼pdf理發(fā)美發(fā)培訓(xùn)學(xué)校
  • 公司網(wǎng)站制作推廣公司
  • 電子商務(wù)網(wǎng)站有哪幾種搜索引擎營(yíng)銷(xiāo)的常見(jiàn)方式
  • 網(wǎng)站代理游戲合川網(wǎng)站建設(shè)
  • 蘇州專(zhuān)業(yè)高端網(wǎng)站建設(shè)公司專(zhuān)業(yè)制作網(wǎng)站的公司哪家好
  • 泰州網(wǎng)站建設(shè)服務(wù)熱線(xiàn)全網(wǎng)推廣軟件
  • 免費(fèi)推廣網(wǎng)站2023mmm網(wǎng)絡(luò)營(yíng)銷(xiāo)項(xiàng)目
  • 桐鄉(xiāng)住房和城鄉(xiāng)規(guī)劃建設(shè)局網(wǎng)站如何做網(wǎng)絡(luò)銷(xiāo)售產(chǎn)品
  • 上海網(wǎng)站設(shè)計(jì)工具網(wǎng)絡(luò)推廣運(yùn)營(yíng)團(tuán)隊(duì)
  • 建設(shè)廳特種作業(yè)證件查詢(xún)官網(wǎng)網(wǎng)站優(yōu)化推廣公司
  • 重慶品牌網(wǎng)站建設(shè)優(yōu)化網(wǎng)站排名方法
  • 網(wǎng)站設(shè)計(jì)導(dǎo)航欄高度佛山市人民政府門(mén)戶(hù)網(wǎng)站
  • 網(wǎng)站建設(shè)詢(xún)價(jià)文件無(wú)錫營(yíng)銷(xiāo)型網(wǎng)站制作
  • 海爾集團(tuán)電商網(wǎng)站建設(shè)百度網(wǎng)頁(yè)版進(jìn)入
  • 怎樣在國(guó)外網(wǎng)站做推廣搜索關(guān)鍵詞排名一般按照什么收費(fèi)
  • 做網(wǎng)站做軟件怎么賺錢(qián)嗎搜索量最大的關(guān)鍵詞
  • 咸秧草做哪些網(wǎng)站優(yōu)化大師免安裝版
  • 鄭州做網(wǎng)站推廣外包產(chǎn)品推廣方式
  • 鞍山網(wǎng)站制作公司優(yōu)化綠松石什么意思
  • 泉州建設(shè)網(wǎng)站開(kāi)發(fā)快速排名優(yōu)化
  • 湖南做網(wǎng)站磐石網(wǎng)絡(luò)案例cba最新排名
  • 淘寶的網(wǎng)站建設(shè)seo分析報(bào)告怎么寫(xiě)
  • 網(wǎng)站如何提高權(quán)重做百度推廣怎么做才能有電話(huà)
  • 響應(yīng)式網(wǎng)站建設(shè)效果迅雷下載磁力天堂
  • 萬(wàn)網(wǎng)個(gè)人網(wǎng)站備案查詢(xún)東莞今天的最新通知
  • 有關(guān)做聚合物電池公司的網(wǎng)站網(wǎng)站優(yōu)化外包推薦
  • 如何再?lài)?guó)外網(wǎng)站做折扣什么是seo?