劉小虎試驗二_第1頁
劉小虎試驗二_第2頁
劉小虎試驗二_第3頁
劉小虎試驗二_第4頁
劉小虎試驗二_第5頁
已閱讀5頁,還剩35頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、實驗2派生類與繼承實驗課程名:高級語言程序設(shè)計H專業(yè)班級: 12 網(wǎng)工 2班 學(xué)號: 201240420228姓名:劉小虎實驗時間:4.20-5.5 實驗 地點: K4-201指導(dǎo)教師:祁文青-.實驗?zāi)康暮鸵?1) 掌握派生類的聲明方法和派生類構(gòu)造函數(shù)的定義方法。(2) 掌握不同繼承方式下,基類成員在派生類中的訪問屬性。(3) 掌握在繼承方式下,構(gòu)造函數(shù)與析構(gòu)函數(shù)的執(zhí)行順序與構(gòu)造規(guī)則。(4) 學(xué)習(xí)虛基類在解決二義性問題中的作用。2.2實驗內(nèi)容與步驟1. 輸入下列程序。test4_1.cpp#in clude<iostream> using n amespace std;class

2、 Basepublic:void setx(int i)x=i;Int getx()return x;public:int x;class Derived:public Basepublic:void sety(int i)y=i;int gety()return y;void show()cout<< ”Base:x=”<<x<<endl;public:inty;int main()Derived bb;bb,setx(16); bb.sety(25);bb.show(); cout<<”Base:x=”<<bb.x<<

3、endl; cout<< ”Derived:y= ”<<bb.y<<endl; cout<<”Base:x=”<<bb.getx()<<endl; cout<< ”Derived:y= ”<<bb.gety()<<endl;return 0;(1) 寫出程序的運行結(jié)果。(2) 按以下要求,對程序進行修改后再調(diào)試,指出調(diào)試中出錯 的原因。將基類 Base 中數(shù)據(jù)成員 x 的訪問權(quán)限改為 private 時,會出 現(xiàn)哪些錯誤?為什么? 將基類Base中數(shù)據(jù)成員x的訪問權(quán)限改為protecte

4、d時,會 出現(xiàn)哪些錯誤?為什么? 在源程序的基礎(chǔ)上,將派生類Derived的繼承方式改為private 時,會出現(xiàn)哪些錯誤?為什么? 在源程序的基礎(chǔ)上,將派生類Derived的繼承方式改為protected 時,會出現(xiàn)哪些錯誤?為什么?解答: 1.f C:LI se rsAd m inist rato rDes kto=Debugl aexe*B&se = = x-16Ba.se : = x=16Dfitivcd: :y=25Ka.sc ; ; x-16Dcriued: :_y=25P012 4P4迪純劉小虎實驗二第一題Press finy kei to continue2. (1)

5、這2條語句錯誤coutvv ” Base:x= ” vvxvve ndl;coutvv ” Base:x=” vvbb.xvve ndl;將基類Base中數(shù)據(jù)成員x的訪問權(quán)限改為private后,它在 派生類中的訪問屬性變?yōu)椴豢稍L問的成員,因此在派生的 Derived類中不能訪問數(shù)據(jù)成員X,以及也不能在派外直接引用。(2).這條語句錯誤 coutvv ”Base:x=” vvbb.xvvendl;將基類Base中數(shù)據(jù)成員x的訪問權(quán)限改為protected后,經(jīng) 過公用繼承,它在派生類中的訪問屬性任然為protected,所以在派外不能用對象引用。(3) 以下語句會出錯:bb.setx(16);

6、coutvv"Base:x="vvbb.xvve ndl;coutvv"Base:x="vvbb.getx()vve ndl;將派生類Derived的繼承方式改為private后,基類中的公有成員在派生類中訪問屬性都變?yōu)樗接械模?所以在派外不能用對象 引用。(4)以下語句會出錯:bb.setx(16);cout<<"Base:x="<<bb.x<<endl; cout<<"Base:x="<<bb.getx()<<endl;將派生類 Deriv

