最新天津理工大学C++实验三

上传人:lis****211 文档编号:56135688 上传时间:2022-02-20 格式:DOC 页数:21 大小:218.50KB
收藏 版权申诉 举报 下载
最新天津理工大学C++实验三_第1页
第1页 / 共21页
最新天津理工大学C++实验三_第2页
第2页 / 共21页
最新天津理工大学C++实验三_第3页
第3页 / 共21页
资源描述:

《最新天津理工大学C++实验三》由会员分享,可在线阅读,更多相关《最新天津理工大学C++实验三(21页珍藏版)》请在装配图网上搜索。

1、天津理工大学计算机科学与技术学院实验报告至学年第学期课程名称C+程序设计学号学生姓名年级专业教学班号实验地点实验时间年月日第节至 第节主讲教师辅导教师实验(三)实验名称派生与继承软件环境C+visual硬件环境无实验目的(1) 理解继承的含义,掌握派生类的定义方法和实现;(2) 理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问 继承层次中的各种类成员;(3) 理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员;实验内容(应包括实验题目、实验要求、实验任务等)1.# in elude #define PI 3.14159c

2、lass Point/ 定义点”类int x, y;public:Point(int a=0, int b=0)x=a; y=b; void ShowPoi nt()coutPoi nt:(x,y)n;int Getx() return x; int Gety()return y; void Setxy(i nt 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 = ra; void

3、Setr(i nt ra) r = ra; double Area( )/求圆的面积 return Pl*r*r; void Move(i nt x_offset, i nt y_offset) / 将圆心坐标平移int x仁Getx( );/存取基类的私有成员int y仁 Gety( );/ D/ E/ FGx1 += x_offset; y1 += y_offset; Setxy(x1, y1);void ShowCircle()ShowPoi nt();cout Radius: rt:coutArea: Area( )e ndl; ;void mai n()/重新置圆心坐标/重新置半径值

4、Circle c(1, 1, 1); c.ShowCircle(); c.Move(1,2); c.ShowCircle();c.Setxy(4, 5);c.Setr(2); c.ShowCircle();实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)S3RC:UsersdellDesktopC+S?= |g-DebugRadius: 13,14159Po int:Fadius: 1Area: 3_14159Point:Fladiu : 25664Press any keyto continue(1)记录程序的运行结果Point(1,1)Radius: 1Area: 3.1

5、4159Point(2, 3)Radius: 1Area: 3.14159Point(4, 5)Radius: 2Area: 12.5664(2)测试能否将move函数体改写为不可以,因为x=x+x_offset;y=y+y_offset;x和y是Point类的私有(private)成员。2.# in elude #define PI 3.14159class Pointprotected:/Aint x, y;public:Poin t(i nt a=0, int b=0) x=a; y=b; void ShowPoi nt() coutPoi nt:(x,y)n;int Getx( ) r

6、eturn x; int Gety( ) return y; void Setxy(i nt a, int b) x=a; y=b; ;class Radiusprotected:int r;public:Radius(i nt ra=0) r = ra; void Setr(i nt ra) r = ra; int Getr( ) return r; ;class Circle : public Point, public Radiuspublic:Circle(int x, int y, int ra): Point(x, y) , Radius(ra) D double Area() r

7、eturn PI* r* r; 直接访问基类的保护成员void Move(i nt x_offset, int y_offset) x += x_offset; y += y_offset; void ShowCircle()ShowPoi nt();coutRadius: rt;coutArea: Area( )Radius: 1Apeft: 3-14159Po int : C2, 3RaJ.u.s ! 13-14159Po in t : 4,Radius: 2ftrea = 12 .S&4Press anyto continue(1) 记录程序的运行结果Point:(1,1)Radius:

8、 1Area: 3.14159Point:(2,3)Radius: 1Area: 3.14159Point:(4,5)Radius: 2Area: 12.5664(2) 为什么可以直接使用x,y,x和y是Point类的保护(protected)成员(3) 如果x,y在基类中是私有的行吗? 不行#in elude class Baselprotected:int data1;public:Base1(i nt a=0) data1 = a;coutBase Constructor1n;Base1() coutBase Destructor1n; ;class Base2protected:int

