您的位置:首页 > 编程语言 > C语言/C++

2012年 上半年 第十二周 C++程序设计 (三十五)

2012-05-13 14:05 363 查看
【任务】(教材P394习题9)分别定义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 Teacther
{
public:
Teacther(string t,string nam,int n, char m,string p,string s):title(t),name(nam),age(n),sex(m),place(p),phone(s){}
void display();

protected:
string title;
string name;
int age;
char sex;
string place;
string phone;
};
void Teacther::display()
{

cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"住址:"<<place<<endl;
cout<<"电话:"<<phone<<endl;
cout<<"职称:"<<title<<endl;
}
class Cadre
{
public:
Cadre(string a,string b,int c,char d,string e,int f,string h):title(a),name(b),age(c),sex(d),place(e),phone(f),post(h){}

protected:
string title;
string name;
int age;
char sex;
string place;
int phone;
string post;
};
class Teacther_Cadre:public Teacther,public Cadre
{
public:
Teacther_Cadre(string t,string nam,int n, char m,string p,string s,string a,string b,int c,char d,string e,int f,string h,int q):Teacther(t,nam,n,m,p,s),Cadre(a,b,c,d,e,f,h){wages=q;}
void show();

int wages;
};
void Teacther_Cadre::show()
{
display();
cout<<"职务:"<<post<<endl;
cout<<"工资:"<<wages<<endl;
}
void main()
{
Teacther_Cadre t( "litaiping", 20,'f'," jinan",18253591885,"teacher",10000);
t.show();
system("pause");
}







运行结果

姓名:litaiping
年龄:20
性别:f
住址:jinan
职称:student
职务:teacher
工资:10000
请按任意键继续. . .




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息