7、ed 的繼承方式改為 protected 后,基類中的公 有成員在派生類中訪問屬性都變?yōu)楸Wo的, 所以在派外不能用對 象引用。2. 編寫一個學(xué)生和教師的數(shù)據(jù)輸入和顯示程序。學(xué)生數(shù)據(jù)有 編號、姓名、 性別、 年齡、系別和成績, 教師數(shù)據(jù)有編號、 姓名、 性別、年齡、職稱和部門。要求將編號、姓名、性別、年齡的輸 入和顯示設(shè)計成一個類Perso n,并作為學(xué)生類Stude nt和教師類Teacher的 基類。供參考的類結(jié)構(gòu)如下:class Person;class Student:public Person;class Teacher:public Person;解答:程序如下 #include&l

8、t;iostream> #include<string> using namespace std;class Personpublic:void get_value()cin>>num>>name>>sex>>age;void display()cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<

9、sex<<endl; cout<<"age:"<<age<<endl;private:int num;string name;char sex;int age;class Student :public Personpublic:void get_value_1()cin>>xibie>>score;void display_1()cout<<"xibie:"<<xibie<<endl; cout<<"score:"

10、<<score<<endl;private:string xibie;int score;class Teacher:public Personpublic:void get_value2()cin>>title>>bumen;void display_2()cout<<"title:"<<title<<endl;cout <<"bumen:"<<bumen<<endl;private:string title;string bumen

11、;int main()int i,m,n;cin>>m>>n;Student *stud=new Studentn;for(i=0;i<n;i+)studi.get_value(); studi.get_value_1(); studi.display();studi.display_1();Teacher * teach=new Teacher m;for(i=0;i<m;i+)teachi.get_value(); teachi.get_value2(); teachi.display();teachi.display_2();cout<<&

12、quot;201240420228,劉小虎"<<endl;return 0;'C:Usrs.Admir strtorDesktopD?bngCppl exe'卩12S liuxiohu m 20 pisualj 1 97nun:28name: 1 iuxiaoliuage-2& MibzLB : j isuai Jzl score20 hmLuA n QEReacher Jlsuaij1 nun=30nane:buliuaA5ex:mage = 35title zteac tier bunen; jis u<ti ji 20124042822

13、8 劉小虎 Press 且n野 kei/ to centinue3按要求閱讀、編輯、編譯、調(diào)試和運行以下程序。(1)閱讀、編輯、編譯、調(diào)試和運行以下程序,并寫出程序的運行結(jié)果。test4_3_1.cpp#in clude<iostream>#in cludevstri ng> using n amespace std; class MyArraypublic:MyArray(int leng); MyArray; void Input(); void Display(string); protected: int*alist;int length;MyArray:MyArra

14、y(int leng) if(leng<=0)cout<< ”error length”; exit(1);alist=new int leng; length=leng;if(alist=NULL)cout<< ”assign failure”; exit(1);”<<endl;cout<< ”MyArray 類對象已創(chuàng)建MyArray:MyArray()delete alist;cout<< ”MyArray 類對象被撤銷。 ”<<endl;void MyArray:Display(string str)int

15、i;int *p=alist;cout<<str<<length<< ”個整數(shù):“;for(i=0;i<length;i+,p+)cout<<*p<< ”;”cout<<endl;void MyArray:Inputcout<< ”請鍵盤輸入 ”<<length<< ”個整數(shù) :”;int i;int *p =alist;for(i=0;i<length;i+,p+)cin>>*p;int main()MyArray a(5);a.Input();a.Displa

16、y( “顯示已輸入的 ”;)return 0;(2) 聲明一個類 SortArray 繼承類 MyArray, 在該類中定義一 個函數(shù),具有將輸入的整數(shù)從小到大進行排序的功能。【提示】在第( 1)步的基礎(chǔ)上可增加下面的參考框架:class SortArray : public MyArray public:void Sort();SortArray(int leng):MyArray(leng)cout<< ”SortArray 類對象已創(chuàng)建。 ”<<endl;virtual SortArray();SortArray:SortArray()cout<< ”

17、SortArray 類對象被撤銷。 ”<<endl;void SortArray:Sort()/ 請自行編寫 Sort 函數(shù)的代碼,將輸入的整數(shù)從小到大排序。/并將主函數(shù)修改為:int main()SortArray a(5);s.Input();s.Display( “顯示排序以前的 ”);s.Sort();s.Display( “顯示排序以后的 ”); return 0;聲明一個類 ReArray 繼承類 MyArray, 在該類中定義一個函數(shù), 具有將輸入的整數(shù)進行倒置的功能?!咎崾尽吭诘? 1)步的基礎(chǔ)上可增加下面的參考框架:Class ReArray:public MyA

