實(shí)驗(yàn)四繼承與派生_第1頁
實(shí)驗(yàn)四繼承與派生_第2頁
實(shí)驗(yàn)四繼承與派生_第3頁
實(shí)驗(yàn)四繼承與派生_第4頁
實(shí)驗(yàn)四繼承與派生_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、實(shí)驗(yàn)四派生類與繼承【實(shí)驗(yàn)類型】驗(yàn)證性實(shí)驗(yàn)【實(shí)驗(yàn)課時(shí)】2 學(xué)時(shí)【實(shí)驗(yàn)?zāi)康摹?1)理解類的繼承的概念,能夠定義和使用類的繼承關(guān)系。(2)掌握派生類的聲明與定義方法。(3)熟悉公有派生和私有派生的訪問特性。(4)學(xué)習(xí)虛基類在解決二義性問題中的作用?!緦?shí)驗(yàn)環(huán)境】硬件:計(jì)算機(jī)軟件:Microsoft Visual C+ 6.0【實(shí)驗(yàn)內(nèi)容】1、按要求閱讀、編寫、調(diào)試和運(yùn)行以下程序。(1)實(shí)驗(yàn)內(nèi)容1定義一個(gè)基類 MyArray,基類中可以存放一組整數(shù)。class MyArraypublic:MyArray(i nt len g);MyArray();void In put();void Display。;p

2、rotected:long int *alist;/指向動(dòng)態(tài)申請(qǐng)的一組空間int length;/整數(shù)的個(gè)數(shù)基類中有構(gòu)造函數(shù)、析構(gòu)函數(shù)、輸入數(shù)據(jù)和輸出數(shù)據(jù)的函數(shù)。2定義一個(gè)類 SortArray 繼承自 MyArray,在該類中定義函數(shù)實(shí)現(xiàn)排序功能。3定義一個(gè)類 ReArray 繼承自 MyArray,在該類中定義函數(shù)實(shí)現(xiàn)逆轉(zhuǎn)功能。4定義一個(gè)類 AverArray 繼承自 MyArray,在該類中定義函數(shù) Aver 求解整數(shù)的平均值。5定義 NewArray 類,同時(shí)繼承了 SortArray, ReArray 和 AverArray,使得 NewArray 類的對(duì)象同時(shí)具有排序、逆轉(zhuǎn)、和求平

3、均值的功能。在繼承的過程中聲明為虛基類,體會(huì)虛基類在解決二義性問題中的作用。(2)實(shí)驗(yàn)程序(參考)程序如下:#i nclude iostream.h#in clude process.hclass MyArraypublic:MyArray(i nt le ng);MyArray();void In put();void Display。;protected:long int *alist;/指向動(dòng)態(tài)申請(qǐng)的一組空間int length;/整數(shù)的個(gè)數(shù);MyArray:MyArray(i nt leng)len gth=le ng;alist=new long in tle ngth;if(ali

4、st=NULL)cout對(duì)不起,創(chuàng)建失敗。請(qǐng)重試。;exit(1);MyArray:MyArray()delete alist;cout數(shù)組被清空。endl;void MyArray:Display() / 顯示數(shù)組內(nèi)容int i;long int *p=alist;for (i=0;ile ngth;i+,p+)cout *p;void MyArray:Input()/ 從鍵盤若干整數(shù)cout請(qǐng)輸入:length 個(gè)整數(shù):;int i;long int *p=alist;for(i=0;ile ngth;i+,p+)ci n*p;class SortArray: virtual public

