c++实验8-继承与派生上机练习题(共6页)_第1页
c++实验8-继承与派生上机练习题(共6页)_第2页
c++实验8-继承与派生上机练习题(共6页)_第3页
c++实验8-继承与派生上机练习题(共6页)_第4页
c++实验8-继承与派生上机练习题(共6页)_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上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() return

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

3、ge;int GetWeight() return 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<<"狗的颜色"<<itscolor<<endl;/定义Dog类的数据成员和成员函数;/Mammal:Mamm

4、al()int age1,weight1;cout<<"请输入动物的年龄:"<<endl;cin>>age1;SetAge(age1);cout<<"请输入动物的体重:"<<endl;cin>>weight1;SetWeight(weight1);Mammal:Mammal()cout<<"Destructor called."<<endl;Dog:Dog()char color20;cout<<"请输入狗的颜色:&q

5、uot;<<endl;cin>>color;Setcolor(color);cout<<"狗的颜色"<<itscolor<<"体重"<<GetWeight()<<"年龄"<<GetAge()<<endl;void main()Dog dog1;(4)设计人员基类Person。其成员包括:数据成员:姓名(字符数组)、性别(字符数组)和年龄(整型)成员函数:SetPerson,设置人员数据函数;DisplayPerson,显示人员数

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

7、。新增成员包括:数据成员:无成员函数:SetPostDoctor,设置数据成员函数;DisplayPostDoctor,显示数据成员函数;主函数:输入并输出一个教师、一个本科生、一个博士后数据。#include <iostream.h>#include <string>using namespace std;#define n 20/类的定义class Personprotected:char namen;char sexn;int age;public:Person();void setperson();void displayperson();class Teach

8、er :virtual public Personprotected:char jobn;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,pu

9、blic Studentpublic :Postdoctor();void setpostdoctor();void displaypostdoctor();/结构函数Person:Person()setperson();Teacher:Teacher()setteacher();Student:Student()setstudent();Postdoctor:Postdoctor()/设置数据/void Person:setperson()cout<<"*"<<"姓名:"cin>>name;cout<<&

10、quot;*"<<"性别:"cin>>sex;cout<<"*"<<"年龄:"cin>>age;void Teacher:setteacher()cout<<"*"<<"职称:"cin>>job;cout<<"*"<<"教研室:"cin>>room;cout<<"*"<<&

11、quot;所授课程:"cin>>subject;void Student:setstudent()cout<<"*"<<"专业:"cin>>major;cout<<"*"<<"班级:"cin>>banji;cout<<"*"<<"类别(1本科2硕士3博士):"cin>>leibie;/数据显示/void Person:displayperson()

12、cout<<"姓名:"<<name<<"性别:"<<sex<<"年龄:"<<age;void Teacher:displayteacher()displayperson();cout<<"职称:"<<job<<"教研室:"<<room<<"所授课程:"<<subject<<endl;void Student:display

13、student()displayperson();cout<<"专业:"<<major<<"班级:"<<banji<<"类别:"<<leibie<<endl;void Postdoctor:displaypostdoctor()displayperson();cout<<"职称:"<<job<<"教研室:"<<room<<"所授课程:"

14、;<<subject<<"专业:"<<major<<"班级:"<<banji<<"类别:博士后"<<endl;/void main()cout<<"您正在输入一个老师的信息:"<<endl;Teacher t1;cout<<"*syy割"<<endl;cout<<"您正在输入一个学生的信息:"<<endl;Student s1;cout<<"*syy割"<<endl;cout&l

温馨提示

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

评论

0/150

提交评论