




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實驗報告課程名稱程序設(shè)計語言C/C+實驗項目繼承和派生一、實驗?zāi)康?、掌握繼承的概念。2、理解派生類與基類的關(guān)系。3、理解不同的繼承類型。4、掌握繼承下的構(gòu)造函數(shù)和析構(gòu)函數(shù)。5、掌握單繼承和多繼承使用方法。6、理解const常數(shù)據(jù)成員和常成員函數(shù)的使用。二、實驗內(nèi)容1.上機建立、編譯、運行并分析下面程序,并理解繼承的概念。#include#includeusingnamespacestd;classPersonprivate:charm_strName20;intm_nAge;intm_nSex;public:Person。;/構(gòu)造函數(shù)Person(char*name,intage,chars
2、ex);/構(gòu)造函數(shù)Person(constPerson&p);拷貝構(gòu)造函數(shù)Person()析構(gòu)函數(shù)coutNowdestroyingtheinstanceofPersonendl;voidSetName(char*name);voidSetAge(intage);voidsetSex(charsex);char*GetName();intGetAge();charGetSex();voidShowMe();;源程序文件person.cpp的內(nèi)容:/#includeperson.hPerson:Person():m_nAge(0),m_nSex(0)/構(gòu)造函數(shù)strcpy(m_strName,X
3、XX);Person:Person(char*name,intage,charsex):m_nAge(age),m_nSex(sex=m?0:1)/構(gòu)造函數(shù)strcpy(m_strName,name);Person:Person(constPerson&p):m_nAge(p.m_nAge),m_nSex(p.m_nSex)/拷貝構(gòu)造函數(shù)strcpy(m_strName,p.m_strName);voidPerson:SetName(char*name)strcpy(m_strName,name);voidPerson:SetAge(intage)一;voidn:setSex(charsex)
4、m_nSex=sex=m?0:1;Char*Person:一returnmstrName;:。一=iAghar)return(m_nSex=0?m:f);VoidPerson:coutGetName()tGetAge()tGetSex()t;一/#include“person.h:lassEi:”雇員類定義charm_strDept20;工作部門floatm_fSalary;/月薪public:Employee。;Employee(char*name,intage,charsex,char*dept,floatsalary)Employee(Employee&e);Employee()cout
5、NowdestroyingtheinstanceofEmployeeendl;voidSetDept(char*dept);voidSetSalary(floatsalary);char*GetDept();floatGetSalary();voidShowMe();顯示雇員信息;源程序文件employee.cpp的內(nèi)容:/#includeemployee.hEmployee:Employee():m_fSalary(0.0)strcpy(m_strDept,xxxx);Employee:Employee(char*name,intage,charsex,char*dept,floatsala
6、ry):Person(name,age,sex),m_fSalary(salary)strcpy(m_strDept,dept);Employee:Employee(Employee&e):Person(e.GetName(),e.GetAge(),e.GetSex(),m_fSalary(e.m_fSalary)strcpy(m_strDept,e.m_strDept);voidEmployee:SetDept(char*dept)strcpy(m_strDept,dept);Void(floatsalary)mfSalary=salary;Char*returnmstrDept;/Empl
7、oyees3)returnmfSalary;顓一SPcoutm_strDepttm_fSalaryendl;容/#includeemployee.htmain()Employeeemp1;emp1.ShowMe();Employeeemp2(張莉,40,f,圖書館,2000);emp2.ShowMe();cout調(diào)用基類GetName()返回值為:emp2.GetName()endlreturn0;要求:(1)給出實驗結(jié)果;(2)如果改為私有派生,會出現(xiàn)什么情況?(3)若要讓程序能正常運行,該如何調(diào)試處理?(4)為了防止類中的某些數(shù)據(jù)成員在使用過程中被修改可以采取什么措施?(掌握const常數(shù)
8、據(jù)成員和常成員函數(shù)的使用)2.上機分析下面程序,理解繼承下構(gòu)造函數(shù)和析構(gòu)函數(shù)的執(zhí)行順序。#includeusingnamespacestd;classApublic:A()coutConstructor1_Axendl;A(intm):x(m)coutConstructor2_Axendl;A()coutDestructor_Axendl;private:intx;classB:publicApublic:B()coutConstructor1_Byendl;B(intm,intn,intl):A(m),a(n),y(l)coutConstructor2_Byendl;B()coutDestr
9、uctor_Byendl;private:Aa;inty;intmain()Bb1,b2(5,6,7);return0;要求:(1)給出實驗結(jié)果;(2)掌握構(gòu)造函數(shù)和析構(gòu)函數(shù)的執(zhí)行順序的方法。3、從Person類中派生一個教師類。要求新增的屬性有專業(yè)、職稱和主講課程,并為這些屬性提供相應(yīng)的方法。4、聲明一個Shape(形狀)基類,它有兩個派生類:Circle(圓)和Square(正方形)。要求:(1)根據(jù)給出的圓心坐標(biāo)和半徑計算圓的面積;(2)根據(jù)給出的正方形中點坐標(biāo)和一個頂點坐標(biāo)計算正方形的面積。提示:Shape類的數(shù)據(jù)成員包括中心點的坐標(biāo),Circle類中新增一個數(shù)據(jù)成員,即圓的半徑,Sq
10、uare類新增一個頂點的坐標(biāo)。三、實驗步驟及結(jié)果分析.程序的類結(jié)構(gòu)圖為:Person-m_strName20:char-m_nAge:int-m_nSex:int+Person()+Person(char*name,intage,charsex)+Person(constPerson&p)+Person()+SetName(char*name):void+SetAge(intage):void+setSex(charsex):void+*GetName():char+GetAge():int+GetSex():char+ShowMe():voidtEmployeem_strDept20:cha
11、rm_fSalary:float+Employee()+Employee(char*name,intage,charsex,char*dept,floatsalary)+Employee(Employee&e)+Employee。+SetDept(char*dept):void+SetSalary(floatsalary):void+*GetDept():char+GetSalary():float+ShowMe():void運行結(jié)果:XXX0RXKXX0張莉40f圖書館2000調(diào)用基類GetNam巳0返回值為:張莉NowdestroyingtheinstanceofEmployeeNowde
12、stroyingtheinstanceofPersonNowdestroyingtheinstanceofEmployeeNowdestroyingtheinstanceofPersonPressanykeytocoiitiLiue修改過后的程序代碼如下:#include#includeusingnamespacestd;lassPersonprivate:charm_strName20;intm_nAge;intm_nSex;public:Person。;/構(gòu)造函數(shù)Person(char*name,intage,charsex);/構(gòu)造函數(shù)Person(constPerson&p);/拷貝構(gòu)
13、造函數(shù)Person()析構(gòu)函數(shù)coutNowdestroyingtheinstanceofPersonendl;VoidSetName,char*name);voidSetAge(intage);voidsetSex(charsex);char*GetName();intGetAge();charGetSex();一);源程序文件person.cpp的內(nèi)容:/#include“person.hi:i):(00函數(shù)strcpy(m_strName,XXX);P小n(char*name,intage,charsex):m_nAge,age),m_nSex,sex=m?0:1)/,構(gòu)造函數(shù)strcp
14、y(m_strName,name);ieSi-(S)拷貝構(gòu)造函數(shù)strcpy(m_strName,p.m_strName);一一ar*name)strcpy(m_strName,name);VoidPerson:SetAge(intage);age;:”,m_nSex=sex=m?0:1;Char*Person:一returnmstrName;ntP一1-Charireturn(m_nSex=0?m:f);VoidPerson:ShowMe()coutGetName()tGetAge()tGetSex()t;內(nèi)容:/#includeperson.hlassEmployee:0員類定義charm
15、_strDept20;工作部門floatm_fSalary;/月薪public:Person:SetName;Person:SetAge;Person:setSex;Person:GetName;Employee。;Employee(char*name,intage,charsex,char*dept,floatsalary)Employee(Employee&e);Employee()coutNowdestroyingtheinstanceofEmployeeendl;voidSetDept(char*dept);voidSetSalary(floatsalary);char*GetDept
16、();floatGetSalary();voidShowMe();顯示雇員信息;源程序文件employee.cpp的內(nèi)容:/#includeemployee.hEmployee:Employee():m_fSalary(0.0)strcpy(m_strDept,xxxx);Employee:Employee(char*name,intage,charsex,char*dept,floatsalary):Person(name,age,sex),m_fSalary(salary)strcpy(m_strDept,dept);Employee:Employee(Employee&e):Person
17、(e.GetName(),e.GetAge(),e.GetSex(),m_fSalary(e.m_fSalary)strcpy(m_strDept,e.m_strDept);voidEmployee:SetDept(char*dept)strcpy(m_strDept,dept);一_)m_fSalary=salary;2returnmstrDept;/Employee.etSa.yOreturnmfSalary;Void-S,;coutm_strDepttm_fSalaryendl;in.cp/#includeemployee.hntmain()Employeeemp1;emp1.ShowM
18、e();Employeeemp2(張莉,40,f,圖書館,2000);emp2.ShowMe();cout調(diào)用基類GetName()返回值為:emp2.GetName()endlreturn0;運行結(jié)果:調(diào)用基類GetNameO返回值為:張莉NowdestroyingtheinstanceofEmployeeNowdestroyingtheinstanceofPersonNowdestroyingtheinstanceofEmployeeNowdestroyingthinstanceofPersonPressanykeytocontinueinfXXX0張莉40 xxxx0圖書評2000.程序
19、的類結(jié)構(gòu)圖為:運行結(jié)果:Constructor!A-858993460Construetorl_A-85899346CConstruetorIB-858993460Construetor2_A5Construetor2_A6Construetor2_B7Destruetor_B7Destruetor_A6Destruetor_A5Destruetor_B-858993460Destruetor_A-858993460Destruetor_A-858993460Pressanykeytocontinue3.程序的類結(jié)構(gòu)圖為:Person#m_name20:char#m_age:int#m_sex
20、:char+Person()+information(char*void+Person()name,intage,charsex):Teacher#major20:char#position20:char#course20:char+m_major(char*m):void+m_position(char*p):void+m_course(char*c):void程序代碼如下:#include#includeusingnamespacestd;classPersonpublic:Person。;/構(gòu)造函數(shù)voidinformation(char*name,intage,charsex)/定義P
21、erson類的信息函數(shù)m_age=age;m_sex=sex;strcpy(m_name,name);cout姓名:m_nameendl;cout年齡:m_ageendl;cout性別:m_sexendl;Person()/析構(gòu)函數(shù)coutNowdestroyingtheinstanceendl;protected:charm_name20;intm_age;charm_sex;Person:Person()classTeacher:publicPersonpublic:voidm_major(char*m);/定義專業(yè)voidm_position(char*p);/定義職稱voidm_cou
22、rse(char*c);/定義主講課程protected:charmajor20;charposition20;charcourse20;mmajor,charm)I_cout專業(yè):majorendl;VoidTeache一。),),cout職稱:positionendl;voidTeacher:m_course(char*c)strcpy(course,c);cout”主講課程:courseendl;intmain()TeacherA;創(chuàng)建派生類的對象cout教師的個人資料:endl;A.information(*,*,*);A.m_major(*);A.m_position(*);A.mc
23、ourse(*);retUrn0;_LOU-L-L-Lvz;)4.程序的類結(jié)構(gòu)圖為:Shape1#x_size,y_size:double+Shape1(doublex,doubley)+area():virtualdouble+perimeter():virtualdouble+print():virtualvoidzzkCircle1Square1#radius:double#i_size,j_size:double+Circle1(doublex=0.0,doubley=0.0,doubler=0.0)+set_radius(doubler=0.0):void+get_radius():
24、double+area():virtualdouble+perimeter():virtualdouble+print():virtualvoid+Square1(doublex=0.0,doubley=0.0,doublei=0.0,doublej=0.0)+set_i(doublei):void+set_j(double_j):void+get_i():void+get_j():void+area():virtualdouble+perimeter():virtualdouble+print():virtualvoid程序代碼如下:#includeusingnamespacestd;cla
25、ssShape1protected:doublex_size,y_size;public:Shape1(doublex,doubley);/構(gòu)造函數(shù)virtualdoublearea()/純虛函數(shù),在派生類中重載return0.0;virtualdoubleperimeter。return0.0;virtualvoidprint()=0;;Shape1:Shape1(doublea,doubleb)x_size=a;y_size=b;classCircle1:publicShapelprotected:doubleradius;public:Circle1(doublex=0.0,double
26、y=0.0,doubler=0.0);/構(gòu)造函數(shù)voidset_radius(doubler=0.0);設(shè)置半徑doubleget_radius();輸出半徑virtualdoublearea();virtualdoubleperimeter。;virtualvoidprint();輸出圓心坐標(biāo)和半徑;Circle1:Circle1(doublex,doubley,doubler):Shape1(x,y),radius(r)/構(gòu)造函數(shù)voidCircle1:set_radius(doubler)radius二r;doubleCircle1:get_radius()returnradius;do
27、ubleCircle1:area()return3.14159*radius*radius;doubleCircle1:perimeter()return3.14159*radius*2;voidCircle1:print()輸出圓心坐標(biāo)coutxsize,ysize,radius;classSquare1:publicShape1protected:doublei_size,j_size;public:Square1(doublex=0.0,doubley=0.0,doublei=0.0,doublej=0.0);/構(gòu)造函數(shù)voidset_i(doublei);voidset_j(doublej);doubleget_i();voidset_i(doublei);voidset_j(doublej);doubleget_i();doubleget_j();virtualdoublearea();virtualdoubleperimeter。;virtualvoidprint();設(shè)置頂點橫坐標(biāo)設(shè)置頂點縱坐標(biāo)輸出頂點橫坐標(biāo)輸出頂點縱坐標(biāo)輸出中
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國四路數(shù)字硬盤錄像機數(shù)據(jù)監(jiān)測研究報告
- 【假期提升】五升六語文暑假作業(yè)(九)-人教部編版(含答案含解析)
- 2025年消防設(shè)施操作員之消防設(shè)備中級技能考前沖刺模擬試卷A卷含答案
- 2025年消防設(shè)施操作員之消防設(shè)備高級技能每日一練試卷A卷含答案
- 廣東省廣州市海珠區(qū)南武集團2022-2023學(xué)年八年級下學(xué)期期中物理試題(含答案)
- 煙草公司2023招聘考試全真筆試試題(綜合能力測試卷)和答案解析
- 酒店用品銷售代理合同(2篇)
- 采購分包配送合同(2篇)
- 廣告行業(yè)廣告創(chuàng)意版權(quán)保護協(xié)議
- 社區(qū)農(nóng)業(yè)服務(wù)提供合同書
- 基地種植合作合同范本
- 露天煤礦安全生產(chǎn)技術(shù)露天煤礦安全管理培訓(xùn)
- 2025年湖南大眾傳媒職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫學(xué)生專用
- 【2025年衛(wèi)生健康宣傳日】世界防治結(jié)核病日
- 新版《醫(yī)療器械經(jīng)營質(zhì)量管理規(guī)范》(2024)培訓(xùn)試題及答案
- 2025年人教版數(shù)學(xué)五年級下冊教學(xué)計劃(含進度表)
- h型鋼力學(xué)性能計算表
- 新老加油加氣站設(shè)計與施工規(guī)范要點對比解讀
- 醫(yī)藥企業(yè)價格和營銷行為信用承諾書
- 三體系程序文件(參考)
- L-J質(zhì)控圖制作(傻瓜式填數(shù)據(jù)生成圖)
評論
0/150
提交評論