问题描述:
分别定义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 <cstring>
using namespace std;
class Teacher{
protected:
string name;
string sex;
string address;
string phone;
string title;
int age;
public:
Teacher(string n,string s,string a,string p,string t,int ag){
name=n;sex=s;address=a;
phone=p;title=t,age=ag;
}
void output();
};
void Teacher::output(){
cout<<"name: "<<name<<'\12';
cout<<"sex: "<<sex<<'\12';
cout<<"address: "<<address<<'\12';
cout<<"phone: "<<phone<<'\12';
cout<<"title: "<<title<<'\12';
cout<<"age: "<<age<<'\12';
}
class Cadre{
protected:
string name;
string sex;
string address;
string phone;
string past;
int age;
public:
Cadre(string n,string s,string a,string p,string pa,int ag)
:name(n),sex(s),address(a),phone(p),past(pa),age(ag){}
void output();
};
void Cadre::output(){
cout<<"name: "<<name<<'\12';
cout<<"sex: "<<sex<<'\12';
cout<<"address: "<<address<<'\12';
cout<<"phone: "<<phone<<'\12';
cout<<"past: "<<past<<'\12';
cout<<"age: "<<age<<'\12';
}
class Teacher_Cadre:public Teacher,public Cadre{
private:
double wages;
public:
Teacher_Cadre(string n,string s,string a,string p,string t,string pa,int ag,double w)
:Teacher(n,s,a,p,t,ag),Cadre(n,s,a,p,pa,ag),wages(w){}
void show();
};
void Teacher_Cadre::show(){
cout<<"name: "<<Teacher::name<<'\12';
cout<<"sex: "<<Teacher::sex<<'\12';
cout<<"address: "<<Teacher::address<<'\12';
cout<<"phone: "<<Teacher::phone<<'\12';
cout<<"title: "<<Teacher::title<<'\12';
cout<<"past: "<<Cadre::past<<'\12';
cout<<"age: "<<Teacher::age<<'\12';
cout<<"wages: "<<wages<<'\12';
}
int main(){
Teacher_Cadre my("小范","男","烟台大学南校区","178628*****","讲师","助教",22,8888);
my.show();
cout<<'\12';
my.Teacher::output();
cout<<'\12';
my.Cadre::output();
return 0;
}
运行结果: