天津理工大學(xué)C++實(shí)驗(yàn)三_第1頁
天津理工大學(xué)C++實(shí)驗(yàn)三_第2頁
天津理工大學(xué)C++實(shí)驗(yàn)三_第3頁
天津理工大學(xué)C++實(shí)驗(yàn)三_第4頁
天津理工大學(xué)C++實(shí)驗(yàn)三_第5頁
已閱讀5頁,還剩14頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、天津理工大學(xué)計(jì)算機(jī)科學(xué)與技術(shù)學(xué)院實(shí)驗(yàn)報(bào)告 至 學(xué)年 第 學(xué)期課程名稱C+程序設(shè)計(jì)學(xué)號學(xué)生姓名年級專業(yè)教學(xué)班號實(shí)驗(yàn)地點(diǎn)實(shí)驗(yàn)時(shí)間 年 月 日 第 節(jié) 至 第 節(jié)主講教師輔導(dǎo)教師實(shí)驗(yàn)( 三 )實(shí)驗(yàn)名稱派生與繼承軟件環(huán)境C+visual硬件環(huán)境無實(shí)驗(yàn)?zāi)康模?) 理解繼承的含義,掌握派生類的定義方法和實(shí)現(xiàn);(2) 理解公有繼承下基類成員對派生類成員和派生類對象的可見性,能正確地訪問繼承層次中的各種類成員;(3) 理解保護(hù)成員在繼承中的作用,能夠在適當(dāng)?shù)臅r(shí)候選擇使用保護(hù)成員以便派生類成員可以訪問基類的部分非公開的成員;實(shí)驗(yàn)內(nèi)容(應(yīng)包括實(shí)驗(yàn)題目、實(shí)驗(yàn)要求、實(shí)驗(yàn)任務(wù)等)1.#include #define P

2、I 3.14159class Point / 定義“點(diǎn)”類int x, y;public:Point(int a=0, int b=0)x=a; y=b;void ShowPoint( )coutPoint:(x,y)n;int Getx( ) return x; int Gety( )return y;void Setxy(int a, int b)x=a; y=b;class Circle: public Point / 定義“圓”類,公有繼承int r; / “圓”的半徑 public:Circle(int x, int y, int ra) : Point(x, y) / B r =

3、ra; void Setr(int ra)r = ra; double Area( ) /求圓的面積return PI*r*r;void Move(int x_offset, int y_offset) /將圓心坐標(biāo)平移int x1=Getx( ); /存取基類的私有成員int y1=Gety( ); / Dx1 += x_offset; y1 += y_offset;Setxy(x1, y1); / Evoid ShowCircle( )ShowPoint( ); / Fcout Radius: rt;coutArea: Area( )endl; /G;void main( )Circle

4、c(1, 1, 1);c.ShowCircle( );c.Move(1, 2);c.ShowCircle( );c.Setxy(4, 5); /重新置圓心坐標(biāo) c.Setr(2); /重新置半徑值 c.ShowCircle( );實(shí)驗(yàn)過程與實(shí)驗(yàn)結(jié)果(可包括實(shí)驗(yàn)實(shí)施的步驟、算法描述、流程、結(jié)論等)(1) 記錄程序的運(yùn)行結(jié)果Point(1, 1)Radius: 1Area: 3.14159Point(2, 3)Radius: 1Area: 3.14159Point(4, 5)Radius: 2Area: 12.5664(2) 測試能否將move函數(shù)體改寫為x=x+x_offset;y=y+y_of

5、fset;不可以,因?yàn)閤和y是Point類的私有(private)成員。2.#include #define PI 3.14159class Point protected: /A int x, y;public:Point(int a=0, int b=0) x=a; y=b; void ShowPoint( ) coutPoint:(x,y)n; int Getx( ) return x; int Gety( ) return y; void Setxy(int a, int b) x=a; y=b; ;class Radius protected: int r;public:Radius

