当前位置:   article > 正文

C++学习记录7:定义教师类Teacher和干部类Cadre,采用多重继承的方式由这两个类派生出新类Teacher_Cadre(教师兼干部类)_分别定义teacher类和cadre类,多文件程序

分别定义teacher类和cadre类,多文件程序

分别定义教师类Teacher和干部类Cadre,采用多重继承的方式由这两个类派生出新类Teacher_Cadre(教师兼干部类)。要求:
(1)在两个基类中都包含姓名、年龄、性别、地址、电话数据成员。
(2)在Teacher类中还包含数据成员职称title,在Cadre类中还包含数据成员职务post,在Teacher_Cadre类中还包含数据成员工资wage。
(3)对两个基类中的姓名、年龄、性别、地址、电话数据成员用相同的名字,在访问这类数据成员时,指定作用域
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、地址、电话,然后再用cout语句输出职务和工资。

//个人认为三文件版本更清晰,且符合工程标准,故只放三文件版

头文件

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. class Teacher//基类
  5. {
  6. private:
  7. string name;
  8. int age;
  9. char sex;
  10. string address;
  11. string tele;
  12. string title;
  13. public:
  14. Teacher(string a,int b,char c,string d,string e,string f);//构造函数
  15. void Display();//输出
  16. };
  17. class Cadre:virtual public Teacher//虚基类//继承
  18. {
  19. public:
  20. string post;
  21. Cadre(string a,int b,char c,string d,string e,string f,string g);//构造函数
  22. };
  23. class TeacherCadre:public Cadre
  24. {
  25. public:
  26. int wage;
  27. void show();
  28. TeacherCadre(string a,int b,char c,string d,string e,string f,string g,int h);//构造函数
  29. };

f(x)的实现

  1. #include<iostream>
  2. #include<string>
  3. #include"白兰地兑豆瓣酱.h"
  4. using namespace std;
  5. Teacher::Teacher(string a,int b,char c,string d,string e,string f)//构造函数
  6. {
  7. name=a;
  8. age=b;
  9. sex=c;
  10. address=d;
  11. tele=e;
  12. title=f;
  13. }
  14. Cadre::Cadre(string a,int b,char c,string d,string e,string f,string g):Teacher(a,b,c,d,e,f)//将Teacher类中初始的数据代入
  15. {
  16. post=g;
  17. }
  18. 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类中初始的数据代入
  19. {
  20. wage=h;
  21. }
  22. void Teacher::Display()//输出函数
  23. {
  24. cout<<"姓名:"<<name<<endl;
  25. cout<<"年龄:"<<age<<endl;
  26. cout<<"性别:"<<sex<<endl;
  27. cout<<"地址:"<<address<<endl;
  28. cout<<"电话:"<<tele<<endl;
  29. cout<<"职称:"<<title<<endl;
  30. }
  31. void TeacherCadre::show()
  32. {
  33. Teacher::Display();//调用Display
  34. cout<<"职务:"<<post<<endl;//因为post和wage未在基类Teacher中定义,所以这一步写
  35. cout<<"工资:"<<wage<<endl;
  36. }

 

主函数

  1. #include<iostream>
  2. #include<string>
  3. #include"白兰地兑豆瓣酱.h"
  4. using namespace std;
  5. int main()
  6. {
  7. TeacherCadre a1("zhu",19,'M',"151000000","湖北省武汉市","校长","老师",12000);//基本信息输入
  8. a1.show();//调用输出函数
  9. return 0;
  10. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/106057
推荐阅读
相关标签
  

闽ICP备14008679号