第十二周任务报告2

* 程序头部注释开始* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生

* All rights reserved.* 文件名称:

* 作 者: 时永杰

* 完成日期:2012 年 4月 22日

* 版 本 号:v1.1

【任务2】(教材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>
 namespace std;

class Teacher
{
public:
	Teacher(string n,char s,int a,string t,string adr,string tit):name(n),sex(s),age(a),tel(t),adress(adr),title(tit){}
	void show();
protected:
	string name;
	char sex;
	int age;
	string tel;
	string adress;
	string title;
};
class Carder
{
public:
	Carder(string n,char s,int a,string t,string adr,string p):name(n),sex(s),age(a),tel(t),adress(adr),post(p){}
	void show();
protected:
	string name;
	char sex;
	int age;
	string tel;
	string adress;
	string post;

};
class Teacher_Carder:public Teacher,public Carder
{
public:
	Teacher_Carder(string n,char s,int a,string t,string ade,string tit,string p,int w):Teacher(n,s,a,t,ade,tit),Carder(n,s,a,t,ade,p)
	{
		wages=w;
	};
	void show();
private:
	int wages;
};
void Teacher::show()
{
	cout<<"name:"<<name<<" age:"<<age<<" sex:"<<sex<<" aderss:"<<adress<<" title:"<<title<<endl;
}
void Carder::show()
{
	cout<<"name:"<<name<<" age:"<<age<<" sex:"<<sex<<" aderss:"<<adress<<" post:"<<post<<endl;
}
void Teacher_Carder::show()
{
	cout<<"name:"<<Teacher::name<<" age:"<<Teacher::age<<" sex:"<<Teacher::sex<<" aderss:"<<Teacher::adress<<" title:"<<title<<" post:"<<post<<" wages:"<<wages<<endl;
}
int main()
{
	Teacher_Carder stu1("shiyongjie",'m',21,"18253591716","china","student","no",0);
	stu1.Teacher::show();
	stu1.Carder::show();
	stu1.show();
	system("pause");
	return 0;
}

name:shiyongjie age:21 sex:m aderss:china title:student
name:shiyongjie age:21 sex:m aderss:china post:no
name:shiyongjie age:21 sex:m aderss:china title:student post:no wages:0
请按任意键继续. . .

你可能感兴趣的:(第十二周任务报告2)