/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:李晓凯
* 完成日期:2015年 5 月 24 日
* 版 本 号:v1.0
*
* 问题描述:【项目1 - 教师兼干部类】
分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:
(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。
(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
* 输入描述:
* 程序输出:
*/
代码:
#include <iostream>
using namespace std;
class Teacher
{
public:
Teacher(string na,int a,char s,string add,string te,string ti):
name(na),age(a),sex(s),address(add),tell(te),title(ti) {}
void display()
{
cout<<"name: "<<name<<endl;
cout<<"age: "<<age<<endl;
cout<<"sex: "<<sex<<endl;
cout<<"title: "<<title<<endl;
cout<<"tell: "<<tell<<endl;
cout<<"address: "<<address<<endl;
}
protected:
string name;
int age;
char sex;
string address;
string tell;
string title;
};
class Catre
{
public:
Catre(string na,int a,char s,string add,string te,string po):
name(na),age(a),sex(s),address(add),tell(te),post(po) {}
void display()
{
cout<<"name: "<<name<<endl;
cout<<"age: "<<age<<endl;
cout<<"sex: "<<sex<<endl;
cout<<"post: "<<post<<endl;
cout<<"tell: "<<tell<<endl;
cout<<"address: "<<address<<endl;
}
protected:
string name;
int age;
char sex;
string address;
string tell;
string post;
};
class Teacher_Catre:public Teacher,public Catre
{
public:
Teacher_Catre(string na,int a,char s,string add,string te,string ti,string po,double w):
Teacher(na,a,s,add,te,ti),Catre(na,a,s,add,te,po),wages(a) {}
void show();
private:
double wages;
};
void Teacher_Catre::show()
{
Catre::display();
cout<<"title: "<<Teacher::title<<endl;
cout<<"wages: "<<wages<<endl;
}
//void Teacher_Catre::show() //该函数同样可以
//{
// Teacher::display();
// cout<<"post: "<<Catre::post<<endl;
// cout<<"wages: "<<wages<<endl;
//}
int main()
{
Teacher_Catre tc("Wang-li",50,'f',"135 Beijing Road,Shanghai","(021)61234567","president","prof.",1534.5);
tc.show();
return 0;
}