当前位置:   article > 正文

1.定义一个人员类Person,包括姓名、编号、性别等数据成员和用于输入、输出的成员函数,在此基础上派生出学生类Student类(增加成绩)和教师类Teacher(增加教龄),并实现对学生和教师信息的_创建一个person类,其定义如下

创建一个person类,其定义如下
  1. #include <iostream>
  2. #include<string.h>
  3. using namespace std;
  4. class person
  5. { private:
  6. string name,sex;
  7. int number;
  8. public:
  9. void setperson(string n,string s,int nu)
  10. {
  11. name = n;
  12. sex = s;
  13. number = nu;
  14. }
  15. void output();
  16. };
  17. void person::output()
  18. {
  19. cout<<"name: "<<name<<endl;
  20. cout<<"sex: "<<sex<<endl;
  21. cout<<"number: "<<number<<endl;
  22. }
  23. class student:public person
  24. {
  25. private:
  26. int grade;
  27. public:
  28. student(){}
  29. student(string n,string s,int nu,int g)
  30. {
  31. person::setperson(n,s,nu);
  32. grade=g;
  33. }
  34. void output()
  35. {
  36. person::output();
  37. cout<<"grade: "<<grade<<endl;
  38. }
  39. };
  40. class teacher:public person
  41. {
  42. private:
  43. int teachage;
  44. public:
  45. teacher(){}
  46. teacher(string n,string s,int nu,int t)
  47. {
  48. person::setperson(n,s,nu);
  49. teachage=t ;
  50. }
  51. void output()
  52. {
  53. person::output();
  54. cout<<"teachage: "<<teachage<<endl;
  55. }
  56. };
  57. int main()
  58. {
  59. student m("李磊","男",16064,100);
  60. m.output();
  61. teacher n("赵宝宝","男",1141,20);
  62. n.output();
  63. }


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

闽ICP备14008679号