6、(int ra=0) r = ra; void Setr(int ra) r = ra; int Getr( ) return r; ;class Circle : public Point, public Radius public:Circle(int x, int y, int ra) : Point(x, y), Radius(ra) /D double Area( ) return PI*r*r; /直接訪問基類的保護(hù)成員 void Move(int x_offset, int y_offset) x += x_offset; y += y_offset; void ShowCirc

7、le( ) ShowPoint( );coutRadius: rt;coutArea: Area( )endl;void main( )Circle c(1, 1, 1);c.ShowCircle( );c.Move(1, 2);c.ShowCircle( );c.Setxy(4, 5);c.Setr(2);c.ShowCircle( );(1) 記錄程序的運(yùn)行結(jié)果Point:(1,1)Radius: 1 Area: 3.14159Point:(2,3)Radius: 1 Area: 3.14159Point:(4,5)Radius: 2 Area: 12.5664(2) 為什么可以直接使用x

8、,y,x和y是Point類的保護(hù)(protected)成員(3) 如果x,y在基類中是私有的行嗎?不行#include class Base1protected: int data1;public:Base1(int a=0)data1 = a; coutBase Constructor1n;Base1( )coutBase Destructor1n; ;class Base2protected: int data2;public:Base2(int a=0)data2 = a; coutBase Constructor2n;Base2( )coutBase Destructor2n; ;cl

9、ass Derived: public Base1, public Base2 int d;public:Derived(int x, int y, int z):Base1(x), Base2(y) d=z; coutDerived Constructorn;Derived( ) coutDerived Destructorn; void Show( ) coutdata1,data2,dendl; ;void main( ) Derived c(1, 2, 3);c.Show( );(1) 記錄程序的運(yùn)行結(jié)果Base Constructor1Base Constructor2Derived

10、 Constructor1,2,3Derived DestructorBase Destructor2Base Destructor1#include class Base1protected: int data1; public:Base1(int a=8)data1 = a; coutdata1, Base Constructor1n;Base1( )coutdata1, Base Destructor1n;class Base2protected: int data2;public:Base2(int a=9)data2 = a; coutdata2, Base Constructor2

11、n;Base2( )coutdata2, Base Destructor2n;class Derived:public Base1, public Base2 int d;Base1 b1, b2; public:Derived(int x, int y, int z) : Base1(x), Base2(y), b1(x+y), b2(x+z) d=z; coutDerived Constructorn;Derived( ) coutDerived Destructorn;void Show( )coutdata1,data2,dendl;void main( )Derived c(1, 2

12、, 3);c.Show( );(1) 記錄程序的運(yùn)行結(jié)果1, Base Constructor12, Base Constructor23, Base Constructor14, Base Constructor1Derived Constructor1,2,3Derived Destructor4, Base Destructor13, Base Destructor12, Base Destructor21, Base Destructor15.#include#includeusing namespace std;class baseprotected: float price; /單

13、價(jià) string place; /產(chǎn)地 int count; /庫存量 public: base(float &p,int &c,string &pl) price=p; count=c; place=pl; void in_something(int add_cnt) count+=add_cnt; void out_something(int del_cnt) if(del_cntcount)count=0; /變0保護(hù) 小于0的按0計(jì)算 else count-=del_cnt; double total_price() return price*count; int r_count()

14、return count; ;class shirt:public baseprotected: string material;public: shirt(float &p ,int &c ,string &pl ,string &m):base( p , c, pl) material=m; void print() coutn襯衣t產(chǎn)地:placet材料:material n庫存量:countt單價(jià):pricet總價(jià)格total_price()endl; ;class wardrobe:public baseprotected: string wood; string color;pub

15、lic: wardrobe(float &p ,int &c ,string &pl ,string &w ,string &co ):base(p,c,pl) wood=w; color=co; void print() coutn立柜t產(chǎn)地:placet顏色:colort木料:woodn庫存量: countt單價(jià):pricet總價(jià)格total_price()endl; ;class cap:public shirtprotected: string style;public: cap(float &p ,int &c ,string &pl ,string &m,string &st ):

