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

设计一个基类:学生类(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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值