赞
踩
分别定义教师类Teacher和干部类Cadre,采用多重继承的方式由这两个类派生出新类Teacher_Cadre(教师兼干部类)。要求:
(1)在两个基类中都包含姓名、年龄、性别、地址、电话数据成员。
(2)在Teacher类中还包含数据成员职称title,在Cadre类中还包含数据成员职务post,在Teacher_Cadre类中还包含数据成员工资wage。
(3)对两个基类中的姓名、年龄、性别、地址、电话数据成员用相同的名字,在访问这类数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、地址、电话,然后再用cout语句输出职务和工资。
//个人认为三文件版本更清晰,且符合工程标准,故只放三文件版
头文件
- #include<iostream>
- #include<string>
- using namespace std;
-
- class Teacher//基类
- {
- private:
- string name;
- int age;
- char sex;
- string address;
- string tele;
- string title;
- public:
- Teacher(string a,int b,char c,string d,string e,string f);//构造函数
- void Display();//输出
- };
-
- class Cadre:virtual public Teacher//虚基类//继承
- {
- public:
- string post;
- Cadre(string a,int b,char c,string d,string e,string f,string g);//构造函数
- };
-
-
- class TeacherCadre:public Cadre
- {
- public:
- int wage;
- void show();
- TeacherCadre(string a,int b,char c,string d,string e,string f,string g,int h);//构造函数
- };
f(x)的实现
- #include<iostream>
- #include<string>
- #include"白兰地兑豆瓣酱.h"
- using namespace std;
-
- Teacher::Teacher(string a,int b,char c,string d,string e,string f)//构造函数
- {
- name=a;
- age=b;
- sex=c;
- address=d;
- tele=e;
- title=f;
- }
-
- Cadre::Cadre(string a,int b,char c,string d,string e,string f,string g):Teacher(a,b,c,d,e,f)//将Teacher类中初始的数据代入
- {
- post=g;
- }
-
- TeacherCadre::TeacherCadre(string a,int b,char c,string d,string e,string f,string g,int h):Teacher(a,b,c,d,e,f),Cadre(a,b,c,d,e,f,g)//将Teacher和Cadre类中初始的数据代入
- {
- wage=h;
- }
-
- void Teacher::Display()//输出函数
- {
- cout<<"姓名:"<<name<<endl;
- cout<<"年龄:"<<age<<endl;
- cout<<"性别:"<<sex<<endl;
- cout<<"地址:"<<address<<endl;
- cout<<"电话:"<<tele<<endl;
- cout<<"职称:"<<title<<endl;
- }
-
- void TeacherCadre::show()
- {
- Teacher::Display();//调用Display
- cout<<"职务:"<<post<<endl;//因为post和wage未在基类Teacher中定义,所以这一步写
- cout<<"工资:"<<wage<<endl;
- }
主函数
- #include<iostream>
- #include<string>
- #include"白兰地兑豆瓣酱.h"
- using namespace std;
-
- int main()
- {
- TeacherCadre a1("zhu",19,'M',"151000000","湖北省武汉市","校长","老师",12000);//基本信息输入
- a1.show();//调用输出函数
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。