




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 itsWeight;vo
2、id 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() return itsWeight;void SetW
3、eight(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请输入动物的体重:weight1;SetWeigh
4、t(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:Teacher,派生于Person。新增成员
5、包括:数据成员:职称(字符数组)、教研室(字符数组)和所授课程(字符数组)成员函数:SetTeacher,设置数据成员函数;DisplayTeacher,显示数据成员函数;设计派生类2:Student,派生于Person。新增成员包括:数据成员:专业(字符数组)、班级(字符数组)和类别(int)其中类别取值:1(本科生)、2(硕士生)、3(博士生)成员函数:SetStudent,设置数据成员函数;DisplayStudent,显示数据成员函数;设计派生类3:PostDoctor(博士后),多重继承于Student与Teacher。新增成员包括:数据成员:无成员函数:SetPostDoctor,
6、设置数据成员函数;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;char roomn;char subj
7、ectn;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 setpostdoctor();void displa
8、ypostdoctor();/结构函数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;cout*banji;cout*leibi
9、e;/数据显示/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*sy
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025至2030中国男士帆布鞋行业市场发展态势及发展趋势与发展趋势分析与未来投资战略咨询研究报告
- 2025至2030中国电弧焊接设备行业发展趋势分析与未来投资战略咨询研究报告
- 2025至2030中国琥珀酸亚铁市场投资建议及未来发展潜力评估报告
- 2022-2027年中国高校软硬件应用市场竞争态势及行业投资潜力预测报告
- 2025年中国代步平衡车行业市场调研分析及投资战略规划报告
- 2025年中国磷酸燃料电池行业市场深度分析及投资战略规划建议报告
- 2020-2025年中国咖啡市场运行态势及行业发展前景预测报告
- 2024年中国石墨泡沫炭行业市场调查报告
- 2025年PTA项目提案报告模板
- 2025年中国大豆粉行业市场深度分析及未来发展趋势预测报告
- 2、2024广西专业技术人员继续教育公需科目参考答案(97分)
- 结节性多动脉炎的早期诊断方法-深度研究
- 《水利工程建设项目法人工作手册2023版》知识培训
- 超星尔雅学习通《形势与政策》2025春章节测试附答案
- 广东省广州市越秀区2025年中考一模历史模拟试题(含答案)
- 有效咳嗽训练操作流程
- 消防安装抗震支架施工方案
- 云南xx城镇老旧小区改造项目可行性研究报告
- 企业内部停车位分配方案
- 湖南省长沙市雅礼实验高中-主题班会-把学习变为热爱:内驱力【课件】
- 2025年中考英语总复习:补全对话 练习题汇编(含答案解析)
评论
0/150
提交评论