收藏 分销(赏)

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

上传人:快乐****生活 文档编号:4375571 上传时间:2024-09-14 格式:DOC 页数:6 大小:21.50KB
下载 相关 举报
c++实验8-继承与派生上机练习题.doc_第1页
第1页 / 共6页
c++实验8-继承与派生上机练习题.doc_第2页
第2页 / 共6页
c++实验8-继承与派生上机练习题.doc_第3页
第3页 / 共6页
c++实验8-继承与派生上机练习题.doc_第4页
第4页 / 共6页
c++实验8-继承与派生上机练习题.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

1、1. 定义一个哺乳动物类Mammal,并从中派生出一个狗类Dog,下面给出Mammal类得定义,要求:(1) 添加Dog类得颜色数据成员,访问属性为私有,通过SetColor与GetColor成员函数来对颜色进行设置与获取。(2) 分别为基类与派生类添加相应得构造函数(有参、无参)与析构函数,并进行测试。class Mammalprotected:int itsAge;int itsWeight;public:int GetAgereturn itsAge;void SetAge(int age) itsAge=age;int GetWeight return itsWeight;void S

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

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

4、MammalcoutDestructor called、endl;Dog:Dogchar color20;cout请输入狗得颜色:color;Setcolor(color);cout狗得颜色itscolor体重GetWeight年龄GetAgeendl;void mainDog dog1;(4)设计人员基类Person。其成员包括:数据成员:姓名(字符数组)、性别(字符数组)与年龄(整型)成员函数:SetPerson,设置人员数据函数;DisplayPerson,显示人员数据函数;设计派生类1:Teacher,派生于Person。新增成员包括:数据成员:职称(字符数组)、教研室(字符数组)与所

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

6、示数据成员函数;主函数:输入并输出一个教师、一个本科生、一个博士后数据。#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;char roomn;char subjectn;public :Teacher;void setteach

7、er;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 setpostdoctor;void displaypostdoctor;/结构函数Person:Personsetperson;Teacher:Te

8、achersetteacher;Student:Studentsetstudent;Postdoctor:Postdoctor/设置数据/void Person:setpersoncout*name;cout*sex;cout*age;void Teacher:setteachercout*job;cout*room;cout*subject;void Student:setstudentcout*major;cout*banji;cout*leibie;/数据显示/void Person:displaypersoncout姓名:name性别:sex年龄:age;void Teacher:di

9、splayteacherdisplayperson;cout职称:job教研室:room所授课程:subjectendl;void Student:displaystudentdisplayperson;cout专业:major班级:banji类别:leibieendl;void Postdoctor:displaypostdoctordisplayperson;cout职称:job教研室:room所授课程:subject专业:major班级:banji类别:博士后endl;/void maincout您正在输入一个老师得信息: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;

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服