赞
踩
设计一个基类:学生类(Student),采用公有继承的方式派生出一个研究生类(PostGraduate),要求:
(1)Student类中包含:学号、姓名、性别、专业。
(2)要求在PostGraduate类中增加导师(tutor)、津贴(allowance)、研究方向(researchArea)。
(3)两个类中都包含:display()函数,用于输出本类中的成员信息。
实验思路:首先写出基类Student,在派生类PostGraduate中继承Student类时用公有继承,在构造时也要调用Student的构造函数,在打印信息时,可在基类中设置一个打印信息的函数,在派生类打印信息时,也直接调用这个函数打印Student的信息。
- #include <iostream>
- #include <string>
- using namespace std;
- class Student{
- public:
- Student(){}
- Student(int aID,string aname,string asex,string aspe):
- ID(aID),name(aname),sex(asex),spe(aspe){}
- Student(Student &p): ID(p.ID),name(p.name),sex(p.sex),spe(p.spe){}
- ~Student(){}
- void display()const;
-
- private:
- int ID;
- string name,sex,spe;
-
-
- };
- void Student::display()const{
- cout << ID << " " << name << " "
- << sex << " " << spe << " ";
-
- }
-
- class PostGraduate:public Student{
- public:
- PostGraduate(){}
- PostGraduate(int ID,string name,string sex,string spe,
- string tutor,double allowance,string researchArea);
- void display()const;
- ~PostGraduate(){}
- private:
- string tutor;
- double allowance;
- string researchArea;
- };
- PostGraduate::PostGraduate(int aID,string aname,string asex,string aspe,
- string atutor,double aallowance,string aresearchArea):
- Student(aID,aname,asex,aspe),
- tutor(atutor),allowance(aallowance),researchArea(aresearchArea){}
-
- void PostGraduate::display()const{
- Student::display();
- cout << tutor << " "<< " " << allowance << " " << researchArea << endl;
- }
- int main() {
- Student s1(20190001,"Michael","Male","Computer Science");
- //参数分别为:学号,姓名,性别,专业
- s1.display();
- cout << endl;
- PostGraduate p1(20190001,"Michael","Male","Computer Science","Liu",1000,"Deep learing");//导师:“Liu”,津贴:“1000”,研究方向:“Deep learning”
-
- p1.display();
- return 0;
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。