ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:17.91KB ,
资源ID:5877273      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5877273.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(继承与多态性习题参考答案.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

继承与多态性习题参考答案.docx

1、继承与多态性习题参考答案继承与多态性习题参考答案 作者: 日期: C+程序设计语言习题及实验指导第8章 继承与多态性习题参考答案8.3 习题8.3.1 选择题题号12345678910答案CDADABCBDD题号11121314151617181920答案DDACBCCCAC8.3.2 填空题1 (1) a、b、c、x、y、z (2) b、y (3) c、z (4) a、x (5) b、c、x、y、z (6) b、y (7) y (8) b、c、z (9) a、x (10) b、c、x、y、z (11) y (12) y (13) z (14) a、b、c、x (15) b、c、x、y、z (

2、16) y2 私有3 抽象4 虚基5 (1) 基类 (2) 对象6 (1) 静态 (2) 编译 (3) 虚函数7 (1) 2 1 (2) 0 58 (1) 100 200 300 30 (2) 10 100 (3) 20 2009 (1) 5 10 (2) end. (3) 20 1010(1) classA classB (2) end. (3) classB classA11(1) 10 (2) build B (3) build C (4) release A12(1) class B (2) class C (3) class D (4) 513(1) 5 5 (2) 20 2014(

3、1) 10 (2) 10 2015(1) 1 (2) 3 (3) 5 (4) 100 1016(1) B:f() (2) B:fun() (3) A:f() (4) B:fun()17(1) 姓名陈涛年薪6.8万元。 (2) 姓名李原 (3) 姓名李原月工资4000元18(1) 110 (2) 222019(1) protected 或public (2) Base1(s1),Base2(s2) 或Base2(s2),Base1(s1) (3) Base1:str (4) test.print()20(1) virtual void show()=0 (2) C(int,int,int) (注

4、:参数可有任一名称) (3) b1(y) (4) &t8.3.3 编程题1求长方形的面积和长方体的表面积、体积#includeclass Rectangleprotected: float L; / 长方形的长 float W; / 长方形的宽 float area; / 长方形的面积public: Rectangle(float L1,float W1) L=L1; W=W1; void function() area=L*W; void show() cout长为L,宽为W的长方形面积为areaendl; ; class Cuboid:public Rectangle float H; /

5、 长方体的高 float volume; / 长方体的体积public: Cuboid(float a,float b,float c):Rectangle(a,b) H=c; void function() area=(L*W+L*H+W*H)*2; void fun() volume=L*W*H; void show() cout长为L,宽为W,高为H的长方体表面积为area,体积为volumeendl; ; void main() Rectangle r(2,3); r.function(); r.show(); Cuboid c(2,3,4); c.function(); c.fun(

6、); c.show();2产生并输出圆桌信息#include#includeclass Circleprotected: double radius; double area;public: Circle(double r) radius=r; ; class Tableprotected: double height;public: Table(double h) height=h; ; class Roundtable:public Table,public Circle char *color;public: Roundtable(double h,double r,char c):Ci

7、rcle(r),Table(h) color=new charstrlen(c)+1; strcpy (color, c); void fun() area=3.14*radius*radius; void show() cout高度:height米endl; cout面积:area平方米endl; cout颜色:colorendl; Roundtable() delete color; ;void main() Roundtable rt(0.75,1.1,黑色); rt.fun(); rt.show();3计算企业的经营税#include#includeclass Taxprotected

8、: char name50; / 企业名称 double income; / 经营收入 double taxes; / 税金public: Tax(char *n,double in) strcpy(name,n); income=in; virtual void fun()=0; void show() coutname的经营收入为income万元,税金为taxes万元n; ; class Service:public Taxpublic: Service(char *n,double in):Tax(n,in) void fun() taxes=income*5/100; ; class

9、Fabrication:public Taxpublic: Fabrication(char *n,double in):Tax(n,in) void fun() taxes=income*17/100; ;void print(Tax &t) t.fun(); t.show(); void main() char name50; int income; coutname; coutincome; Service s(name,income); print(s); coutname; coutincome; Fabrication f(name,income); print(f);4把原始体育

10、成绩转换为等第#includeclass Sportsprotected: int item; / 项目 char name20; / 姓名 double score; / 原始成绩 char grade10; / 评定等第public: Sports(int i,char *n) item=i; strcpy(name,n); coutscore; void show() coutname的item米成绩为score秒,等第为gradeendl; virtual void change()=0; class Run50:public Sportspublic: Run50(int i,cha

11、r *n):Sports(i,n) void change() if(score=6.50)strcpy(grade,优秀); else if(score=6.70)strcpy(grade,良好); else if(score=7.10)strcpy(grade,及格); else strcpy(grade,不及格); ; class Run100:public Sportspublic: Run100(int i,char *n):Sports(i,n) void change() if(score=13.10)strcpy(grade,优秀); else if(score=13.70)s

12、trcpy(grade,良好); else if(score=14.90)strcpy(grade,及格); else strcpy(grade,不及格); ; class Run1000:public Sportspublic: Run1000(int i,char *n):Sports(i,n) void change() if(score=203.00)strcpy(grade,优秀); else if(score=213.00)strcpy(grade,良好); else if(score=233.00)strcpy(grade,及格); else strcpy(grade,不及格);

13、 ; class Run1500:public Sportspublic: Run1500(int i,char *n):Sports(i,n) void change() if(score=323.00)strcpy(grade,优秀); else if(score=337.00)strcpy(grade,良好); else if(score=365.00)strcpy(grade,及格); else strcpy(grade,不及格); ; void main() Sports *s; int it; coutit; char na20; coutna; if(it=50) Run50 r

14、50(it,na); s=&r50; s-change(); s-show(); else if(it=100) Run100 r100(it,na); s=&r100; s-change(); s-show(); else if(it=1000) Run1000 r1000(it,na); s=&r1000; s-change(); s-show(); else if(it=1500) Run1500 r1500(it,na); s=&r1500; s-change(); s-show(); else cout输入项目错误n; exit(0); 8.4 实验指导8.4.1 派生类的定义与使用

15、:设计一个程序求三角函数的值。#include#includeclass Trigonometricprotected: double arc; double value;public: Trigonometric(int t) arc=t%360*3.14159/180; value=0; ;class Sine:public Trigonometricpublic: Sine(int t):Trigonometric(t) double f1(double x,int n) double t=1; for(int i=0;i=1e-6); void show() coutsin(arc)=

16、valueendl; ;void main() int d; coutd; Sine sin(d); sin.function(); sin.show();8.4.2 虚函数实现运行多态性:设计一个程序判断某人是否为优秀教师或优秀学生。#includeclass Peopleprotected: char category20; / 人员类别 char name10; / 姓名 int num; / 分数或论文数 int result; / 结论,1为优秀,0为不优秀public: People( ) coutcategory; coutname; void show() if(result)

17、coutname是优秀categoryendl; else coutname不是优秀categoryendl; virtual void inputnum()=0; virtual void isgood( )=0; ; class Student:public Peoplepublic: void inputnum() coutnum; void isgood( ) result=(num90)? 1 : 0; ; class Teacher:public Peoplepublic: void inputnum( ) coutnum; void isgood( ) result=(num5)? 1 : 0 ; ; void main() People *p; Student s; p=&s; p-inputnum(); p-isgood(); p-show(); / A Teacher t; p=&t; p-inputnum(); p-isgood(); p-show(); / B

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1