16、shirt(p,c,pl,m) style=st; void print() coutn帽子t產(chǎn)地:placet材料:material 樣式:stylen庫存量:countt單價(jià): pricet總價(jià)格total_price()endl; ;int choose(string a) /用于判斷下一步執(zhí)行的命令 0.結(jié)束1.襯衣2.帽子3.立柜4.查詢5.入庫6.出庫7.出錯(cuò)重輸 if(a=0)return 0; /結(jié)束 else if(a=襯衣|a=shirt)return 1; else if(a=帽子|a=cap)return 2; else if(a=立柜|a=wardrobe)retur

17、n 3; else if(a=查詢|a=?|a=about)return 4; else if(a=入庫|a=+|a=in)return 5; else if(a=出庫|a=-|a=out)return 6; else if(a=幫助|a=?|a=help)return 8; else return 7;int total_count(cap &a,shirt &b,wardrobe &c) return a.r_count()+b.r_count()+c.r_count(); /Powered by X.Duke int main(void)/=輸入部分 int cho=1,i; /cho為

18、選擇計(jì)數(shù)器 接收choose函數(shù)的返回值 通過if語句判斷要執(zhí)行的語句 int ci=0,wi=0,si=0; /標(biāo)記 角標(biāo) float pci5=0.00,pwi5=0.00,psi5=0.00; /單價(jià) int cou_cap5=0,0,0,0,0,cou_shi5=0,0,0,0,0,cou_war5=0,0,0,0,0; /依次記錄帽子 襯衣 立柜的庫存量 string pla_cap5=0,0,0,0,0; /保存帽子產(chǎn)地信息 string pla_war5=0,0,0,0,0; /保存立柜產(chǎn)地信息 string pla_shi5=0,0,0,0,0; /保存襯衣產(chǎn)地信息 strin

19、g col5=0,0,0,0,0; /保存顏色信息 string s_mat5=0,0,0,0,0; /保存襯衣_布料信息 string c_mat5=0,0,0,0,0; /保存帽子_布料信息 string woo5=0,0,0,0,0; /保存木料信息 string sty5=0,0,0,0,0; /保存樣式信息 string temp,temp1=0,temp2=0,temp3=0; /臨時(shí)寄存字符 int cou_temp;float p_temp; /臨時(shí)記錄初始值 int loop_sw=0; /事件標(biāo)記點(diǎn) 為0時(shí)不跳過 非0時(shí)跳過 /提示性語句 cout現(xiàn)在對3種商品 n襯衣(s

20、hirt),帽子(cap),立柜(wardrobe) n實(shí)現(xiàn)基本管理功能endln=首先進(jìn)行初始化工作=endl; cout即將錄入商品名稱(也可以是括號中的英文)輸入0結(jié)束endl; while(cho!=0) /分類輸入 couttemp; cho=choose(temp); if(cho=1) /shirt -place material if(si=5)cout超過最大數(shù)量輸入限制,跳出;continue; /溢出保護(hù) coutp_temp; /設(shè)置帽子單價(jià) cout輸入產(chǎn)地 布料 初始數(shù)量 并以空格隔開temp1temp2cou_temp; for(i=0;i=si;i+) /用于判

21、斷是否與之前輸入的有沖突 if(temp1=pla_shii&temp2=s_mati) if(p_temp!=psisi)coutpsisi; cout與之前輸入的商品重復(fù),將數(shù)量疊加place material style if(ci=5)cout超過最大數(shù)量輸入限制,跳出;continue; coutp_temp; / cout輸入產(chǎn)地 布料 樣式 初始數(shù)量 并以空格隔開temp1temp2temp3cou_temp; for(i=0;i=ci;i+) if(temp1=pla_capi&temp2=c_mati&temp3=styi) if(p_temp!=pcici)coutpcic

22、i; cout與之前輸入的商品重復(fù),將數(shù)量疊加place wood color if(wi=5)cout超過最大數(shù)量輸入限制,跳出;continue; coutp_temp; cout輸入產(chǎn)地 木料 顏色 初始數(shù)量 并以空格隔開temp1temp2temp3cou_temp; for(i=0;i=wi;i+) if(temp1=pla_wari&temp2=wooi&temp3=coli) if(p_temp!=pwiwi)coutpwiwi; cout與之前輸入的商品重復(fù),將數(shù)量疊加endl;cou_wari+=cou_temp; loop_sw=1; if(loop_sw=0) pwiwi

