c++实验8-继承与派生上机练习题教程文件.docVIP

  • 19
  • 0
  • 约4.26千字
  • 约 8页
  • 2020-06-20 发布于浙江
  • 举报

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

c++实验8-继承与派生上机练习题 精品文档 精品文档 收集于网络,如有侵权请联系管理员删除 收集于网络,如有侵权请联系管理员删除 精品文档 收集于网络,如有侵权请联系管理员删除 定义一个哺乳动物类Mammal,并从中派生出一个狗类Dog,下面给出Mammal类的定义,要求: 添加Dog类的颜色数据成员,访问属性为私有,通过SetColor和GetColor成员函数来对颜色进行设置和获取。 分别为基类和派生类添加相应的构造函数(有参、无参)和析构函数,并进行测试。 class Mammal { protected: int itsAge; int itsWeight; public: int GetAge(){return itsAge;} void SetAge(int age) {itsAge=age;} int GetWeight() { return itsWeight;} void SetWeight(int weight) {itsWeight= weight;} }; class Dog : public Mammal { //定义Dog类的数据成员和成员函数 }; 改: #include iostream.h #include string using namespace std; class Mammal { protected: int itsAge; int itsWeight; public: Mammal(); ~Mammal(); int GetAge(){return itsAge;} void SetAge(int age) {itsAge=age;} int GetWeight() { return itsWeight;} void SetWeight(int weight) {itsWeight= weight;} }; class Dog : public Mammal { protected: char itscolor[20]; public: Dog(); void Setcolor(char *color) {strcpy(itscolor,color);} void getcolor(){cout狗的颜色itscolorendl;} //定义Dog类的数据成员和成员函数 }; //////////////////////// Mammal::Mammal() { int age1,weight1; cout请输入动物的年龄:endl; cinage1; SetAge(age1); cout请输入动物的体重:endl; cinweight1; SetWeight(weight1); } Mammal::~Mammal() { coutDestructor called.endl; } Dog::Dog() {char color[20]; cout请输入狗的颜色:endl; cincolor;Setcolor(color); cout狗的颜色itscolor体重GetWeight()年龄GetAge()endl; } void main() { Dog dog1; } (4)设计人员基类Person。其成员包括: 数据成员:姓名(字符数组)、性别(字符数组)和年龄(整型) 成员函数:SetPerson,设置人员数据函数; DisplayPerson,显示人员数据函数; 设计派生类1:Teacher,派生于Person。新增成员包括: 数据成员:职称(字符数组)、教研室(字符数组)和所授课程(字符数组) 成员函数:SetTeacher,设置数据成员函数; DisplayTeacher,显示数据成员函数; 设计派生类2:Student,派生于Person。新增成员包括: 数据成员:专业(字符数组)、班级(字符数组)和类别(int) 其中类别取值:1(本科生)、2(硕士生)、3(博士生) 成员函数:SetStudent,设置数据成员函数; DisplayStudent,显示数据成员函数; 设计派生类3:PostDoctor(博士后),多重继承于Student与Teacher。新增成员包括: 数据成员:无 成员函数:SetPostDoctor,设置数据成员函数; DisplayPostDoctor,显示数据成员函数; 主函数: 输入并输出一个教师、一个本科生、一个博士后数据。 #include iostream.h #include string using namespace std; #define n 20 ////////////类的定义 class Person { protected: char name[n]

文档评论(0)

1亿VIP精品文档

相关文档