




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗四 派生類與繼承【實驗類型】驗證性實驗 【實驗課時】2學(xué)時 【實驗?zāi)康摹浚?) 理解類的繼承的概念,能夠定義和使用類的繼承關(guān)系。(2) 掌握派生類的聲明與定義方法。(3) 熟悉公有派生和私有派生的訪問特性。(4) 學(xué)習(xí)虛基類在解決二義性問題中的作用。【實驗環(huán)境】硬件:計算機軟件:Microsoft Visual C+ 6.0【實驗內(nèi)容】1、按要求閱讀、編寫、調(diào)試和運行以下程序。(1)實驗內(nèi)容定義一個基類MyArray,基類中可以存放一組整數(shù)。class MyArraypublic: MyArray(int leng);
2、 MyArray(); void Input(); void Display();protected:long int *alist; / 指向動態(tài)申請的一組空間int length; / 整數(shù)的個數(shù)基類中有構(gòu)造函數(shù)、析構(gòu)函數(shù)、輸入數(shù)據(jù)和輸出數(shù)據(jù)的函數(shù)。定義一個類SortArray繼承自MyArray ,在該類中定義函數(shù)實現(xiàn)排序功能。定義一個類ReArray繼承自MyArray ,在該類中定義函數(shù)實現(xiàn)逆轉(zhuǎn)功能。定義一個類AverArray繼承自MyAr
3、ray ,在該類中定義函數(shù)Aver求解整數(shù)的平均值。定義NewArray類,同時繼承了SortArray, ReArray和AverArray,使得NewArray類的對象同時具有排序、逆轉(zhuǎn)、和求平均值的功能。在繼承的過程中聲明為虛基類,體會虛基類在解決二義性問題中的作用。(2)實驗程序 (參考)程序如下:#include "iostream.h"#include "process.h"class MyArraypublic: MyArray(int leng); MyArray()
4、; void Input(); void Display();protected: long int *alist; / 指向動態(tài)申請的一組空間 int length; / 整數(shù)的個數(shù);MyArray:MyArray(int leng) length=leng; ali
5、st=new long intlength; if(alist=NULL) cout<<"對不起,創(chuàng)建失敗。請重試。"exit(1); MyArray:MyArray() delete alist; cout<<"數(shù)組被清空。"<<e
6、ndl; void MyArray:Display() / 顯示數(shù)組內(nèi)容 int i; long int *p=alist; for (i=0;i<length;i+,p+) cout<<" "<<*p;void MyArray:Input() / 從鍵盤若干整數(shù) cout<<"請輸入:"<&l
7、t;length<<"個整數(shù):" int i; long int *p=alist; for(i=0;i<length;i+,p+) cin>>*p;class SortArray: virtual public MyArray private: int len;
8、; long int *sp;public: SortArray(int leng):MyArray(leng) len=leng; Sort(); ; void Sort() sp=new long intlen; long int q;
9、0; sp=alist; for(int i=0;i<len;i+) for(int j=0;j<len-1;j+) if(*(sp+j)>*(sp+j+1)
10、 q=*(sp+j); *(sp+j)=*(sp+j+1); *(sp+j+1)=q; ; class ReArray:
11、virtual public MyArray / 這里是虛基類,public: void Reverse() rp=new long intlen; long int q; rp=alist; for(int i=0;i<len/2;i+)
12、160; q=*(rp+i); *(rp+i)=*(rp+len-i-1); *(rp+len-i-1)=q; ReArray(int leng):MyArray(leng) &
13、#160; len=leng; Reverse(); private: int len; long int *rp;class AverArray:virtual public MyArray / 這里是虛基類,public: double Aver()
14、0; ap=new long intlen; double q=0; ap=alist; for(int i=0;i<len;i+) q=q+*ap;ap+; q=q/len; return q; AverArray(
15、int leng):MyArray(leng) len=leng; private: int len; long int *ap;class NewArray:public ReArray,public SortArray,public AverArray public: NewArray(int leng); NewArray();NewArray:NewArray(intleng):MyArray(leng),SortArray
16、(leng),ReArray(leng),AverArray(leng) cout<<"n新數(shù)組正在創(chuàng)建。n"NewArray:NewArray() cout<<"n新數(shù)組已被清空。n"void main() char b; int leng; do cout<<"請輸入數(shù)組長度:"<<endl;
17、; cin>>leng; while(leng<=0) cout<<"數(shù)組長度必須為大于零的整數(shù),請重新輸入數(shù)組長度:n"exit(1); cin>>leng; cout<<"n開始:n"
18、60; NewArray n(leng); n.Input(); cout<<"n您輸入的數(shù)組為:"<<endl; n.Display(); / 顯示數(shù)組 n.Reverse(); /顯示逆序
19、 cout<<"n倒序數(shù)組為:"<<endl; n.Display(); / 顯示逆轉(zhuǎn)以前的情況 cout<<"n平均值是:"<<n.Aver()<<endl;/求平均值 n.Sort();
20、; /排序 cout<<"n排序后(從小到大)數(shù)組為:"<<endl; n.Display(); / 顯示排序以后的情況 cout<<"nA繼續(xù) Q
21、退出"<<endl; cin>>b; while(b='A'|b='a');執(zhí)行結(jié)果為: 2、編寫一個學(xué)生和教師數(shù)據(jù)輸入和顯示程序。(1)實驗內(nèi)容編寫學(xué)生和教師數(shù)據(jù)輸入和顯示程序,學(xué)生數(shù)據(jù)有編號、姓名、班號和成績,教師數(shù)據(jù)有編號、姓名、職稱和部門。要求將編號、姓名輸入和顯示設(shè)計成一個類person,并作為學(xué)生數(shù)據(jù)操作類student和教師數(shù)據(jù)操作類teacher的基類。(2)實驗程序(參考)#include<iostream.h> cla
22、ss person protected: int m; char A20; char *name; public: void input() cout<<"編號:" cin>>m; cout<<"姓名:"
23、0; cin>>A; name=&A0; void display() cout<<"編號:"<<m<<endl;
24、 cout<<"姓名:"<<name<<endl; ; class student:public person protected: int classnum, mark;public: void input1()
25、0; cout<<"輸入一個學(xué)生數(shù)據(jù):"<<endl; input(); cout<<"班號:" cin>>classnum; cout<<"成績:" cin&g
26、t;>mark; void display1() cout<<"顯示一個學(xué)生的數(shù)據(jù):"<<endl; display(); cout<<"班號:"<<classnum<<endl; cout<<"成績:"<<
27、;mark<<endl; ;class teacher: public person protected: char zhicheng20,bumen20; char *p; public: void input2() cout<<"顯示一個老師的數(shù)據(jù):"<<endl;
28、; input(); cout<<"職稱:" cin>>zhicheng; cout<<"部門:" cin>>bumen; void display2() cout<<"顯示一個老師的數(shù)據(jù):"<<endl;
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《巴塞羅那城市規(guī)劃》課件
- 接受多人委托協(xié)議
- 英語口語演講稿鐘
- 《課件展示:城鄉(xiāng)結(jié)合部本土資源的創(chuàng)新開發(fā)與應(yīng)用》
- 周圍神經(jīng)損傷的診療進展課件
- 《本科有機化學(xué)課件新-緒論》
- 雙十二與傳統(tǒng)媒體
- 養(yǎng)老機構(gòu)九類常見安全事件處置流程及預(yù)防措施
- 《蝴蝶的樂園:課件》
- 《心臟電生理》課件
- 2025年公共衛(wèi)生與預(yù)防醫(yī)學(xué)考試試卷及答案
- 2024年四川公安廳招聘警務(wù)輔助人員筆試真題
- 網(wǎng)站聯(lián)盟廣告專題報告
- 廣東入團考試試題及答案
- 2025年四川省成都市高新區(qū)中考數(shù)學(xué)二診試卷
- 貴州煙草專賣局招聘筆試題庫2025
- 高考數(shù)學(xué)總復(fù)習(xí)第九章概率9.1隨機事件的概率
- 中國證券金融股份有限公司招聘筆試真題2024
- 鋼琴藝術(shù)培訓(xùn)管理制度
- 深圳市人才集團筆試題庫
- 校園廣播設(shè)備維保合同
評論
0/150
提交評論