77范文网 - 专业文章范例文档资料分享平台

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

来源:网络收集 时间:2020-04-17 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

1. 定义一个哺乳动物类Mammal,并从中派生出一个狗类Dog,下面给出Mammal类的定义,要求:

(1) 添加Dog类的颜色数据成员,访问属性为私有,通过SetColor和GetColor成员函数来对颜色进行设置和获取。

(2) 分别为基类和派生类添加相应的构造函数(有参、无参)和析构函数,并进行测试。

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 #include 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<<\狗的颜色\ //定义Dog类的数据成员和成员函数 };

//////////////////////// Mammal::Mammal() {

int age1,weight1;

cout<<\请输入动物的年龄:\cin>>age1; SetAge(age1);

cout<<\请输入动物的体重:\cin>>weight1;

SetWeight(weight1); }

Mammal::~Mammal() {

cout<<\}

Dog::Dog()

{char color[20];

cout<<\请输入狗的颜色:\cin>>color;Setcolor(color);

cout<<\狗的颜色\体重\年龄\ }

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 #include using namespace std; #define n 20

////////////类的定义 class Person {

protected: char name[n]; char sex[n]; int age; public: Person(); void setperson(); void displayperson(); };

class Teacher :virtual public Person {

protected: char job[n]; char room[n]; char subject[n]; public :

Teacher(); void setteacher(); void displayteacher(); };

class Student:virtual public Person { protected: char major[n]; char banji[n]; int leibie; public : Student(); void setstudent(); void displaystudent(); };

class Postdoctor:public Teacher,public Student {

public : Postdoctor(); void setpostdoctor(); void displaypostdoctor(); };

/////////////结构函数 Person::Person() { setperson(); }

Teacher::Teacher() { setteacher(); }

Student::Student() { setstudent(); }

Postdoctor::Postdoctor()

{ }

//////////////////设置数据////////////////// void Person::setperson() { cout<<\姓名:\ cin>>name; cout<<\性别:\ cin>>sex; cout<<\年龄:\ cin>>age; }

void Teacher::setteacher() { cout<<\职称:\ cin>>job; cout<<\教研室:\ cin>>room; cout<<\所授课程:\ cin>>subject; }

void Student::setstudent() { cout<<\专业:\ cin>>major; cout<<\班级:\ cin>>banji; cout<<\类别(1本科2硕士3博士):\ cin>>leibie; }

/////////////数据显示/////////// void Person::displayperson() { cout<<\姓名:\性别:\年龄:\}

void Teacher::displayteacher() { displayperson(); cout<<\职称:\教研室:\所授课程:\ }

void Student::displaystudent() { displayperson(); cout<<\专业:\班级:\类别:\ }

void Postdoctor::displaypostdoctor() { displayperson(); cout<<\职称:\教研室:\所授课程:\专业:\班级:\类别:博士后\ }

/////////////////// void main() {

cout<<\您正在输入一个老师的信息:\Teacher t1;

cout<<\***syy割\

cout<<\您正在输入一个学生的信息:\Student s1;

cout<<\***syy割\

cout<<\您正在输入一个博士后的信息:\Postdoctor p1;

cout<<\***syy割\cout<

t1.displayteacher(); cout<

s1.displaystudent(); cout<

p1.displaypostdoctor(); }

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库c++实验8 继承与派生上机练习题在线全文阅读。

c++实验8 继承与派生上机练习题.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/988343.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: