资源描述
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 <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<<"狗得颜色"<<itscolor<<endl;}
//定义Dog类得数据成员与成员函数
};
////////////////////////
Mammal::Mammal
{
int age1,weight1;
cout<<"请输入动物得年龄:"<<endl;
cin>>age1;
SetAge(age1);
cout<<"请输入动物得体重:"<<endl;
cin>>weight1;
SetWeight(weight1);
}
Mammal::~Mammal
{
cout<<"Destructor called、"<<endl;
}
Dog::Dog
{char color[20];
cout<<"请输入狗得颜色:"<<endl;
cin>>color;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];
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<<"姓名:"<<name<<"性别:"<<sex<<"年龄:"<<age;
}
void Teacher::displayteacher
{
displayperson;
cout<<"职称:"<<job<<"教研室:"<<room<<"所授课程:"<<subject<<endl;
}
void Student::displaystudent
{
displayperson;
cout<<"专业:"<<major<<"班级:"<<banji<<"类别:"<<leibie<<endl;
}
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<<"***************************************************************************syy割"<<endl;
cout<<"您正在输入一个博士后得信息:"<<endl;
Postdoctor p1;
cout<<"***************************************************************************syy割"<<endl;
cout<<endl;
t1、displayteacher;
cout<<endl;
s1、displaystudent;
cout<<endl;
p1、displaypostdoctor;
}
展开阅读全文