【C++OJ多重继承与虚拟继承】OOP教师与干部(多重继承)
B. OOP教师与干部(多重继承)
题目描述
分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)类。要求:
(1) 在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
(2) 在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务)。在Teacher_Cadre类中还包含数据成员wages(工资)。
(3) 对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
(4) 在类体中声明成员函数,在类外定义成员函数。
(5) 在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
输入
依次输入姓名、年龄、性别、职称、职务、地址、电话、工资
输出
依次输出姓名、年龄、性别、职称、职务、地址、电话、工资
输入样例
Wang-li 50 f prof. president
135 Beijing Road,Shanghai
(021)61234567 1534.5
输出样例
name:Wang-li
age:50
sex:f
title:prof.
address:135 Beijing Road,Shanghai
tel:(021)61234567
post:president
wages:1534.5
参考代码
#include <iostream>
#include <cstring>
using namespace std;
class Teacher // Teacher(教师)类
{
public:
Teacher(string nam, int a, char s, string tit, string ad, string tele); //构造函数,初始化参数列表
void display(); // Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话
~Teacher(); //析构函数
protected: // 两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
string name;
int age;
char sex;
string address;
string telephone;
string title; // 在Teacher类中还包含数据成员title(职称)
};
//在类体中声明成员函数,在类外定义成员函数。
Teacher::Teacher(string nam, int a, char s, string tit, string ad, string tele) : name(nam), age(a), sex(s), title(tit), address(ad), telephone(tele) {}
void Teacher::display()
{
cout << "name:" << name << endl;
cout << "age:" << age << endl;
cout << "sex:" << sex << endl;
cout << "title:" << title << endl;
cout << "address:" << address << endl;
cout << "tel:" << telephone << endl;
}
Teacher::~Teacher() {}
class Cadre // Cadre(干部)类
{
public:
Cadre(string nam, int a, char s, string ad, string p, string tele); //构造函数,初始化参数列表
~Cadre(); //析构函数
protected: // 两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
string name;
int age;
char sex;
string address;
string telephone;
string post; //在Cadre类中还包含数据成员post(职务)
};
Cadre::Cadre(string nam, int a, char s, string ad, string p, string tele) : name(nam), age(a), sex(s), address(ad), post(p), telephone(tele) {}
Cadre::~Cadre() {}
//采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)类
class Teacher_Cadre : public Teacher, public Cadre
{
public: //基类数据成员使用相同的名字,在引用这些数据成员时,指定作用域。
Teacher_Cadre(string nam, int a, char s, string ad, string tit, string p, string tele, float w); //构造函数,初始化参数列表
void show(); // show中调用Teacher类中的display函数,用cout语句输出职务与工资。
~Teacher_Cadre(); //析构函数
private:
float wages; // 在Teacher_Cadre类中还包含数据成员wages(工资)。
};
Teacher_Cadre::Teacher_Cadre(string nam, int a, char s, string ad, string tit, string p, string tele, float w) : Teacher(nam, a, s, ad, tit, tele), Cadre(nam, a, s, ad, p, tele), wages(w) {}
void Teacher_Cadre::show()
{
Teacher::display();
cout << "post:" << Cadre::post << endl;
cout << "wages:" << wages << endl;
}
Teacher_Cadre::~Teacher_Cadre(){};
int main()
{
//依次输入姓名、年龄、性别、职称、职务、地址、电话、工资
string name;
int age;
char sex;
string title;
string post;
string address;
string telephone;
float wages;
cin >> name >> age >> sex >> title >> post;
cin.ignore(); // cin.ignore()将输入流里面的换行符舍弃掉,输入完成以后还可以进行第二次输入
getline(cin, address); //读取一行数据
cin >> telephone >> wages;
Teacher_Cadre t(name, age, sex, title, address, post, telephone, wages);
t.show();
return 0;
}