最新c++实验8-继承与派生上机练习题

上传人:仙*** 文档编号:141457733 上传时间:2022-08-24 格式:DOC 页数:13 大小:127.50KB
收藏 版权申诉 举报 下载
最新c++实验8-继承与派生上机练习题_第1页
第1页 / 共13页
最新c++实验8-继承与派生上机练习题_第2页
第2页 / 共13页
最新c++实验8-继承与派生上机练习题_第3页
第3页 / 共13页
资源描述:

《最新c++实验8-继承与派生上机练习题》由会员分享,可在线阅读,更多相关《最新c++实验8-继承与派生上机练习题(13页珍藏版)》请在装配图网上搜索。

1、精品资料c+实验8-继承与派生上机练习题.1. 定义一个哺乳动物类Mammal,并从中派生出一个狗类Dog,下面给出Mammal类的定义,要求:(1) 添加Dog类的颜色数据成员,访问属性为私有,通过SetColor和GetColor成员函数来对颜色进行设置和获取。(2) 分别为基类和派生类添加相应的构造函数(有参、无参)和析构函数,并进行测试。class Mammalprotected:int itsAge;int itsWeight;public:int GetAge()return itsAge;void SetAge(int age) itsAge=age;int GetWeight(

2、) return itsWeight;void SetWeight(int weight) itsWeight= weight; ;class Dog : public Mammal/定义Dog类的数据成员和成员函数;改:#include #include using namespace std;class Mammalprotected:int itsAge;int itsWeight;public:Mammal();Mammal();int GetAge()return itsAge;void SetAge(int age) itsAge=age;int GetWeight() retur

3、n itsWeight;void SetWeight(int weight) itsWeight= weight; ;class Dog : public Mammalprotected:char itscolor20;public:Dog();void Setcolor(char *color) strcpy(itscolor,color); void getcolor()cout狗的颜色itscolorendl;/定义Dog类的数据成员和成员函数;/Mammal:Mammal()int age1,weight1;cout请输入动物的年龄:age1;SetAge(age1);cout请输入动

4、物的体重:weight1;SetWeight(weight1);Mammal:Mammal()coutDestructor called.endl;Dog:Dog()char color20;cout请输入狗的颜色:color;Setcolor(color);cout狗的颜色itscolor体重GetWeight()年龄GetAge()endl;void main()Dog dog1;(4)设计人员基类Person。其成员包括:数据成员:姓名(字符数组)、性别(字符数组)和年龄(整型)成员函数:SetPerson,设置人员数据函数;DisplayPerson,显示人员数据函数;设计派生类1:T

5、eacher,派生于Person。新增成员包括:数据成员:职称(字符数组)、教研室(字符数组)和所授课程(字符数组)成员函数:SetTeacher,设置数据成员函数;DisplayTeacher,显示数据成员函数;设计派生类2:Student,派生于Person。新增成员包括:数据成员:专业(字符数组)、班级(字符数组)和类别(int)其中类别取值:1(本科生)、2(硕士生)、3(博士生)成员函数:SetStudent,设置数据成员函数;DisplayStudent,显示数据成员函数;设计派生类3:PostDoctor(博士后),多重继承于Student与Teacher。新增成员包括:数据成员

6、:无成员函数:SetPostDoctor,设置数据成员函数;DisplayPostDoctor,显示数据成员函数;主函数:输入并输出一个教师、一个本科生、一个博士后数据。#include #include using namespace std;#define n 20/类的定义class Personprotected:char namen;char sexn;int age;public:Person();void setperson();void displayperson();class Teacher :virtual public Personprotected:char jobn

7、;char roomn;char subjectn;public :Teacher();void setteacher();void displayteacher();class Student:virtual public Personprotected:char majorn;char banjin;int leibie;public :Student();void setstudent();void displaystudent();class Postdoctor:public Teacher,public Studentpublic :Postdoctor();void setpos

8、tdoctor();void displaypostdoctor();/结构函数Person:Person()setperson();Teacher:Teacher()setteacher();Student:Student()setstudent();Postdoctor:Postdoctor()/设置数据/void Person:setperson()cout*name;cout*sex;cout*age;void Teacher:setteacher()cout*job;cout*room;cout*subject;void Student:setstudent()cout*major;

9、cout*banji;cout*leibie;/数据显示/void Person:displayperson()cout姓名:name性别:sex年龄:age;void Teacher:displayteacher()displayperson();cout职称:job教研室:room所授课程:subjectendl;void Student:displaystudent()displayperson();cout专业:major班级:banji类别:leibieendl;void Postdoctor:displaypostdoctor()displayperson();cout职称:job教研室:room所授课程:subject专业:major班级:banji类别:博士后endl;/void main()cout您正在输入一个老师的信息:endl;Teacher t1;cout*syy割endl;cout您正在输入一个学生的信息:endl;Student s1;cout*syy割endl;cout您正在输入一个博士后的信息:endl;Postdoctor p1;cout*syy割endl;coutendl;t1.displayteacher();coutendl;s1.displaystudent();coutendl;p1.displaypostdoctor();

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