设计一个基类:学生类(Student),采用公有继承的方式派生出一个研究生类(PostGraduate),要求:
(1)Student类中包含:学号、姓名、性别、专业。
(2)要求在PostGraduate类中增加导师(tutor)、津贴(allowance)、研究方向(researchArea)。
(3)两个类中都包含:display()函数,用于输出本类中的成员信息。
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
int xuehao;
string name;
string sex;
string zhuanye;
void display()
{
cout << "学号:"<< xuehao <<",姓名:"<<name<<",性别:"<<sex<<",专业:"<<zhuanye<<endl;
}
};
class PostGraduate:public Student
{
public:
string tutor;
float allowance;
string researchArea;
void display()
{
cout << "学号:"<< xuehao <<",姓名:"<<name<<",性别:"<<sex<<",专业:"<<zhuanye;
cout <<",导师:"<<tutor << ",津贴"<< allowance <<",研究方向:"<<researchArea<<endl;
}
};
int main()
{
Student stu;
stu.xuehao = 12345;
stu.name = "张三";
stu.sex = "男";
stu.zhuanye = "电子工程";
stu.display();
PostGraduate pt;
pt.xuehao = 23456;
pt.name = "李四";
pt.sex = "男";
pt.zhuanye = "计算机";
pt.tutor = "李教授";
pt.allowance = 1000;
pt.researchArea = "图论";
pt.display();
return 0;
}
感谢你的回答,如果是用以下代码测试,应该怎么写?
int main(){
Student s1(“20190001”, “Michael”, “Male”, “Computer Science”);
//参数分别为:学号,姓名,性别,专业
s1.display();
PostGraduate p1(……);//导师:“Liu”,津贴:“1000”,研究方向:“Deep learning”
p1.display();
return 0;
}