要求:分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式有这两个基类派生出新的Teacher_Cadre(教师兼干部)类。
(1)在两个基类包括姓名、年龄、性别、地址、电话等数据成员。
(2)在Teacher类还有title(职称)数据,在Cadre类还有post(职务)数据,在Teacher_Cadre类有wages(工资)。
(3)在两个基类姓名、年龄、性别、地址、电话等数据成员使用相同的名字,在应用这些数据成员时,指定作用域。
(4)类内声明成员函数,类外定义该成员函数。
(5)在派生类Teacher_Cadre类的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、地址、电话、职称等,然后再用cout输出职务和工资。
代码如下:
#include<iostream>
#include<string>
using namespace std;
class Teacher //定义一个Teacher基类
{
public:
Teacher(); //声明构造函数
void display(); //声明成员函数
protected: //定义保护类型变量
string name;
int age;
string sex;
string adress;
string telphone;
string title;
};
class Cadre
{
public:
Cadre(); //声明构造函数
protected: //定义保护类型变量
string name;
int age;
string sex;
string adress;
string telphone;
string post;
};
class Teacher_Cadre :public Teacher, public Cadre //定义多重继承子类
{
public:
Teacher_Cadre(); //声明构造函数
void show(); //声明成员函数
private: //定义一个私有变量
int wages;
};
Teacher::Teacher() //定义Teacher类构造函数实现初始化
{
cout << "Teacher请输入姓名、年龄、性别、地址、电话、职称" << endl;
cin >> name >> age >> sex >> adress >> telphone >> title;
}
void Teacher::display() //定义Teacher类成员函数display()
{
cout << "姓 名:" << name << endl;
cout << "年 龄:" << age << endl;
cout << "性 别:" << sex << endl;
cout << "地 址:" << adress << endl;
cout << "电 话:" << telphone << endl;
cout << "职 称:" << title << endl;
}
Cadre::Cadre() //定义Cadre类构造函数实现初始化
{
cout << "Cadre请输入姓名、年龄、性别、地址、电话、职务" << endl;
cin >> name >> age >> sex >> adress >> telphone >> post;
}
Teacher_Cadre::Teacher_Cadre() //定义Teacher_Cadre类构造函数,初始化工资
{
cout << "请输入工资" << endl;
cin >> wages;
}
void Teacher_Cadre::show() //定义Teacher_Cadre类的成员函数show()
{
display(); //调用display()函数
cout << "职 务:" << Cadre::post << endl;
cout << "工 资:" << wages << endl;
}
int main()
{
Teacher_Cadre s;
s.show();
}