C++习题第五章9

时间:2022-10-25 00:15:57

理解多继承的派生类的编程和应用:
分别定义Teacher类和Cadre类,采用多重继承方式由这两个类派生出新类Teacher_Cadre类。要求:(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。
(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些成员时,指定作用域。(4)在类体中声明成员函数,在类外定义成员函数。



#include<iostream>

#include<string>
using namespace std;
class Teacher
{
public:
Teacher(string nam,int a,char s,string add,int num, string t)
{
name=nam;
age=a;
sex=s;
address=add;
number=num;
tittle=t;
}
void display()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"address:"<<address<<endl;
cout<<"number:"<<number<<endl;
cout<<"tittle:"<<tittle<<endl;
}
protected:
string name;
int age;
char sex;
string address;
int number;
string tittle;
};


class Cadre
{
public:
Cadre(string nam,int a,char s,string add,int num ,string p)
{
name=nam;
age=a;
sex=s;
address=add;
number=num;
post=p;
}
void display()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"address:"<<address<<endl;
cout<<"number:"<<number<<endl;
cout<<"post:"<<post<<endl;
}
protected:
string name;
int age;
char sex;
string address;
int number;
string post;
};
class Teacher_Cadre:public Teacher,public Cadre
{
public:
Teacher_Cadre(string nam,int a,char s,string add,int num, string t,string p,float w):
Teacher(nam,a,s,add,num, t),Cadre(nam,a,s,add,num,p)
,wages(w) {}
   void show()
   {
  Teacher::display();
  cout<<"post:"<<post<<endl<<"wages:"<<wages<<endl;
   }
private:
float wages;
};
int main()
{
Teacher T1("li",34,'f',"henan",1313,"jiangshi");
Cadre C1("li",34,'f',"henan",1313,"ganbu");
T1.display();
C1.display();
Teacher_Cadre p("li",34,'f',"henan",1313,"jiangshi","ganbu",44444);
p.show();
     return 0;
}