9、 data2;public:Base2(i nt a=0) data2 = a; coutBase Co nstructor2n;Base2()coutBase Destructor2n;;class Derived: public Basel, public Base2int d;public:Derived(i nt x, int y, int z):Base1(x), Base2(y) d=z; coutDerived Con structor n;Derived() coutDerived Destructorn; void Show()coutdata1,data2,de ndl;

10、;void mai n() Derived c(1,2, 3);c.Show();(1)记录程序的运行结果Base ConstructorBase ConstructorDerived Constructor1,2,3Derived DestructorBase Destructor2Base Destructorl*C:U sersdel lDesktopC+ 实捡三第三题ase Constructorl aae Constructo eriued Constructorerived Destructor ase Destructoi*2 ase Destructorl pcss any k

11、ev to continue#in elude class Baselprotected:int datal;public:Base1(i nt a=8) data1 = a;coutdata1. Base Con structor1 n;Base1() coutdata1, Base Destructor1n; ;class Base2protected:int data2;public:Base2(int a=9) data2 = a;coutdata2. Base Constructor2n;Base2() coutdata2, Base Destructor2n; ;class Der

12、ived: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 Con struct orn;Derived()coutDerived Destructorn;void Show()coutdata1,data2,de ndl;void mai n() Derived c(1,2, 3);c.Show();(1)记录程序的运行结果1, Base Construct。2, Bas

13、e Constructor3, Base Constructorl4, Base ConstructorlDerived Constructor1,2,3Derived Destructor4, Base Destructorl3, Base Destructorl2, Base Destructor21, Base DestructorlC:UsersdellDesktopC+四題Hl P BaseConstructor!IK. BaseiConstruetor23, Be:亡Const uctor-1*1, BaseConstructorl91)rConstpuctov|H1尸艺戸/ivc

14、 dDes七片口心上。!*4.DestructorlM3,Destructorlm2 , BaseDes 七 BaseDestructorlPress anyto continue5. #i nclude#in cludeusing n amespace std;class baseprotected:float price; / 单价stri ng place;产地int count; / 库存量public:base(float &p,i nt &c,stri ng &pl) price=p; coun t=c; place=pl; void in _someth in g(i nt ad

15、d_c nt) coun t+=add_c nt; void out_someth in g(i nt del_c nt)if(del_cntcount)count=O; / 变 0 保护 小于 0 的按 0 计算else coun t-=del_c nt;double total_price() return price*co un t; int r_co un t() return count; ;class shirt:public baseprotected:string material;public:shirt(float &p ,int &c ,stri ng &pl ,stri

16、 ng &m):base( p , c, pl) material=m; void prin t()coutn 衬衣t 产地:placet 材料:materialn 库存量:countt 单价:pricet 总价格total_price()endl;class wardrobe:public baseprotected:stri ng wood;string color;public:wardrobe(float &p ,int &c ,stri ng &pl ,stri ng &w,stri ng &co ):base(p,c,pl)wood=w; color=co;void prin t(

17、)coutn 立柜t 产地:placet 颜色:colort 木料:woodn 库存 量:countt 单价:pricet 总价格total_price()endl;;class cap:public shirtprotected:stri ng style;public:cap(float &p ,int &c ,string &pl ,string &m,string &st ):shirt(p,c,pl,m)style=st;void prin t()coutn 帽子t 产地:placet 材料:material样式:stylen 库存量:countt 单价:”pricet 总价格tot

18、al_price()endl;int choose(string a) /用于判断下一步执行的命令0.结束1.衬衣2.帽子3立柜4.查询5入库6.出库7.出错重输if(a=0)return 0; / 结束else if(a=衬衣|a=shirt)return 1;else if(a=帽子|a=cap)return 2;else if(a=立柜|a=wardrobe)return 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=

