




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)五 多態(tài)程序設(shè)計(jì)一、 實(shí)驗(yàn)?zāi)康模豪斫膺\(yùn)算符重載的概念和實(shí)質(zhì),掌握運(yùn)算符重載函數(shù)的定義方法,掌握運(yùn)算符重載為類的成員函數(shù)和友元函數(shù)的方法。掌握虛函數(shù)的定義方法及其在實(shí)現(xiàn)多態(tài)性中的應(yīng)用,理解靜態(tài)連編和動態(tài)鏈編的區(qū)別。二、 實(shí)驗(yàn)時(shí)間:2022/3/7三、 實(shí)驗(yàn)地點(diǎn):四、 實(shí)驗(yàn)內(nèi)容:1已知基類:class Base public : virtual void I_am()cout”Base”endl; 要求:(1)從Base類中派生出兩個(gè)類,分別定義I_am()函數(shù),實(shí)現(xiàn)輸出自己類的名字。(2)主函數(shù)中創(chuàng)建者3個(gè)類的對象來調(diào)用I_am()函數(shù),再利用Base的指針數(shù)組通過循環(huán)語句來調(diào)用這3個(gè)對象的
2、I_am()函數(shù)。/ 1.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。/#include stdafx.h#include#includeusing namespace std;class basepublic:base()virtual void i_am()couti am baseendl;class base_x:public basepublic:base_x()void i_am()couti am base_xendl;class base_y:public basepublic:base_y()void i_am()couti am base_yendl;int main() ba
3、se b; base_x b_x; base_y b_y; base *a3=&b,&b_x,&b_y; for(int i=0;ii_am();return 0; 2. 編寫程序,計(jì)算圓柱體,球體,正方體的表面積和體積。要求用抽象類實(shí)現(xiàn)。/ 2.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。/#include stdafx.h#include#includeusing namespace std;const double PI=3.1415926;class geometrypublic: virtual void surface() const=0;virtual void volume() c
4、onst=0;class square:public geometryprivate:float a,b,c;public:square(float a1,float b1,float c1)a=a1;b=b1;c=c1;void surface()constcout該正方形的表面積為:2*(a*b+b*c+c*a)endl;void volume()constcout該正方形的體積為:a*b*cendl;class cylinder:public geometryprivate:float r,h;public:cylinder(float r1,float h1) r=r1; h=h1;v
5、oid surface()constcout該圓柱體的表面積為:2*PI*r*(r+h)endl;void volume()constcout該圓柱體的體積為:PI*r*r*hendl;class globe:public geometryprivate:float r;public:globe(float r1)r=r1;void surface()constcout該球體的表面積為:4*PI*r*rendl;void volume()const cout該球體的體積為:4*PI*r*r*r/3endl;int main()square s(3,4,5);cylinder c(4.5,6);
6、globe g(4);geometry *a3=&s,&c,&g;for(int i=0;isurface(); ai-volume();return 0;3. 設(shè)計(jì)字符串類String,完成以下功能: (1)使用+運(yùn)算符實(shí)現(xiàn)兩個(gè)字符串的連接功能。 (2)使用=運(yùn)算符實(shí)現(xiàn)字符串的復(fù)制功能。 (3)使用=運(yùn)算符判斷兩個(gè)字符串是否相等功能。 (4)計(jì)算字符串長度的功能。/ 3.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。/#include stdafx.h#include#includeusing namespace std;class Stringprivate:char *str;public:S
7、tring(char *str1=) str=new charstrlen(str1)+1; strcpy(str,str1);String() delete str;void display()coutstr:strstr=new charstrlen(str1.str)+strlen(str2.str)+1;strcpy(s-str,str1.str);strcat(s-str,str2.str);return *s; String & operator=(String &str1) if(strcmp(str1.str,str)=0) return str1; else str=new
8、charstrlen(str1.str)+1; strcpy(str,str1.str); return *this ; friend bool operator=(String &str1,String &str2) if(strcmp(str1.str,str2.str)=0) return true; else return false; friend int strlen_S(String &str1) int i=0; int len=0; while(str1.stri!=0) len=len+1; i+; return len; /return strlen(str1.str);
9、 ;int main() String s(2323); String s2(2323); String s3; s.display(); s2.display(); if(s=s2) cout兩字符串不相等,后者長度為strlen_S(s2)endl; else cout兩字符串相等,前者長度為strlen_S(s)endl; s=s+s2; s3=s; s=s; s.display(); s3.display(); s2.display(); if(s=s2) cout兩字符串不相等,后者長度為strlen_S(s2)endl; else cout兩字符串相等,前者長度為strlen_S(s)endl;return 0; 五、實(shí)驗(yàn)要求: 在上機(jī)前寫出全部源程序; 六、實(shí)驗(yàn)結(jié)果分析:七、實(shí)驗(yàn)心得體會及問題反饋: 此次實(shí)驗(yàn)還是比較簡單,課
溫馨提示
- 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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 場內(nèi)員工宿舍管理制度
- 出納做好票據(jù)管理制度
- 公司騎行車輛管理制度
- 醫(yī)療資格動態(tài)管理制度
- 醫(yī)院廁所革命管理制度
- 購房合同撤銷協(xié)議
- 食堂財(cái)務(wù)透明合作協(xié)議
- 彩繪人幣采購合同
- 2025至2030年中國AMPS聚合物行業(yè)發(fā)展戰(zhàn)略規(guī)劃及投資方向研究報(bào)告
- 春節(jié)作文開頭優(yōu)美語句5條(10篇)
- T/CCS 049-2023綠色煤炭資源評價(jià)技術(shù)規(guī)范
- 三級醫(yī)院評審標(biāo)準(zhǔn)實(shí)施細(xì)則(2023 年版)
- 嵌入式Linux圖形化界面與邊緣計(jì)算的協(xié)同設(shè)計(jì)研究-洞察闡釋
- 順路接送免責(zé)協(xié)議書
- 2025年春江蘇開放大學(xué)中國文化概論060692過程性考核作業(yè)123參考答案
- 《手術(shù)室圍術(shù)期的護(hù)理》課件
- 金融租賃測試題及答案
- 法律文化-形考作業(yè)2-國開(ZJ)-參考資料
- 2025年跨文化溝通能力考試試卷及答案
- 2025-2030中國激光多普勒測振儀行業(yè)市場發(fā)展趨勢與前景展望戰(zhàn)略研究報(bào)告
- 聾校義務(wù)教育數(shù)學(xué)課程標(biāo)準(zhǔn)解讀與實(shí)施
評論
0/150
提交評論