18、rrayPublic:Void reverse();ReArray(int leng);Virtual ReArray();請讀者自行編寫構(gòu)造函數(shù)、析構(gòu)函數(shù)和倒置函數(shù)ReArray,以及修改主函數(shù)。(3) 聲明一個類 AverArray 繼承類 MyArray, 在該類中定義一個函數(shù),具有求輸入的整數(shù)平均值的功能。提示】在第( 1)步的基礎(chǔ)上增加下面的參考框架:class AverArray:public MyArrayPublic:AverArray(int leng);AverArray();Double Aver();請讀者自行編寫構(gòu)造函數(shù)、 析構(gòu)函數(shù)和求平均值函數(shù) Ave(r 求 解整

19、數(shù)的平均值) ,以及修改主函數(shù)。(2)聲 明一個 NewArray 類,同時繼承了類 SortArray,ReArray 和 AverArray, 使得類 NewArray 的對象同時具有排序、倒置和 求平均值的功能。 在繼承的過程中聲明 MyArray 為虛基類, 體會虛基類在解決二義性問題中的作用。#include<iostream> #include<string> using namespace std; class MyArraypublic:MyArray(int leng);MyArray();void Input();void Display(strin

20、g);protected: int*alist;int length;MyArray:MyArray(int leng)if(leng<=0)cout<<"error length" exit(1);alist=new int leng; length=leng;if(alist=NULL)cout<<"assign failure"exit(1);cout<<"MyArray 類對象已創(chuàng)建MyArray:MyArray()delete alist;cout<<"MyArray 類

21、對象被撤銷 "<<endl;"<<endl;void MyArray:Display(string str) int i;int *p=alist;cout<<str<<length<<" 個整數(shù): "for(i=0;i<length;i+,p+)cout<<*p;cout<<endl;void MyArray:Input()cout<<" 請鍵盤輸入 "<<length<<" 個整數(shù) :"

22、int i;int *p =alist;for(i=0;i<length;i+,p+)cin>>*p;class SortArray : public MyArraypublic:void Sort();SortArray(int leng):MyArray(leng)cout<<"SortArray 類對象已創(chuàng)建virtual SortArray();SortArray:SortArray()cout<<"SortArray 類對象被撤銷void SortArray:Sort()int *a=alist;"<<

23、;endl;"<<endl;int i,j,temp;for(i=0;i<length-1;i+)for(j=0;j<length-i-1;j+) if(*(a+j)>*(a+j+1) temp=*(a+j);*(a+j)=*(a+j+1);*(a+j+1)=temp;int main()SortArray a(5);a.Input();a.Display(" 顯示排序以前的 ");a.Sort();a.Display(" 顯示排序以后的 "); cout<<"201240420228, 劉小

24、虎 "<<endl; return 0;* C:Use rs Admi nisi rate rDe 5 kt opDe bugC ppl-exe"5 2 1:?7521:1257?AH41建創(chuàng) co b象敝喬芻象被t .Jan即 扌數(shù)y 丄IL- rf 8 X e 丄冃啟k Kp y 丿 ££ 0 yR陀 y raAr盤聾硼Arvas rt A- 2 t p s ArqllFne Hyso請顯顯 Mso眄"#include<iostream>#include<string>using namespace st

25、d;class MyArraypublic:MyArray(int leng);M yArray();void Input();void Display(string);protected:int*alist;int length;MyArray:MyArray(int leng)if(leng<=0)cout<<"error length"exit(1);alist=new int leng; length=leng;if(alist=NULL)cout<<"assign failure"exit(1);"<

26、;<endl;"<<endl;cout<<"MyArray 類對象已創(chuàng)建。MyArray:MyArray()delete alist;cout<<"MyArray 類對象被撤銷。void MyArray:Display(string str)int i;int *p=alist; cout<<str<<length<<" 個整數(shù): " for(i=0;i<length;i+,p+) cout<<*p<<" "cout&

27、lt;<endl;void MyArray:Input()cout<<" 請鍵盤輸入 "<<length<<" 個整數(shù) :" int i;int *p =alist;for(i=0;i<length;i+,p+) cin>>*p;class ReArray : public MyArray public:void reverse();ReArray(int leng):MyArray(leng)cout<<"ReArray 類對象已創(chuàng)建。 "<<end