23、=p_temp; pla_warwi=temp1; woowi=temp2; colwi=temp3; cou_warwi+=cou_temp; cou_temp=0; loop_sw=0; if(cho=7)cout輸入有誤 重新; cho=1; /選擇計(jì)數(shù)器重新設(shè)置 /開始初始化工作 shirt s_5= shirt(psi0,cou_shi0,pla_shi0,s_mat0), shirt(psi1,cou_shi1,pla_shi1,s_mat1), shirt(psi2,cou_shi2,pla_shi2,s_mat2), shirt(psi3,cou_shi3,pla_shi3,s

24、_mat3), shirt(psi4,cou_shi4,pla_shi4,s_mat4) ; cap c_5= cap(pci0,cou_cap0,pla_cap0,c_mat0,sty0), cap(pci1,cou_cap1,pla_cap1,c_mat1,sty1), cap(pci2,cou_cap2,pla_cap2,c_mat2,sty2), cap(pci3,cou_cap3,pla_cap3,c_mat3,sty3), cap(pci4,cou_cap4,pla_cap4,c_mat4,sty4) ; wardrobe w_5= wardrobe(pwi0,cou_war0,p

25、la_war0,woo0,col0), wardrobe(pwi1,cou_war1,pla_war1,woo1,col1), wardrobe(pwi2,cou_war2,pla_war2,woo2,col2), wardrobe(pwi3,cou_war3,pla_war3,woo3,col3), wardrobe(pwi4,cou_war4,pla_war4,woo4,col4) ;/輸入部分結(jié)束/=功能部分 int total=0,totalcap=0,totalshi=0,totalwar=0; /用于累計(jì)庫存總數(shù) float ptotal=0.00,stotal=0.00,ctot

26、al=0.00,wtotal=0.00; /用于累計(jì)總價(jià)值 int j=0; cout初始化結(jié)束 啟用基本功能endln輸入查詢或about 查詢當(dāng)前庫存情況endl; cout輸入+或in或 入庫 實(shí)現(xiàn)入庫功能endl; cout輸入-或out或 出庫 實(shí)現(xiàn)出庫功能endl;while(cho!=0) if(loop_sw=0)couttemp; cho=choose(temp); if(cho=7)cout輸入有誤 重新; if(cho=4) /查詢功能 for(i=0;isi;i+)s_i.print(); for(i=0;ici;i+)c_i.print(); for(i=0;iwi;

27、i+)w_i.print(); for(i=0;i5;i+)total+=total_count(c_i,s_i,w_i); ptotal+=(s_i.total_price()+c_i.total_price()+w_i.total_price(); stotal+=s_i.total_price(); ctotal+=c_i.total_price(); wtotal+=w_i.total_price(); totalcap+=c_i.r_count(); totalshi+=s_i.r_count(); totalwar+=w_i.r_count(); coutn襯衣總庫存:totals

28、hit價(jià)值:stotaln帽子總庫存:totalcapt價(jià)值:ctotaln立柜總庫存:totalwart價(jià)值:wtotaln所有商品庫存總量:totalt總價(jià)值:ptotalendl; if(loop_sw=1)break; if(cho=5) /入庫 while(cho!=0) couttemp; cho=choose(temp); if(cho)couttemp中有以下小類 輸入編號進(jìn)行入庫操作; if(cho=1) for(i=0;isi;i+) coutn編號 it產(chǎn)地 pla_shii t布料:s_matit現(xiàn)有 s_i.r_count(); coutj; if(si=0)cout無商品endl; else while(j=si)coutj; coutcou_temp;s_j.in_something(cou_temp); if(cho=2) for(i=0;ici;i+) coutn編號 it產(chǎn)地 pla_capit布料:c_

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論