【问题描述】
研究生Graduate既有学生Student的属性,又有教师Teacher的属性,通过多重继承说明一个研究生类Graduate。
从前面实验题目完成的Person类派生出Teacher类,新增专业(dept)和月薪(salary)属性,并定义Teacher类的构造函数初始化新增的数据成员,showMe成员函数显示新增的数据成员的值。
再从Person类派生出Student类,新增班级(class)和学号(ID)属性,定义Student类的构造函数初始化新增的数据成员,showMe成员函数显示新增的数据成员的值。
通过“多重继承”说明一个研究生类Graduate,定义Graduate类的构造函数,定义Graduate类的showMe成员函数显示各个数据成员的值。
主函数设计如下,请勿修改:
int main(){undefined
Graduate stu1(“Lisi”,22,‘m’,“College of Informatics”,2000,“2015013”,“S101”);
stu1.showMe();
return 0;
}
提示:
(1)Graduate的父类是Student和Teacher,而Student和Teacher有共同的父类Person,如果是普通继承则姓名、性别和年龄就发生了重复。因此,这一题需要用到虚基类的概念。
(2)注意showMe()成员函数在每个类里都有。
【样例输出】
class: S101
id: 2015013
name: Lisi
sex: m
age: 22
dept: College of Informatics
salary: 2000
代码如下
#include<iostream>
#include<string.h>
using namespace std;
class Person {
char name[10];
int age;
char sex;
public:
void Register(char *s, int n, char c) {
strcpy(name,s);
age = n; sex = (c == 'm' ? 0 : 1);
}
char *getName() { return name; }
char getSex() { return sex == 0 ? 'm' : 'f'; }
int getAge() { return age; }
void showMe();
};
class Teacher:virtual public Person
{
public:
Teacher(char* name,int age,char gender,
char* profession,int salary);
void showMe();
char* getdept(){ return dept; }
int getsalary(){ return salary; }
private:
char dept[30];
int salary;
};
class Student:virtual public Person
{
public:
Student(char* name,int age,char gender,
char* stu_id, char* stu_class);
void showMe();
char* getClass(){ return myclass; }
char* getID(){ return ID; }
private:
char myclass[10];
char ID[10];
};
class Graduate:public Teacher,public Student
{
public:
Graduate(char* name,int age,char gender,
char* profession,int salary,
char* stu_class,char* stu_id);
void showMe();
};
Teacher::Teacher(char* name,int age,char gender, char* profession,int salary)
{
Register(name,age,gender);
strcpy(dept,profession);
this->salary = salary;
}
Student::Student(char* name,int age,char gender,char* stu_id, char* stu_class)
{
Register(name,age,gender);
strcpy(myclass,stu_class);
strcpy(ID, stu_id);
}
Graduate::Graduate(char* name,int age,char gender,
char* profession,int salary,
char* stu_id,char* stu_class):Teacher(name,age,gender,profession,salary),Student(name,age,gender,stu_id,stu_class){}
void Person::showMe()
{
cout<<"name: "<<getName()<<endl;
cout<<"sex: "<<getSex()<<endl;
cout<<"age: "<<getAge()<<endl;
}
void Student::showMe()
{
cout<<"class: "<<myclass<<endl;
cout<<"id: "<<ID<<endl;
cout<<"name: "<<getName()<<endl;
cout<<"sex: "<<getSex()<<endl;
cout<<"age: "<<getAge()<<endl;
}
void Teacher::showMe()
{
cout<<"name: "<<getName()<<endl;
cout<<"sex: "<<getSex()<<endl;
cout<<"age: "<<getAge()<<endl;
cout<<"co: "<<dept<<endl;
cout<<"salary: "<<salary<<endl;
}
void Graduate::showMe()
{
cout<<"class: "<<getClass()<<endl;
cout<<"id: "<<getID()<<endl;
cout<<"name: "<<getName()<<endl;
cout<<"sex: "<<getSex()<<endl;
cout<<"age: "<<getAge()<<endl;
cout<<"co: "<<getdept()<<endl;
cout<<"salary: "<<getsalary()<<endl;
}
int main()
{// 主函数设计如下,请勿修改:
Graduate stu1(const_cast<char*>("Lisi"),22,'m',const_cast<char*>("College of Informatics"),2000,const_cast<char*>("2015013"),const_cast<char*>("S101"));
stu1.showMe();
return 0;
}