设计一个学生类Student,该类包含保护性数据成员: 学号(NUM),姓名(Name),性别(Sex);定义公用成员函数:ReadData() 用于输入Num, Name和Sex, OutputData() 用于输出Num, Name和Sex。
在学生类的基础上采用公用继承派生出研究生类Graduate, 在Graudate类中增加私有数据成员研究方向(R_Interest),增加成员函数Set_Research()设置研究方向,增加成员函数ResearchWork()用于输出学号和研究方向。
注意:数据成员的类型可以根据需要设定,成员函数的参数可以根据需要设定。
程序代码:
#include<iostream>
#include<string.h>
using namespace std;
class Student
{
public:
void ReadData();
void OutputData();
protected:
int Num;
string Name;
char Sex;
};
void Student::ReadData()
{
cin>>Num>>Name>>Sex;
}
void Student::OutputData()
{
cout<<"学号:"<<Num<<endl;
cout<<"姓名:"<<Name<<endl;
cout<<"性别:"<<Sex<<endl;
}
class Graduate:public Student
{
public:
void Set_Research();
void ResearchWork();
private:
string R_Interest;
};
void Graduate::Set_Research()
{
cin>>R_Interest;
}
void Graduate::ResearchWork()
{
cout<<"研究方向:"<<R_Interest;
}
int main()
{
Graduate t1;
cout<<"输入一个学生的学号,姓名,性别,研究方向。"<<endl;
cout<<"例如:1710252387 Tom B math (男用B表示,女用G表示)"<<endl;
t1.ReadData();
t1.OutputData();
t1.Set_Research();
t1.ResearchWork();
}