当前位置:   article > 正文

设计一个基类:学生类(Student),采用公有继承的方式派生出一个研究生类_设计新类student1为类student的public派生类

设计新类student1为类student的public派生类

设计一个基类:学生类(Student),采用公有继承的方式派生出一个研究生类(PostGraduate),要求:

(1)Student类中包含:学号、姓名、性别、专业。
(2)要求在PostGraduate类中增加导师(tutor)、津贴(allowance)、研究方向(researchArea)。
(3)两个类中都包含:display()函数,用于输出本类中的成员信息。

实验思路:首先写出基类Student,在派生类PostGraduate中继承Student类时用公有继承,在构造时也要调用Student的构造函数,在打印信息时,可在基类中设置一个打印信息的函数,在派生类打印信息时,也直接调用这个函数打印Student的信息。

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Student{
  5. public:
  6. Student(){}
  7. Student(int aID,string aname,string asex,string aspe):
  8. ID(aID),name(aname),sex(asex),spe(aspe){}
  9. Student(Student &p): ID(p.ID),name(p.name),sex(p.sex),spe(p.spe){}
  10. ~Student(){}
  11. void display()const;
  12. private:
  13. int ID;
  14. string name,sex,spe;
  15. };
  16. void Student::display()const{
  17. cout << ID << " " << name << " "
  18. << sex << " " << spe << " ";
  19. }
  20. class PostGraduate:public Student{
  21. public:
  22. PostGraduate(){}
  23. PostGraduate(int ID,string name,string sex,string spe,
  24. string tutor,double allowance,string researchArea);
  25. void display()const;
  26. ~PostGraduate(){}
  27. private:
  28. string tutor;
  29. double allowance;
  30. string researchArea;
  31. };
  32. PostGraduate::PostGraduate(int aID,string aname,string asex,string aspe,
  33. string atutor,double aallowance,string aresearchArea):
  34. Student(aID,aname,asex,aspe),
  35. tutor(atutor),allowance(aallowance),researchArea(aresearchArea){}
  36. void PostGraduate::display()const{
  37. Student::display();
  38. cout << tutor << " "<< " " << allowance << " " << researchArea << endl;
  39. }
  40. int main() {
  41. Student s1(20190001,"Michael","Male","Computer Science");
  42. //参数分别为:学号,姓名,性别,专业
  43. s1.display();
  44. cout << endl;
  45. PostGraduate p1(20190001,"Michael","Male","Computer Science","Liu",1000,"Deep learing");//导师:“Liu”,津贴:“1000”,研究方向:“Deep learning”
  46. p1.display();
  47. return 0;
  48. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号