5、 MyArrayprivate:int len;long int *sp;public:SortArray(i nt le ng):MyArray(le ng)Ien=leng;Sort();;void Sort() sp=new long in tle n; long int q;sp=alist;for(i nt i=0;ile n; i+) for(i nt j=O;j*(sp+j+1)q=*(sp+j);*(sp+j)=*(sp+j+1); *(sp+j+1)=q;;class ReArray: virtual public MyArray/這里是虛基類,public:void Rev

6、erse()rp=new long in tle n;long int q;rp=alist;for(i nt i=0;ile n/2;i+)q=* (rp+i);*(rp+i)=*(rp+le n-i-1); *(rp+le n_i_1)=q;ReArray(i nt len g):MyArray(le ng) len=leng;Reverse();private:int len; long int *rp;;class AverArray:virtual public MyArray/這里是虛基類,public:double Aver()ap=new long in tle n;doubl

7、e q=0;ap=alist;for(i nt i=0;ile n;i+)q=q+*ap;ap+;q=q/le n;return q;AverArray(i nt le ng):MyArray(le ng)len=leng;private:in t le n;long int *ap;class NewArray:public ReArray,public SortArray,public AverArray public:NewArray(i nt len g);NewArray();NewArray:NewArray(i ntle ng):MyArray(le ng),SortArray(

8、le ng),ReArray(le ng),AverArray(le ng)coutn 新數(shù)組正在創(chuàng)建。n;NewArray:NewArray()coutn 新數(shù)組已被清空。n;void mai n()char b; int leng; docout請(qǐng)輸入數(shù)組長度: le ng;while(le ng=0) cout le ng; coutn 開始:n; NewArray n (le ng);n.ln put();coutn 您輸入的數(shù)組為:e ndl;n.Display();/ 顯示數(shù)組n.Reverse。;/ 顯示逆序coutn 倒序數(shù)組為:e ndl;n.Display。;/顯示逆轉(zhuǎn)以前

9、的情況coutn 平均值是: n. Aver()e ndl;/ 求平均值 n.Sort();/ 排序coutn 排序后(從小到大)數(shù)組為: e ndl;n.Display。;/顯示排序以后的情況coutnA繼續(xù) Q退出 b;while(b=A|b=a);執(zhí)行結(jié)果為:2、編寫一個(gè)學(xué)生和教師數(shù)據(jù)輸入和顯示程序。(1) 實(shí)驗(yàn)內(nèi)容編寫學(xué)生和教師數(shù)據(jù)輸入和顯示程序,學(xué)生數(shù)據(jù)有編號(hào)、班號(hào)和成績,教師數(shù)據(jù)有編號(hào)、職稱和部門。要求將編號(hào)、姓名輸入和顯示設(shè)計(jì)成一個(gè)類person ,并作為學(xué)生數(shù)據(jù)操作類 student 和教師數(shù)據(jù)操作類 teacher 的基類。(2) 實(shí)驗(yàn)程序(參考)#in cludeclass

10、 pers onprotected: int m;char A20; char *n ame;public:void in put()cout編號(hào):;cinm;cout A;name=&A0;void display。cout編號(hào):mendl; cout : nameendl;class stude nt:public pers onprotected:int class num, mark;public:void in put1()cout輸入一個(gè)學(xué)生數(shù)據(jù):endl;in put();cout class num;cout mark;void display1()cout顯示一個(gè)學(xué)生

11、的數(shù)據(jù) :endl; display();cout班號(hào):classnumendl; cout成績:markendl;class teacher: public pers onprotected:char zhiche ng20,bume n20;char *p;public:void in put2()cout顯示一個(gè)老師的數(shù)據(jù) :endl;in put(); cout zhiche ng; cout bume n;void display2()cout顯示一個(gè)老師的數(shù)據(jù) :endl; display();p=& zhiche ng0;cout職稱:pendl;p=&bume

12、n 0;cout部門:pendl;void mai n()stude nt S;teacher T;S. i nput1();T. i nput2();S. display1();T. display2();【實(shí)驗(yàn)提示】繼承是面向?qū)ο蟪绦蛟O(shè)計(jì)的一個(gè)重要特性,它允許在已有類的基礎(chǔ)上創(chuàng)建新的類,新類可以從一個(gè)或多個(gè)既有類中繼承函數(shù)和數(shù)據(jù),而且可以重新定義或加進(jìn)新的數(shù)據(jù)和函數(shù),從而形成類的層次或等級(jí)。 實(shí)驗(yàn) 1 中著重時(shí)在這方面進(jìn)行練習(xí),除了需要熟練掌握派生類的聲明和定義,還需要靈活運(yùn)用公有派生和私有派生來解決實(shí)際問題。虛基類用于想把公共的基類只產(chǎn)生一個(gè)拷貝時(shí),它的初始化與一般的多繼承的初始化在語法上是一樣的,但是構(gòu)造函數(shù)的調(diào)用順序不同。C+編程中派生類可以被基類指針引用,這叫向后兼容,可以提高程序的可擴(kuò)充性和可維護(hù)性。以前寫

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論