北郵C面向?qū)ο蟪绦蛟O(shè)計(jì)第三講.ppt_第1頁
北郵C面向?qū)ο蟪绦蛟O(shè)計(jì)第三講.ppt_第2頁
北郵C面向?qū)ο蟪绦蛟O(shè)計(jì)第三講.ppt_第3頁
北郵C面向?qū)ο蟪绦蛟O(shè)計(jì)第三講.ppt_第4頁
北郵C面向?qū)ο蟪绦蛟O(shè)計(jì)第三講.ppt_第5頁
已閱讀5頁,還剩42頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

C+面向?qū)ο蟪绦蛟O(shè)計(jì),2.3.4 成員函數(shù)的存儲方式,系統(tǒng)為每個對象分配內(nèi)存空間。由于同一類對象的成員函數(shù)相同,若為每個對象的成員函數(shù)都分配內(nèi)存空間,會造成內(nèi)存浪費(fèi)。 只為對象的數(shù)據(jù)成員分配內(nèi)存空間。類的所有對象共享一個成員函數(shù)空間,該空間不占用對象的存儲空間。,2.4 對象成員的引用,“引用”就是如何訪問對象中的成員。 有三種方法: 用對象名和成員運(yùn)算符訪問; 用指向?qū)ο蟮闹羔樤L問; 用對象的引用訪問。,2.4.1 用對象名和成員運(yùn)算符訪問,格式:對象名. 成員名 # include using namespace std; class point private: int x, y; public: void setpoint(int a, int b) x = a; y =b; int getx() return x; int gety() return y; ;,int main() point op1, op2; int i, j; op1.setpoint(1,2); op2.setpoint(3,4); i = op1.getx(); j = op1.gety(); cout“op1 i=” i“opt1 j=”jendl; i = op2.getx(); j = op2.gety(); cout“op2 i=” i“opt2 j=”jendl; ,2.4.2 用指向?qū)ο蟮闹羔樤L問,可以通過指針訪問對象中的成員 class Time public: int hour; int minute; ; Time t, *p; p= ,2.5 類和對象的應(yīng)用舉例,例 2.1 簡單的例子 # include using namespace std; class Time public: int hour; int minute; int sec; ; int main() Time t1; cint1.hour; cint1.minute; cint1.sec; coutt1.hour“:“t1.minute“:“t2.secendl; ,例2.1 引用多個對象的成員 (1)程序(a) # include using namespace std; class Time public: int hour; int minute; int sec; ; int main() Time t1; cint1.hour t1.minutet1.sec; coutt2.hourt2.minutet2.sec; coutt2.hour“:“t2.minute“:“t2.secendl; return 0; ,(2)程序(b) # include using namespace std; class Time public: int hour; int minute; int sec; ; int main() void set_time(Time ,void set_time(Time ,(2)程序(c) # include using namespace std; class Time public: int hour; int minute; int sec; ; int main() void set_time(Time ,void set_time (Time ,例2.3 用成員函數(shù)處理例2.2中的程序 # include using namespace std; class Time public: void set_time(); void show_time(); private: int hour; int minute; int sec; ;,int main( ) Time t1; t1.set_time( ); t1.show_time( ); Time t2; t2.set_time(); t2.show_time(); return 0; void Time:set_time() cinhour; cinminute; cinsec; void Time:show_time() couthour“:“minute“:“secendl; ,例2.4 在整型數(shù)組中找最大值 # include using namespace std; class Array_max public: void set_value(); void max_value(); void show_value(); private: int array10; int max; ; void Array_max:set_value() int i; for (i=0; iarrayi; ,void Array_max:max_value() int i; max=array0; for (i=1; imax) max=arrayi; void Array_max:show_value() cout“max=“max; int main() Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return 0; ,2.6 類的封裝性和信息隱蔽,C+ 通過類實(shí)現(xiàn)封裝性。 在聲明類時,一般將數(shù)據(jù)成員指定為私有,使它們與外界隔離,把讓外界調(diào)用的成員函數(shù)指定為公有,外界通過公有函數(shù)實(shí)現(xiàn)對數(shù)據(jù)成員的訪問。 外界與對象的唯一聯(lián)系就是調(diào)用公有成員函數(shù)。公有成員函數(shù)是用戶使用類的公用接口。 通過成員函數(shù)對數(shù)據(jù)成員進(jìn)行操作稱為類的實(shí)現(xiàn)。,信息隱蔽的好處: (1)如想修改或擴(kuò)充類的功能,只需修改類中有關(guān)的數(shù)據(jù)成員和成員函數(shù),類外的部分不用修改。 (2)如果在編譯時發(fā)現(xiàn)類中的數(shù)據(jù)讀寫有錯,不必檢查整個程序,只需檢查本類中訪問這些數(shù)據(jù)的成員函數(shù)。,類聲明和成員函數(shù)定義的分離,在面向?qū)ο蟪绦蜷_發(fā)中,一般將類的聲明(包括成員函數(shù)的聲明)放在指定的頭文件中,在程序中只要把有關(guān)的頭文件包含進(jìn)來即可,不必在多個程序中重復(fù)寫類的聲明。 為了實(shí)現(xiàn)信息隱蔽,一般類成員函數(shù)的定義不放在頭文件中,而另外放在一個文件中。,/ student.h 頭文件,在此聲明類 # include using namespace std; class Student int num; string name; char sex; public: void display( ); ; / student.cpp 文件定義函數(shù) # include # include “student.h“ void Student : display() cout num endl; cout name endl; cout sex endl; ,/ main.cpp 定義主函數(shù) # include # include “student.h“ using namespace std; int main() Student stud; stud.display(); return 0; ,作業(yè)題目(10月11日提交): 68頁 第5題和第6題 。 5 、將本章的例2.4改寫為一個多文件的程序: (1)將類定義放到頭文件arraymax.h中; (2)將成員函數(shù)定義放在源文件arraymax.cpp中; (3)主函數(shù)放在源文件file1.cpp中。 6、求3個長方體的體積,編寫一個基于對象的程序。數(shù)據(jù)成員包括length(長)、width(寬)、 height(高)。要用成員函數(shù)實(shí)現(xiàn)以下功能: (1)由鍵盤分別輸入3個長方體的長、寬、高; (2)計(jì)算長方體的體積; (3)輸出3個長方體的體積。,第3章 類和對象的進(jìn)一步討論,3.1 構(gòu)造函數(shù),如果定義一個變量,在使用這個變量前要對其賦初始值,否則其內(nèi)的值不定。 對象作為變量,也需要對其中的數(shù)據(jù)成員賦初始值。這通過構(gòu)造函數(shù)來實(shí)現(xiàn)。 構(gòu)造函數(shù)是一種特殊的成員函數(shù),不需要調(diào)用,而是在創(chuàng)建對象時自動執(zhí)行。,構(gòu)造函數(shù)屬于某一個類,可由系統(tǒng)自動生成,也可以由程序員編寫。 構(gòu)造函數(shù)名必須與類名同名。 構(gòu)造函數(shù)沒有類型,不返回值。 構(gòu)造函數(shù)可以帶參數(shù),也可以不帶參數(shù)。 構(gòu)造函數(shù)可以寫在類內(nèi)部,也可以寫在類外部。 構(gòu)造函數(shù)即使被聲明為公有函數(shù),也不能被顯示地調(diào)用。,例3.1在例2.3的基礎(chǔ)上定義構(gòu)造成員函數(shù) # include using namespace std; class Time public: Time( ) hour=0; minute=0; sec=0; void set_time(); void show_time(); private: int hour; int minute; int sec; ;,void Time: set_time() cin hour; cin minute; cin sec; void Time:show_time() cout hour “:“minute“:“secendl; int main( ) Time t1; t1.set_time(); t1.show_time(); Time t2; t2.show_time(); return 0; ,程序運(yùn)行的情況為: 10 25 54 10:25:54 / 輸出t1的值 0: 0: 0 / 輸出t2的值,3.1.3 帶形參數(shù)的構(gòu)造函數(shù),為不同的對象賦不同的初始值,可用帶參數(shù)的構(gòu)造函數(shù)。 構(gòu)造函數(shù)首部格式: 構(gòu)造函數(shù)名 (類型1 形參1,類型2 形參2,. ) 定義對象的格式: 類名 對象名(實(shí)參1,實(shí)參2,); 或 類名 *指針變量 = new 類名 (實(shí)參1,實(shí)參2,);,例 3.2 有兩個長方柱,其長、寬、高分別為:(1)12,25,30; (2)15,30,21;計(jì)算它們的體積。 # include using namespace std; class Box public: Box(int, int, int); int volume(); private: int height; int width; int length; ; Box:Box(int h, int w, int len) / 構(gòu)造函數(shù) height=h; width=w; length=len; ,int Box:volume() / 計(jì)算長方體的體積 return (height*width*length); int main() Box box1(12, 25, 30); / 定義對象box1 cout “box1體積=“ box1.volume() endl; Box box2(15, 30, 21); / 定義對象box2 cout “box2體積=“ box2.volume() endl; return 0; 程序運(yùn)行的結(jié)果如下: box1體積= 9000 box2體積= 9450,int main() Box *A = new Box(12, 25, 30); cout volume() volume() endl; return 0; ,3.1.4 用參數(shù)初始化表對數(shù)據(jù)成員初始化,該方法在函數(shù)的首部實(shí)現(xiàn)數(shù)據(jù)成員的初始化。 定義構(gòu)造函數(shù)的格式: 函數(shù)名 (類型1 形參1,類型2 形參2,): 成員名1(形參1), 成員名2(形參2), 定義對象的格式: 類名 對象名( 實(shí)參1,實(shí)參2, );,例: Box:Box(int h, int w, int len): height (h), width(w), length(len) 定義對象: Box box1(12,25,30); Box box2(15,30,21);,3.1.5 構(gòu)造函數(shù)的重載,構(gòu)造函數(shù)可以重載。一個類可以有多個同名構(gòu)造函數(shù),函數(shù)參數(shù)的個數(shù)、參數(shù)的類型各不相同。 例3.3:在例3.2的基礎(chǔ)上,定義兩個構(gòu)造函數(shù),其中一個無參數(shù),另一個有參數(shù)。 # include using namespace std; class Box public: Box(); / 默認(rèn)構(gòu)造函數(shù) Box(int h, int w, int len): height(h), width(w), length(len) int volume(); private: int height; int width; int length; ;,Box:Box( ) height=10; width=10; length=10; int Box:volume( ) return(height*width*length); int main() Box box1; cout“box1 體積= “box1.volume()endl; Box box2 (15,30,25); cout“box2 體積= “box2.volume()endl; return 0; ,3.1.6 使用默認(rèn)參數(shù)值的構(gòu)造函數(shù),構(gòu)造函數(shù)可為形參指定默認(rèn)值,若創(chuàng)建對象時未給出實(shí)參,則以默認(rèn)值為形參賦值。 格式: 函數(shù)名 (類型 形參1=常數(shù),類型 形參2=常數(shù) , );,例3.4:將例3.3中的構(gòu)造函數(shù)改用帶默認(rèn)值的參數(shù),長、寬、高的默認(rèn)值都是10。 # include using namespace std; class Box public: Box(int w=10, int h=10, int len=10); int volume(); private: int height; int width; int length; ; Box:Box(int w, int h, int len) height=h; width=w; length=len; ,int Box:volume( ) return(height*width*length); int main() Box box1; cout“box1 體積= “box1.volume()endl; Box box2(15); cout“box2 體積 “box2.volume()endl; Box box3(15,30); cout“box3 體積 “box3.volume()endl; Box box4(15,30,20); cout“box4 體積“box4.volume()endl; return 0; ,程序運(yùn)行結(jié)果為: box1 體積=1000 box2 體積=1500 box3 體積=4500 box4 體積=9000,為了避免歧義,不允許同時定義不帶形參的構(gòu)造函數(shù)和全部形參都指定默認(rèn)值的構(gòu)造函數(shù)。 Box(); Box(int h=10, int w=10, int len=10); 同樣為了避免歧義性,如定義了全部形參帶默認(rèn)值的構(gòu)造函數(shù)后,一般不要再定義重載構(gòu)造函數(shù)。反之亦然。 Box(int h=10, int w=10, int len=10); Box(); Box(int, int);,3.2 析構(gòu)函數(shù),析構(gòu)函數(shù)與構(gòu)造函數(shù)作用相反。當(dāng)對象生命周期結(jié)束時,系統(tǒng)自動調(diào)用析構(gòu)函數(shù)。 執(zhí)行析構(gòu)函數(shù)的時機(jī): 一個函數(shù)內(nèi)定義的對象,當(dāng)這個函數(shù)結(jié)束時,對象應(yīng)該釋放,在對象釋放前自動執(zhí)行析構(gòu)函數(shù)。 static局部對象要到main函數(shù)結(jié)束或執(zhí)行exit命令時才自動執(zhí)行析構(gòu)函數(shù)。 全局對象(在函數(shù)外定義的對象),當(dāng)main函數(shù)結(jié)束或執(zhí)行exit命令時自動執(zhí)行析構(gòu)函數(shù)。 用new建立的動態(tài)對象,當(dāng)用delete運(yùn)算符釋放該對象時,自動執(zhí)行析構(gòu)函數(shù)。,析構(gòu)函數(shù)的特征: 析構(gòu)函數(shù)名以符號開始后跟類名。 析構(gòu)函數(shù)沒有數(shù)據(jù)類型、返回值、形參。一個類只有一個析構(gòu)函數(shù)。 如果程序員沒有定義析構(gòu)函數(shù),C+編譯系統(tǒng)會自動生成一個析構(gòu)函數(shù)。,例3.5 包含構(gòu)造函數(shù)和析構(gòu)函數(shù)的C+程序 # include # include using namespace std; class Student public: Student (int n, string nam, char s) num=n; name=nam

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論