




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、-2-3-4-class Rectanglepublic: Rectangle(int newWidth, int newHeight) :width(newWidth), height(newHeight) Rectangle() void drawShape() const; void drawShape(int width, int height) const;private: int width; int height;Rectangle:Rectangle(int newWidth, int newHeight) width=newWidth; height=newHeight;按成
2、員在類中聲明的順序構(gòu)造,而不是按成員在類中聲明的順序構(gòu)造,而不是按構(gòu)造函數(shù)說(shuō)明中的冒號(hào)后面的順序按構(gòu)造函數(shù)說(shuō)明中的冒號(hào)后面的順序class A public : A(int j) : age(j),num(age+1) cout“age:”age“,num:”numendl; protected: int num; int age;成員以其在類中聲明的順序構(gòu)造成員以其在類中聲明的順序構(gòu)造int main() A sa(15);-6-void Rectangle:drawShape() const drawShape(width, height);void Rectangle:drawShape
3、(int width, int height) const for (int i = 0; i height; i+) for (int j = 0; j width; j+) std:cout *; std:cout n; p避免在重載的函數(shù)版本中編寫(xiě)重復(fù)的代碼,應(yīng)將重復(fù)代碼避免在重載的函數(shù)版本中編寫(xiě)重復(fù)的代碼,應(yīng)將重復(fù)代碼 提取成獨(dú)立函數(shù),形成調(diào)用關(guān)系。提取成獨(dú)立函數(shù),形成調(diào)用關(guān)系。-7-#include int main() Rectangle box(30, 5); std:cout drawShape(): n; box.drawShape(); std:cout ndrawShap
4、e(40, 2): n; box.drawShape(40, 2); return 0;p編譯器根據(jù)調(diào)用成員函數(shù)時(shí)傳遞的實(shí)參,匹配一個(gè)函數(shù)編譯器根據(jù)調(diào)用成員函數(shù)時(shí)傳遞的實(shí)參,匹配一個(gè)函數(shù) 版本。重載函數(shù)的版本應(yīng)本著方便用戶的原則設(shè)計(jì)。版本。重載函數(shù)的版本應(yīng)本著方便用戶的原則設(shè)計(jì)。-8-Rectangle(int newWidth, int newHeight) :width(newWidth), height(newHeight) Rectangle () :width(20), height(4) -9-10-pconstconst:成員函數(shù)聲明和定義處都要給出。:成員函數(shù)聲明和定義處都要給
5、出。pinlineinline:通常只在成員函數(shù)聲明處給出。:通常只在成員函數(shù)聲明處給出。p缺省值:只在成員函數(shù)聲明處給出。缺省值:只在成員函數(shù)聲明處給出。-11-class Rectanglepublic: Rectangle(int newWidth, int newHeight) :width(newWidth), height(newHeight) Rectangle() void drawShape(int width, int height, bool useCurrentValue = false) const;private: int width; int height;-12
6、-void Rectangle:drawShape(int newWidth, int newHeight,bool useCurrentValue) const int printWidth, printHeight;if( useCurrentValue = true)printWidth = width;printHeight = height;elseprintWidth = newWidth;printHeight = newHeight;.-13-void Rectangle:drawShape(int width, int height,bool useCurrentValue)
7、 const for (int i = 0; i printHeight; i+) for (int j = 0; j printWidth; j+) std:cout *; std:cout n; p實(shí)際的繪圖處理邏輯沒(méi)有變化。實(shí)際的繪圖處理邏輯沒(méi)有變化。-14-#include int main() Rectangle box(20, 5); std:cout drawShape(0, 0, true).n; box.drawShape(0, 0, true); std:cout drawShape(25, 4).n; box.drawShape(25, 4); return 0;p編譯器
8、根據(jù)函數(shù)聲明中的缺省值,調(diào)用函數(shù)時(shí)補(bǔ)充缺省編譯器根據(jù)函數(shù)聲明中的缺省值,調(diào)用函數(shù)時(shí)補(bǔ)充缺省 值并產(chǎn)生實(shí)際的函數(shù)調(diào)用。值并產(chǎn)生實(shí)際的函數(shù)調(diào)用。box.drawShape(25, 4, false)-15-思考:思考:在哪里初始化對(duì)象的在哪里初始化對(duì)象的數(shù)據(jù)成員?數(shù)據(jù)成員?可以在可以在構(gòu)造函數(shù)構(gòu)造函數(shù)的的初始化部分,也可初始化部分,也可以在構(gòu)造函數(shù)體內(nèi)以在構(gòu)造函數(shù)體內(nèi)設(shè)置。設(shè)置。-17-Rectangle(int newWidth, int newHeight) :width(newWidth), height(newHeight) /構(gòu)造函數(shù)體 p執(zhí)行順序:先執(zhí)行初始化部分,再執(zhí)行構(gòu)造函數(shù)體。執(zhí)
9、行順序:先執(zhí)行初始化部分,再執(zhí)行構(gòu)造函數(shù)體。p執(zhí)行效率:初始化部分執(zhí)行效率高,對(duì)于對(duì)象成員,盡量執(zhí)行效率:初始化部分執(zhí)行效率高,對(duì)于對(duì)象成員,盡量 在初始化部分完成初始化。在初始化部分完成初始化。p引用成員和常數(shù)據(jù)成員只能通過(guò)初始化部分賦初值引用成員和常數(shù)據(jù)成員只能通過(guò)初始化部分賦初值。p不寫(xiě)初始化部分,將自動(dòng)調(diào)用缺省的初始化處理。不寫(xiě)初始化部分,將自動(dòng)調(diào)用缺省的初始化處理。列出需要初始列出需要初始化的變量名化的變量名-18-19-20-p此語(yǔ)句中,構(gòu)造函數(shù)此語(yǔ)句中,構(gòu)造函數(shù)TricycleTricycle接受一個(gè)常量引用,它指向一接受一個(gè)常量引用,它指向一個(gè)現(xiàn)有的個(gè)現(xiàn)有的TricycleTr
10、icycle對(duì)象對(duì)象triketrike。p通過(guò)傳入的通過(guò)傳入的triketrike對(duì)象,創(chuàng)建和對(duì)象,創(chuàng)建和triketrike“相同相同”的新對(duì)象。的新對(duì)象。p默認(rèn)復(fù)制構(gòu)造函數(shù)只將作為參數(shù)傳入的對(duì)象的每個(gè)默認(rèn)復(fù)制構(gòu)造函數(shù)只將作為參數(shù)傳入的對(duì)象的每個(gè)成員變量成員變量復(fù)制到新對(duì)象中,稱為淺復(fù)制。復(fù)制到新對(duì)象中,稱為淺復(fù)制。-21-22- 如何避免以上淺拷如何避免以上淺拷貝造成的問(wèn)題?貝造成的問(wèn)題?編寫(xiě)自己的復(fù)制構(gòu)造函數(shù)編寫(xiě)自己的復(fù)制構(gòu)造函數(shù)-24-p如果一個(gè)類需要在構(gòu)造函數(shù)中分配內(nèi)存,需要編寫(xiě)配套如果一個(gè)類需要在構(gòu)造函數(shù)中分配內(nèi)存,需要編寫(xiě)配套 的析構(gòu)函數(shù)以釋放內(nèi)存,同時(shí)需要實(shí)現(xiàn)相應(yīng)的復(fù)制構(gòu)造的
11、析構(gòu)函數(shù)以釋放內(nèi)存,同時(shí)需要實(shí)現(xiàn)相應(yīng)的復(fù)制構(gòu)造 函數(shù)以及復(fù)制運(yùn)算符函數(shù)(下一章)。函數(shù)以及復(fù)制運(yùn)算符函數(shù)(下一章)。-25-class Tricyclepublic: Tricycle(); Tricycle(const Tricycle& rhs); Tricycle(); int getSpeed() const return *speed; void setSpeed(int newSpeed) *speed = newSpeed; void pedal(); void brake();private: int *speed;-26-Tricycle:Tricycle() speed =
12、 new int; *speed = 5;Tricycle:Tricycle(const Tricycle& rhs) speed = new int; *speed = rhs.getSpeed();Tricycle:Tricycle() delete speed; speed = NULL;p構(gòu)造函數(shù)中分配內(nèi)存,析構(gòu)函數(shù)中釋構(gòu)造函數(shù)中分配內(nèi)存,析構(gòu)函數(shù)中釋 放內(nèi)存。放內(nèi)存。p默認(rèn)復(fù)制構(gòu)造函數(shù)中,不會(huì)分配內(nèi)存默認(rèn)復(fù)制構(gòu)造函數(shù)中,不會(huì)分配內(nèi)存 只是執(zhí)行只是執(zhí)行speed = rhs.speedspeed = rhs.speed。-27-void Tricycle:pedal() setSpee
13、d(*speed + 1); std:cout “ Pedaling getSpeed() “n;void Tricycle:brake() setSpeed(*speed - 1); std:cout “ Pedaling getSpeed() “n;ppedalpedal中通過(guò)中通過(guò)setSpeedsetSpeed設(shè)置新速度,也可以直接修改數(shù)據(jù)設(shè)置新速度,也可以直接修改數(shù)據(jù) * *speed += 1speed += 1,但通過(guò)調(diào)用,但通過(guò)調(diào)用setSpeedsetSpeed可以隱藏實(shí)現(xiàn)細(xì)節(jié)??梢噪[藏實(shí)現(xiàn)細(xì)節(jié)。p更進(jìn)一步:更進(jìn)一步:setSpeed ( getSpeed()+1 ); se
14、tSpeed ( getSpeed()+1 ); 不用關(guān)心不用關(guān)心speedspeed 到底是如何存儲(chǔ)等實(shí)現(xiàn)細(xì)節(jié)。到底是如何存儲(chǔ)等實(shí)現(xiàn)細(xì)節(jié)。-28-#include int main() Tricycle wichita; Tricycle dallas(wichita); wichita.setSpeed(10); std:coutWichita; wichita.pedal(); std:coutDallas; dallas.pedal(); std:coutWichita; wichita.brake(); std:coutDallas; dallas.brake(); return 0
15、;p復(fù)制構(gòu)造后,新創(chuàng)建的復(fù)制構(gòu)造后,新創(chuàng)建的dallasdallas 和和wichitawichita的狀態(tài)相同。的狀態(tài)相同。p復(fù)制構(gòu)造后,兩個(gè)對(duì)象是獨(dú)立復(fù)制構(gòu)造后,兩個(gè)對(duì)象是獨(dú)立 的對(duì)象,可獨(dú)立設(shè)置修改狀態(tài),的對(duì)象,可獨(dú)立設(shè)置修改狀態(tài), 與引用關(guān)系不同。與引用關(guān)系不同。-29-30-31-32-class Catpublic: Cat(int newAge = 1):age(newAge) howManyCats+; Cat() howManyCats-; int getAge() return age; void setAge(int newAge) age = newAge; stati
16、c int howManyCats;private: int age;int Cat:howManyCats = 0;p類中的類中的staticstatic數(shù)據(jù)成員只是一種聲明,需要在數(shù)據(jù)成員只是一種聲明,需要在 類外部(源程序文件中)進(jìn)行定義并初始化。類外部(源程序文件中)進(jìn)行定義并初始化。p靜態(tài)數(shù)據(jù)成員不能在構(gòu)造函數(shù)初始化部分初始化靜態(tài)數(shù)據(jù)成員不能在構(gòu)造函數(shù)初始化部分初始化-33-#include int main() const int maxCats = 5; Cat *catHousemaxCats; int i; for (i = 0; i maxCats; i+) catHous
17、ei = new Cat(i);pcatHousecatHouse是指針數(shù)組,每個(gè)元素為是指針數(shù)組,每個(gè)元素為1 1個(gè)個(gè)CatCat類型指針,類型指針, 指向堆中的指向堆中的1 1個(gè)個(gè)CatCat對(duì)象。對(duì)象。pnew Cat(i)new Cat(i)在堆中創(chuàng)建在堆中創(chuàng)建CatCat對(duì)象,初始化年齡為對(duì)象,初始化年齡為i i。實(shí)例化了實(shí)例化了5個(gè)個(gè)Cat對(duì)象,對(duì)象,并將它們存儲(chǔ)到一個(gè)數(shù)組并將它們存儲(chǔ)到一個(gè)數(shù)組中。調(diào)用中。調(diào)用Cat構(gòu)造函數(shù)構(gòu)造函數(shù)5次,因此變量次,因此變量howManyCats遞增到遞增到5-34-int main() for (i = 0; i maxCats; i+) std
18、:cout There are ; std:cout Cat:howManyCats; std:cout cats left!n; std:cout Deleting the one which is ; std:cout getAge(); std:cout years oldn; delete catHousei; catHousei = 0; return 0;p公有靜態(tài)數(shù)據(jù)成員,可以通過(guò)對(duì)象訪公有靜態(tài)數(shù)據(jù)成員,可以通過(guò)對(duì)象訪 問(wèn),也可以通過(guò)類名限定符直接訪問(wèn)問(wèn),也可以通過(guò)類名限定符直接訪問(wèn)-35-36-class Catpublic: Cat(int newAge = 1):age(n
19、ewAge) howManyCats+; Cat() howManyCats-; int gGetAge() return age; void setAge(int newAge) age = newAge; static int getHowMany() return howManyCats; private: int age; static int howManyCats;int Cat:howManyCats = 0;p靜態(tài)數(shù)據(jù)成員靜態(tài)數(shù)據(jù)成員howManyCatshowManyCats聲明為聲明為privateprivate,只能通過(guò),只能通過(guò) 公有靜態(tài)成員函數(shù)公有靜態(tài)成員函數(shù)getH
20、owManygetHowMany訪問(wèn)。訪問(wèn)。-37-#include void countCats() std:cout There are Cat:getHowMany() cats alive!n;p定義輔助函數(shù)定義輔助函數(shù)countCatscountCats,輸出當(dāng)前還存在的,輸出當(dāng)前還存在的CatCat對(duì)象。對(duì)象。p公有靜態(tài)成員函數(shù)通過(guò)公有靜態(tài)成員函數(shù)通過(guò)Cat:Cat:前綴訪問(wèn)。前綴訪問(wèn)。-38-int main() const int maxCats = 5; Cat *catHousemaxCats; int i; for (i = 0; i maxCats; i+) catHo
21、usei = new Cat(i); countCats(); for (i = 0; i maxCats; i+) delete catHousei; countCats(); return 0;-39-40-41-class Pointpublic:Point(int xx=0,int yy=0): x(xx), y(yy) int getX() const return x; int getY() const return y; friend double getDis(const Point &a,const Point &b);private:int x,y;p通過(guò)通過(guò)friendfriend將將getDisgetDis聲明為聲明為PointP
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 圍堰施工課題申報(bào)書(shū)
- 軟件測(cè)試申報(bào)書(shū)課題
- 課題申報(bào)書(shū)方案構(gòu)建模板
- 合伙企業(yè)人合同范本
- 單位買(mǎi)電合同范本
- 合同范本分包合同
- 課題申報(bào)書(shū)課題類型
- 特殊學(xué)生教育課題申報(bào)書(shū)
- 和單位購(gòu)銷采購(gòu)合同范本
- 品牌門(mén)窗店銷售合同范本
- 全省檢察機(jī)關(guān)公訴業(yè)務(wù)知識(shí)考試試卷
- 10KV開(kāi)關(guān)柜教學(xué)講解課件
- 損傷疼痛病(軟組織損傷)中醫(yī)臨床路徑
- 航模隊(duì)第一講-飛機(jī)基本原理和彈射機(jī)制作
- 花城版三年級(jí)下冊(cè)音樂(lè)教學(xué)計(jì)劃
- 全國(guó)計(jì)算機(jī)等級(jí)證書(shū)樣本
- 說(shuō)明書(shū)-prsPRS-7177電能質(zhì)量監(jiān)測(cè)裝置
- 知名房地產(chǎn)抖音代運(yùn)營(yíng)方案
- ISO 9001:2015新版質(zhì)量管理體系詳解與案例文件匯編
- 人教版八年級(jí)下冊(cè)英語(yǔ)全冊(cè)教案完整版教學(xué)設(shè)計(jì)含教學(xué)反思
- 五年級(jí)下冊(cè)數(shù)學(xué)課件 第1課時(shí) 因數(shù)和倍數(shù) 蘇教版(共25張PPT)
評(píng)論
0/150
提交評(píng)論