19、帮助|a=?|a=help)return 8;else return 7;int total_co un t(cap &a,shirt &b,wardrobe &c) return a.r_co un t()+b.r_co un t()+c.r_co un t();/Powered by X.Dukeint main( void)/=输入部分int cho=1,i;cho为选择计数器 接收choose函数的返回值 通过if语句判断要执行的语句int ci=0,wi=0,si=0;标记 角标float pci5=0.00,pwi5=0.00,psi5=0.00; / 单价int cou_cap5

20、=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; /string pla_war5=0,0,0,0,0; /string pla_sh i5=0,0,0,0,0; /string col5=0,0,0,0,0;string s_mat5=0,0,0,0,0;string c_mat 5=0,0,0,0,0;string woo5=0,

21、0,0,0,0;string sty5=0,0,0,0,0;string temp,temp仁0,temp2=0,temp3=0; / 临时寄存字符事件标记点 为0时不跳过 非0时跳过int cou_temp;float p_temp; / 临时记录初始值int loop_sw=0;n实现基本管理功能 始 化 工 作提示性语句cout现在对 3种商品n衬衣(shirt),帽子(cap),立柜(wardrobe) e ndln=首 先=e ndl;cout即将录入商品名称(也可以是括号中的英文)输入0结束endl;while(cho!=0) / 分类输入cout temp;cho=choose(

22、temp);if(cho=1) /shirt -place materialif(si=5)cout超过最大数量输入限制,跳出;continue;/溢出保护coutp_temp; /设置帽子单价cout输入产地布料初始数量 并以空格隔开 temp1temp2cou_temp;for(i=0;i=si;i+)/用于判断是否与之前输入的有冲突if(temp1=pla_shii&temp2=s_mati) if(p_temp!=psisi)coutpsisi;cout与之前输入 的商品 重复,将数量叠加place material styleif(ci=5)cout超过最大数量输入限制,跳出;con

23、tinue;cout设置帽子单价:”;cinp_temp; /cout输入产地 布料 样式 初始数量 并以空格隔开 temp1temp2temp3cou_temp;for(i=0;i=ci;i+)if(temp1=pla_capi &temp2=c_mati &temp3=styi)if(p_temp!=pcici)coutpcici;cout与之前输入的商品重复,将数量叠加place wood color if(wi=5)cout超过最大数量输入限制,跳出 cout设置立柜单价:;cinp_temp;cout输入产地 木料 颜色 temp1temp2temp3cou_temp;for(i=0

24、;i=wi;i+)if(temp1=pla_wari&temp2=wooi &temp3=coli)if(p_temp!=pwiwi)coutpwiwi;cout与之前输入的商品重复,将数量叠加e ndl;cou_wari+=cou_temp; loop_sw=1;if(loop_sw=0)pwiwi=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; II选择计数器重新设置开始初始化工作shirt s_5=shir

25、t(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_mat3), shirt(psi4,cou_shi4,pla_shi4,s_mat4);cap c_5=cap(pci0,cou_c apO,pla_capO,c_matO,styO),cap(pci1,cou_c ap1,pla_cap1,c_mat1,sty1), cap(pci2,cou_cap2,pla_cap2,

26、c_mat2,sty2),cap(pci3,cou_c ap3,pla_cap3,c_mat3,sty3), cap(pci4,cou_cap4,pla_cap4,c_mat4,sty4);wardrobe w_5=wardrobe(pwi0,cou_war0,pla_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_w

27、ar4,pla_war4,woo4,col4);/输入部分结束/=功能部分int total=0,totalcap=0,totalshi=0,totalwar=0;用于累计库存总数float ptotal=0.00,stotal=0.00,ctotal=0.00,wtotal=0.00; / 用于累计总价值int j=0;cout初始化结束启用基本功能endln输入查询或about查询当前库存情况e ndl;cout输入+或in或入库实现入库功能endl;cout输入-或out或出库实现出库功能endl;while(cho!=0)if(loop_sw=0)cout请输入相应功能:;ci nte

