ImageVerifierCode 换一换
你正在下载:

实验4.docx

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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

实验4.docx

1、实验4面向对象程序设计C+实验报告书班级: 姓名: 学号: 课程名称面向对象程序设计C+实验项目实验四、类的继承与派生实验项目类型验证演示综合设计指导教师项宝卫成 绩一、 实验目的1掌握继承与派生的关系及方法;2在派生类构造函数中初始化基类成员;3多重继承的方法。二、实验步骤1、定义1个CPerson类,其属性有:姓名、性别和年龄。2、从CPerson类派生出CStudent类,增加属性:学号、入学时间和入学成绩;3、从CPerson类派生出CTeacher类,添加属性:职务、部门和工作时间;4、由CStudent类派生出CGraduate类,添加属性:研究方向和导师;5、由CGraduate

2、和CTeacher共同派生出在职研究生类CGradonWork6、对上述类分别定义其中的构造函数和输出函数,并在主函数中定义对象进行测试,测试的代码大致如下:CGradonWork gradonWork(tzcer,男,33,15,2003,335,computer,tangmin,jsj,sxxy,2002-8);gradonWork.Print();其中CGradonWork 的构造函数参数分别对应的属性是一个在职的研究生对象,其姓名为tzcer,性别为男,33岁,学号15,2003年读在职,入学成绩335,就读计算机方向,其导师tangmin,同时tzcer也是sxxy部门,jsj专业的

3、老师,2002-8月入职。 要求记录输出信息并说明实验步骤和解决方法!7、问题:何为继承二义性,在本实验中哪个地方存在二义性,你是如何解决的?三、上机过程原始记录(源程序等)1、定义1个CPerson类,其属性有:姓名、性别和年龄。classpersoncharName10; charSex; intAge;public: person(char*name,intage,charsex) strcpy(Name,name); Age=age; Sex=(sex=m?m:f); virtualvoidset() intage; charname10; charsex; cout请输入姓名:nam

4、e; strcpy(Name,name); cout请输入年龄:age; Age=age; cout请输入性别:sex; Sex=sex; virtualvoidshowinfo() coutNametAgetSexendl; coutendlendl; system(pause); ;2、从CPerson类派生出CStudent类,增加属性:学号、入学时间和入学成绩;classstudent:publicpersonintnumber; /定义学好inttime; /定义入学时间floatscore1;/定义入学成绩public: student(intnum,inttime,floatsc

5、o1,char*name,intage,charsex):person(name,age,sex) score1=sco1; voidset() intsco3; person:set(); coutnumber;number=number;couttime;time=time; coutsco0;score1=sco0; voidshowinfo() coutnumberttimetscore1endl; ; 3、从CPerson类派生出CTeacher类,添加属性:职务、部门和工作时间;classteacher:publicperson chartitle30;/定义职称 chardept

6、30;/定义部门 intlength;/定义工作时间public: teacher(char*tit,char*de,intlen,char*name,charsex,intage):person(name,age,sex) strcpy(title,tit); strcpy(dept,de); length=len; voidset() chartit30;/定义职称 charde30;/定义部门 intlen;/定义工作时间 person:set(); cout职称:tit; strcpy(title,tit); cout部门:de; strcpy(dept,de); cout工作时间:l

7、en; length=len; voidshowinfo() couttitletdepttlengthendl; ;4、由CStudent类派生出CGraduate类,添加属性:研究方向和导师;classgraduate:publicstudent charRFields30;/定义研究方向chartutor30;/定义导师 public: graduate(char*RF,char*tu,int*number,inttime,floatscore1):student(number,time,score1) strcpy(RFields,RF); strcpy(tutor,tu); void

8、set() charRF30;/定义研究方向 chartu30;/定义导师 student:set(); cout研究方向:RF; strcpy(RFields,RF); cout导师:tu; strcpy(tutor,tu); voidshowinfo() coutRFieldsttutorendl; ;5、由CGraduate和CTeacher共同派生出在职研究生类CGradonWorkclassGradonWork:publicGraduate,publicTeacher ;6、对上述类分别定义其中的构造函数和输出函数,并在主函数中定义对象进行测试,测试的代码大致如下:CGradonWo