28、l;virtual ReArray();ReArray:ReArray()"<<endl;cout<<"ReArray 類對象被撤銷void ReArray: reverse ()int i,j,m,temp;m=(length-1)/2;int *p=alist;for(i=0;i<=m;i+)j=length-i-1;temp=*(p+i);*(p+i)=*(p+j);*(p+j)=temp;int main()ReArray s(5);sn put();s.Display(”顯示倒置以前的”);s.reverse();s.Display(

29、"顯示倒置以后的”);cout«"201240420228,劉小虎"<<endl;return 0;#in clude<iostream>#in clude<stri ng> using n amespace std; class MyArraypublic:MyArray(i nt leng);MyArray();void Input();void Display(string);protected:int*alist;int length;MyArray:MyArray(int leng)if(leng<=0

30、)cout<<"error length"exit(1);alist=new int leng; length=leng; if(alist=NULL) cout<<"assign failure"exit(1);"<<endl;"<<endl;cout<<"MyArray 類對象已創(chuàng)建。MyArray:MyArray()delete alist;cout<<"MyArray 類對象被撤銷。void MyArray:Display(string

31、 str)int i;int *p=alist;cout<<str<<length<<" 個整數(shù):for(i=0;i<length;i+,p+)cout<<*p<<" "cout<<endl;void MyArray:Input()cout<<" 請鍵盤輸入 "<<length<<" 個整數(shù) :"int i;int *p =alist;for(i=0;i<length;i+,p+)cin>>*p

32、;class AverArray : public MyArraypublic:double Aver();AverArray(int leng):MyArray(leng)cout<<"AverArray 類對象已創(chuàng)建。 "<<endl;virtual AverArray();AverArray:AverArray()cout<<"AverArray 類對象被撤銷。 "<<endl;double AverArray:Aver()double m=0;for(int i=0;i<length;i+)m

33、=alisti+m;cout<<" 顯示 "<<length<<" 個整數(shù)的平均值: " return m/length;int main()AverArray s(5);s.Input();s.Display(" 顯示 ");cout<<s.Aver()<<endl;cout<<"201240420228 ,劉小虎 "<<endl;return 0;'C;UserjAdnn inist ratorDe e kto pDfr

34、bu gCppl. exe'nwiii*站類對象已創(chuàng)建-類對象已創(chuàng)建。請鍵盤祗£個整熱佔24 3& 25 20顯不百個整數(shù);15 2-1 36 25 20 顯示石木盪數(shù)的平均值;24 20124042 022 fi,劉小羞AverArray類對象祓撇銷°Tress 殖ny kcv to cont inue_#in clude<iostream>#in clude<stri ng> using n amespace std; class MyArraypublic:MyArray(i nt len g);MyArray();void I

35、n put();void Display(stri ng);protected:int len gth;in t*alist;cout<<"MyArray 類對象被撤銷。"<<endl;MyArray:MyArray(int leng)if(leng<=0)cout<<"error length"exit(1);alist=new int leng;length=leng;if(alist=NULL)cout<<"assign failure"exit(1);"<&

36、lt;endl;cout<<"MyArray 類對象已創(chuàng)建。MyArray:MyArray()void MyArray:Display(string str)int i;int *p=alist;cout<<str<<length<<" 個整數(shù):for(i=0;i<length;i+,p+)cout<<*p<<" "cout<<endl;void MyArray:Input()個整數(shù) :"cout<<" 請鍵盤輸入 "<

37、;<length<<"int i;int *p =alist;for(i=0;i<length;i+,p+)cin>>*p;"<<endl;"<<endl;class SortArray :virtual public MyArray public: void Sort();SortArray(int leng):MyArray(leng)cout<<"SortArray 類對象已創(chuàng)建。 virtual SortArray();SortArray:SortArray()cout<

38、;<"SortArray 類對象被撤銷。 void SortArray:Sort()int *a=alist;int i,j,temp;for(i=0;i<length-1;i+)for(j=0;j<length-i-1;j+)if(*(a+j)>*(a+j+1)temp=*(a+j);*(a+j)=*(a+j+1);*(a+j+1)=temp;class ReArray :virtual public MyArray public:void reverse();ReArray(int leng):MyArray(leng)cout<<"ReArray 類對象已創(chuàng)建。 "<<endl;virtual ReArray();ReArray:ReArray()cout<<"ReArray 類對象被撤銷。 "<<endl; void

溫馨提示

  • 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)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論