




已閱讀5頁,還剩61頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
面向?qū)ο蟪绦蛟O(shè)計 講義 第7章 7 1C 類的構(gòu)成關(guān)鍵字 class例如 classpoint intx y public voidsetpoint int int 第7章類的定義及其類對象的封裝性 7 1 1私有成員和公有成員1 私有成員a 關(guān)鍵字 private 有時可省略 b 訪問權(quán)限 只限于通過成員函數(shù)來訪問 也就是說只有類本身能夠訪問它 任何類以外的函數(shù)對私有數(shù)據(jù)的訪問都是非法的 2 公有成員a 關(guān)鍵字 public 不能省略 b 訪問權(quán)限 允許類的使用者來訪問它 7 1 2類的構(gòu)造 7 2成員函數(shù)的定義例如 前面定義的point類 其成員函數(shù)的定義為 voidpoint setpoint intvx intvy 定義成員函數(shù)時的注意事項(xiàng) 1 在所定義的成員函數(shù)之前應(yīng)綴上類名 在類名和函數(shù)之間加上分隔符 2 函數(shù)的返回類型一定要與函數(shù)聲明的類型相匹配 3 在定義成員函數(shù)時 應(yīng)同時給出參量類型和參量名 內(nèi)置函數(shù)的定義方式 1 隱式定義 2 顯式定義 最前面加關(guān)鍵字 inline 7 3類與對象7 3 1類與對象的關(guān)系類與對象的關(guān)系 整型int和整型變量i的關(guān)系創(chuàng)建對象的兩種方法 1 在定義類的同時創(chuàng)建對象例如 classdata tt 2 在使用時定義對象 常用 datatt 7 3 2類的使用例如 classpoint intx y public voidsetpoint intvx intvy x vx y vy voidf1 pointp1 p1 setpoint 10 10 voidf2 pointp1 point p2 p2 p2 setpoint 10 10 7 3 3名字解析classrealset 定義一個實(shí)數(shù)集合類intcard floatelems 16 public voidprint classintset 定義一個整數(shù)集合類intcard intelems 16 public voidprint voidf intsetis realsetrs is print 調(diào)用的是intset中的print 函數(shù)rs print 調(diào)用的是realset中的print 函數(shù) 7 4構(gòu)造函數(shù)與析構(gòu)函數(shù)構(gòu)造函數(shù)和析構(gòu)函數(shù)的特點(diǎn) 1 它們都沒有返回值 定義時不需指出類型 2 它們不能被繼承 3 構(gòu)造函數(shù)可以有缺省參數(shù) 4 析構(gòu)函數(shù)可以是虛的 virtual 但構(gòu)造函數(shù)不行 5 不可取它們的地址 6 不能用常規(guī)調(diào)用方法調(diào)用構(gòu)造函數(shù) 當(dāng)使用完全限定名 帶對象名 類名和函數(shù)名 時可以調(diào)用析構(gòu)函數(shù) 7 當(dāng)定義對象時 編譯程序自動調(diào)用構(gòu)造函數(shù) 當(dāng)刪除對象時 編譯程序自動地調(diào)用析構(gòu)函數(shù) 7 4 1構(gòu)造函數(shù)定義構(gòu)造函數(shù)應(yīng)注意的問題 1 構(gòu)造函數(shù)的名字必須與類名相同 2 構(gòu)造函數(shù)沒有返回值 在聲明和定義時不能說明它的類型 3 構(gòu)造函數(shù)的功能是對對象進(jìn)行初始化 初始化的構(gòu)造函數(shù)數(shù)據(jù)成員一般均為私有成員 構(gòu)造函數(shù)舉例 includeclassqueue 定義定義一個整型隊(duì)列類intq 100 用數(shù)組q來存放隊(duì)列中的各個元素intsloc rloc sloc表示隊(duì)列尾 rloc表示對列頭public queue 聲明構(gòu)造函數(shù)voidqput inti 往隊(duì)列中增加元素的函數(shù)intqget 從隊(duì)列中提取元素的函數(shù) queue queue sloc rloc 0 給隊(duì)列類的兩個私有變量賦初值cout queueinitialized n 在屏幕上輸出對象已 初始化的信息 voidqueue qput inti if sloc 100 若sloc為100 表明隊(duì)列已滿 cout queueisfull n return sloc q sloc i 在隊(duì)列尾后追加隊(duì)列元素i intqueue qget if rloc sloc 若隊(duì)列頭和隊(duì)列尾相交 隊(duì)列中無元素 cout queueisempty n return0 rloc 將隊(duì)列頭標(biāo)識后移 取出第一個元素returnq rloc main queuea b 定義queue的兩個對象a和ba qput 10 往兩個隊(duì)列對象中追加元素 b qput 20 a qput 20 b qput 19 cout a qgrt 將兩個隊(duì)列對象中的元素取出 顯示在屏幕上cout a qgrt n cout b qgrt cout b qgrt n return1 程序運(yùn)行結(jié)果為 queueinitializedqueueinitialized10202019 7 4 2參數(shù)化的構(gòu)造函數(shù)例如 classpoint intx y public point intvx intvy 聲明帶參量的構(gòu)造函數(shù)voidoffset intax intay point point intvx intvy x vx 用傳遞進(jìn)來的參數(shù)對私有量賦x y初值y vy voidpoint offset intax intay x x ax 對私有變量增值y y ay main pointpt1 10 20 定義對象 并給構(gòu)造函數(shù)傳遞參數(shù)pt1 offset 10 10 7 4 3缺省參數(shù)的構(gòu)造函數(shù)例如 classpoint intx y public point intvx 0 intvy 0 x vx y vy main pointp1 不傳遞參數(shù) 全部用缺省值pointp2 10 只傳遞一個參數(shù)pointo3 10 20 傳遞兩個參數(shù) 在函數(shù)所帶的參數(shù)中 若有一部分缺省 則所有取缺省的參數(shù)必須出現(xiàn)在不取缺省值的參數(shù)的右邊 例如 1 point intx inty 0 正確 2 point intx 0 inty 錯誤7 4 4多構(gòu)造函數(shù)例如 classx public x x int x int char x float char 注意事項(xiàng) 在使用缺省參數(shù)的構(gòu)造函數(shù)時 要避免二義性 例如 classx public x x inti 0 main xone 10 xtwo 7 4 5拷貝構(gòu)造函數(shù)特殊功能 將參數(shù)代表的對象逐域拷貝到新創(chuàng)建的對象中 拷貝構(gòu)造函數(shù)的兩種定義形式 1 系統(tǒng)產(chǎn)生 2 用戶定義格式 x x constx x為類名 參數(shù)類型為x 例如 classpoint intx y public point intvx intvy x vx y vy point constpoint 7 4 6動態(tài)存儲new的優(yōu)點(diǎn) 1 new自動計算要分配的類型的大小 2 能自動返回正確的指針類型 不必對返回指針進(jìn)行類型轉(zhuǎn)換 3 可以用new將分配的對象初始化 1 new和delete的語法 1 new的語法格式 名字指針 new名字 名字初始化值 例如 int s s newint 200 用new分配數(shù)組 int p newint 10 int q newint 2 3 5 正確int q newint 3 5 錯誤 2 delete的語法格式 delete名字指針 ints newint 200 deletes 2 new和delete的使用例1動態(tài)分配和釋放存儲區(qū) includemain int p newint if p cout allocationfailure n return0 p 20 cout p deletep return1 例2動態(tài)分配并初始化存儲區(qū) includemain int p newint 99 if p cout allocationfailure n return0 cout p deletep return1 例3動態(tài)分配數(shù)組 includemain double s newdouble 10 inti if s cout allocationfailure n return0 for i 0 i 10 i s i 100 00 i for i 0 i 10 i cout s i delete s return1 例4為結(jié)構(gòu)類型的變量動態(tài)分配存儲 includestructperson char name intage main person p p newperson if p coutname newchar 20 p name Smart p age 23 coutnameage deletep return1 例5為對象動態(tài)分配存儲 includeclasspoint intx y public point intvx 0 intvy 0 x vx y vy voidprint cout x y n main point p1 p1 newpoint 10 20 if p coutprint deletep1 return1 例6為對象數(shù)組動態(tài)分配存儲區(qū) includeclasspoint intx y public point intvx 16 intvy 16 x vx y vy voidprint cout x y n main point p1 定義一個指向point類對象的指針p1 newpoint 10 為point類對象數(shù)組分配存儲 if p1 并將頭指針返回給p1 cout allocationfailure n return0 for inti 0 i 10 i 訪問對象數(shù)組中每一個對象p1 i print 調(diào)用對象的成員函數(shù)delete p1 釋放對象數(shù)組所占的存儲return1 對象數(shù)組初始化的兩種方法 1 在所定義的類中不定義構(gòu)造函數(shù) 而定義一個成員函數(shù)專門用來完成初始化功能 對象數(shù)組被創(chuàng)建以后 可以通過調(diào)用此成員函數(shù)來對對象數(shù)組中的元素進(jìn)行初始化 2 在所定義的類中增加不帶參數(shù)的構(gòu)造函數(shù)或帶缺省參數(shù)的構(gòu)造函數(shù) 7 4 7析構(gòu)函數(shù)1 析構(gòu)函數(shù)的定義 析構(gòu)函數(shù)名字與類名相同 在前面加長一個符號 2 舉例 includeclassstring intlength char contents public string char s string string string char s 定義構(gòu)造函數(shù) if s length strlen s contents newchar length 1 為字符串分配存儲strcpy contents s 字符串之間賦值 else length 0 contents 0 string string 定義析構(gòu)函數(shù) deletecontents 釋放字符串contents所占存儲 創(chuàng)建字符串對象 strings1 7 5靜態(tài)成員7 5 1靜態(tài)數(shù)據(jù)成員1 關(guān)鍵字 static2 初始化 靜態(tài)數(shù)據(jù)的初始化是在類外進(jìn)行的 類型類名 靜態(tài)數(shù)據(jù)成員 初值 3 舉例 include includeclasscounter 定義一個counter類staticintcount 靜態(tài)數(shù)據(jù)定義 表示共創(chuàng)建的對象數(shù)intobjnumber 用來表示對象號public 定義構(gòu)造函數(shù)counter count 每創(chuàng)建一個對象 對象數(shù)加1objnumber count 給當(dāng)前對象號賦值 voidshow cout obj objnumber cout count count n intcounter count 0 給靜態(tài)數(shù)據(jù)初始化main counterobj1 創(chuàng)建第一個對象obj1obj1 show 顯示對象號和當(dāng)前對象數(shù)cout n counterobj2 創(chuàng)建第二個對象obj2obj1 show 顯示當(dāng)前存在的二個對象obj2 show 的數(shù)據(jù)成員cout n counterobj3 創(chuàng)建第三個對象obj3obj1 show obj2 show obj3 show cout n counterobj4 創(chuàng)建第四個對象obj4obj1 show obj2 show obj3 show obj4 show cout n 運(yùn)行結(jié)果 obj1count 1 obj1count 2obj2count 2 obj1count 3obj2count 3obj3count 3 obj1count 4obj2count 4obj3count 4obj4count 4 obj1count 4obj2count 4obj3count 4obj4count 4 7 5 2靜態(tài)成員函數(shù)1 關(guān)鍵字 static2 注意事項(xiàng) 1 使用時要指出它作用在哪個對象上 或用 類名 作限定詞 2 不能隨意地訪問對象中的非靜態(tài)的數(shù)據(jù)成員 舉例說明如下 include includeclassstring 定義一個字符串類staticinttotal length 定義一個靜態(tài)變量 用來存 放所有字符串的總長度intlength 定義存放此字符串長度變量char contents 定義變量 用來存放字符串內(nèi)容public string char s 定義構(gòu)造函數(shù)length strlen s 取字符串的長度 contents newchar length 1 為字符串分配存儲strcpy contents s 字符串拷貝staticintset total length 總長度函數(shù)total length total length length 該語句有問題 returntotal length string 析構(gòu)函數(shù) delete contents 定義變量 用來存放字符串內(nèi)容 intstring total length 0 給靜態(tài)變量賦初值main stringobj1 TheFirstObject cout obj1 set total length n 算總長度 并輸出結(jié)果stringobj2 TheFirstObject cout obj1 set total length n 算總長度 并輸出結(jié)果return1 靜態(tài)成員函數(shù)改為 staticintset total length stringobj total length total length obj length returntotal length main stringobj1 TheFirstObject cout obj1 set total length obj1 n 算總長度 并輸出stringobj2 TheFirstObject cout obj1 set total length obj2 n 算總長度 并輸出return1 3 this指針當(dāng)一個對象接收到一個消息時 系統(tǒng)會自動地向它傳遞一個隱含的 指針 參數(shù) 該指針稱為 this 指針 舉例說明如下 classLinear List int List 指向表頭的指針unsignednMax 表的最大長度unsignednElem 表中實(shí)際元素個數(shù)public voidinit intn 10 List newint n nMax n nElem 0 intElem int 將元素置入表尾 成功返回非零值 intLinear List Elem intelem if nElem nMax List nElem elem returnnElem return0 表已滿 返回零 voidmain Linear Listlist list init 20 for inti 1 inElemnMax this List this Elem elem returnthis nElem return0 表已滿 返回零 this指針有如下的缺省說明 Linear List constthis 當(dāng)程序執(zhí)行語句 list Elem i 時 系統(tǒng)對this進(jìn)行了如下的缺省賦值 this this指針的特殊用途 為了將一個線性表賦給另一個線性表 可以為類增加一個賦值成員函數(shù) voidAssign Linear List 7 6類對象作為數(shù)據(jù)成員注意事項(xiàng) 類的內(nèi)部對象如何初始化例1 includeclassinner class intx public inner class intz 定義構(gòu)造函數(shù) x z voidwrite print d n x classouter class inty inner classx x r為成員對象inner classr public outer class intz 聲明構(gòu)造函數(shù)voidwrite print d n y voidwrite inner x 由成員對象x調(diào)用 x inner class write 調(diào)用inner class中voidwrite inner r 成員函數(shù)write r inner class write outer class outer class intz x 20 r 36 定義outer class類的構(gòu)造函數(shù) y z main outer classobj 10 obj write inner x 顯示對象成員x中的私有成員obj write inner r 顯示對象成員r中的私有成員obj write 顯示對象obj的私有成員 7 7對象數(shù)組注意事項(xiàng) 因?yàn)樵诙x對象數(shù)組時不能不給構(gòu)造函數(shù)傳遞參數(shù) 所以在類中必須有一個不帶參數(shù)的構(gòu)造函數(shù)或帶缺省參數(shù)的構(gòu)造函數(shù) 舉例如下
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 寵物領(lǐng)取活動方案
- 客戶上門關(guān)愛活動方案
- 客戶答謝宴活動方案
- 客服經(jīng)理活動方案
- 有關(guān)賣花的題目及答案
- 宣講部活動策劃方案
- 室內(nèi)小游戲活動方案
- 室內(nèi)自由活動方案
- 室外宣傳活動方案
- 憲法表白活動方案
- 園藝設(shè)施的規(guī)劃設(shè)計與建設(shè)
- 2023年副主任醫(yī)師(副高)-衛(wèi)生管理(副高)考試歷年真題摘選帶答案
- 湖北省天門市(古稱竟陵縣)東鄉(xiāng)(干一鎮(zhèn)附近)江州義門陳
- 輸電線路風(fēng)偏計算基本方法
- 2023年江蘇南京江北新區(qū)第二批招考聘用編制內(nèi)教師100人筆試題庫含答案解析
- 部編八下語文游記閱讀訓(xùn)練題語文八年級下冊能力訓(xùn)練(部編版)
- 馬鞍山市潔源環(huán)保有限公司馬鞍山市一般工業(yè)固廢填埋場項(xiàng)目重新報批環(huán)境影響報告書
- 通信線路投標(biāo)文件
- 集結(jié)號觀后感 集結(jié)號觀后感500字(最全)
- (完整版)全國各省份城市明細(xì)表
- 《“將軍飲馬”問題》說課稿
評論
0/150
提交評論