9、rkgradonWork(tzcer,男,33,15,2003,335,computer,tangmin,jsj,sxxy,2002-8);gradonWork.Print();其中CGradonWork的构造函数参数分别对应的属性是一个在职的研究生对象,其姓名为tzcer,性别为男,33岁,学号15,2003年读在职,入学成绩335,就读计算机方向,其导师tangmin,同时tzcer也是sxxy部门,jsj专业的老师,2002-8月入职。要求记录输出信息并说明实验步骤和解决方法!主要代码:#include#include#include#includeclasspersoncharName

10、10; charSex; intAge;public: person(char*name,intage,charsex) strcpy(Name,name); Age=age; Sex=(sex=m?m:f); virtualvoidset() intage; charname10; charsex; cout请输入姓名:name; strcpy(Name,name); cout请输入年龄:age; Age=age; cout请输入性别:sex; Sex=sex; virtualvoidshowinfo() coutNametAgetSexendl; coutendlendl; system(

11、pause); ;classstudent:publicpersonintnumber; /定义学好inttime; /定义入学时间floatscore1;/定义入学成绩public: student(intnum,inttime,floatsco1,char*name,intage,charsex):person(name,age,sex) score1=sco1; voidset() intsco3; person:set(); coutnumber;number=number;couttime;time=time; coutsco0;score1=sco0; voidshowinfo()

12、 coutnumberttimetscore1endl; ; classteacher:publicperson chartitle30;/定义职称 chardept30;/定义部门 intlength;/定义工作时间public: teacher(char*tit,char*de,intlen,char*name,charsex,intage):person(name,age,sex) strcpy(title,tit); strcpy(dept,de); length=len; voidset() chartit30;/定义职称 charde30;/定义部门 intlen;/定义工作时间

13、person:set(); cout职称:tit; strcpy(title,tit); cout部门:de; strcpy(dept,de); cout工作时间:len; length=len; voidshowinfo() couttitletdepttlengthendl; ;voidhhh() couttttt1.基类基本情况:endl; couttttt2.老师类基本情况:endl; couttttt3.学生类基本情况:endl; couttttt0.退出endl; couttttt请选择种类(03)endl; coutendl请选择:;intmain() person*d3;per

14、sonper(wenpiao,20,f); studentstu(11290033,2011,523,wenpiao,20,f); teachertea(jiaoshou,dianxin,20,chenxiaochang,40,f); d0=&per; d1=&stu; d2=&tea; intchoice; couttttt1.基类基本情况:endl; couttttt2.老师类基本情况:endl; couttttt3.学生类基本情况:endl; couttttt0.退出endl; couttttt请选择种类(03)endl; coutendlchoice; system(cls); if(

15、choice=0&choiceset(); dchoice-1-showinfo(); break; case2:dchoice-set(); dchoice-showinfo(); break; case3:dchoice-2-set(); dchoice-2-showinfo(); break; case0:exit(0); break; else cout错误输入!请重新选择:endl; hhh(); 运行记录:7、问题:何为继承二义性,在本实验中哪个地方存在二义性,你是如何解决的?一般说来,在派生类中对基类成员的访问应该是唯一的,但是,由多继承情况下,可能造成对基类中某成员的访问出现了不唯一的情况,则称为对基类成员访问的二义性问题。使用基类classApublic:voidf();classBpublic:voidf();voidg();classC:publicA,publicB;public:voidg();voidh();/这里派生类A和基类B中都有f(),基类B和基类C中都有g(),所以访问出现二义使用基类名为避免这种二义:obj.A:f();/A中的f()obj.B:f();/B中的f()这种使用基类名来控制成员访问的规则称为支配原则obj.g();/隐含用C的g()obj.B:g();/用B的g()四、上机结果及分析1、2、34

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

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