28、mp;cho=choose(temp);if(cho=7)cout输入有误重新;if(cho=4) / 查询功能for(i=0;isi;i+)s_i.pri nt();for(i=0;ici;i+)c_i.pri nt();for(i=0;iwi;i+)w_i.pri nt();for(i=0;i5;i+)total+=total_cou nt(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

29、(); wtotal+=w_i.total_price(); totalcap+=c_i.r_co un t();totalshi+=s_i.r_co un t();totalwar+=w_i.r_co un t();coutn 衬衣总库存:totalshit 价值:stotaln 帽子总库存:totalcapt 价值:ctotaln 立柜总库存:totalwart价值:wtotaln 所有商品库存总量: totalt 总价值:ptotalendl;if(loop_sw=1)break;if(cho=5) / 入库while(cho!=0)cout输入要入库的商品种类(输入0结束):;cint

30、emp;cho=choose(temp);if(cho)couttemp中有以下小类输入编号进行入库操作;if(cho=1)for(i=0;isi;i+)coutn 编号it 产地pla_shiit 布料:s_matit 现有s_i.r_count();coutn输入商品编号:”;cinj;if(si=0)cout无商品endl;elsewhile(j=si)coutj;cout cou_temp;s_j.i n_someth in g(cou_temp);if(cho=2)for(i=0;ici;i+)coutn 编号it 产地pla_capit 布料:c_mati样式:styit 现有c_

31、i.r_count();if(ci=0)cout无商品endl;elsecoutn输入商品编号:”;cinj;while(j=ci)coutj;cout cou_temp;c_j.i n_someth in g(cou_temp);if(cho=3)for(i=0;iwi;i+)coutn 编号it 产地pla_warit 木料:wooi颜色:colit 现有w_i.r_count();if(ci=0)cout无商品endl;elsecoutn输入商品编号:”;cinj;while(j=wi)coutj;cout cou_temp;w_j.i n_someth in g(cou_temp);i

32、f(cho=7)cout有误重新;cho=5;if(cho=6) / 出库while(cho!=0)cout输入要出库的商品种类(输入0结束):;cintemp; cho=choose(temp);if(cho)couttemp中有以下小类输入编号进行出库操作; if(cho=1)for(i=0;isi;i+)coutn 编程it 产地pla_shiit 布料:s_matit 现有s_i.r_count();coutn输入商品编号:”;cinj;while(j=si)coutj;cout cou_temp;s_j.out_someth in g(cou_temp);if(cho=2)for(i

33、=0;ici;i+)coutn 编程it 产地pla_capit 布料:c_mati 样式:styit 现有c_i.r_count();coutn输入商品编号:”;cinj;while(j=ci)coutj;cout cou_temp;c_j.out_someth in g(cou_temp); if(cho=3) for(i=0;iwi;i+) coutn 编程it 产地pla_warit 木料:wooi 精品文档颜色:colit 现有w_i.r_count();couti;while(j=wi)coutj;cout cou_temp;w_j.out_somethi ng(cou_temp)

34、;if(cho=7)cout有误重新;cho=6;if(cho=0)loop_sw=1;cho=4;cout ,卡冒-f_ 级 521waid-iobe 宀现基本管理兔罷霹餾樹衣入严地菊始数量并叹空格隔开10R務帽子希料樺式初始数曼井以空格隔开I舌 50逮立柜朋;100 肘斗颜色初始数量并叹空格隔开20了入查询或About查询岂刖库存情况 入+或命或 人库 至现入库噬 也二或o吐或i芝圭现出库芳罷 討丽入相应动冃总:查谅产地100产地150产地=20广豐价:广畫价:值 i!4i5127:9总歸冋出屏的H官官亠商入要Ml000S0112s =IT+材料棉质35总、价格35四材料匕棉样式;鸭舌40.吕、价榕畐000颜色匕红色木料红木100总、价格2 00目:入8s歪利输入曼A库的商品种無输人0结束八附录(可包括源程序清单或其它说明)

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!