1、设计一个学生类Student,该类包含 保护性 数据成员: 学号(NUM),姓名(Name),性别(Sex);定义公用成员函数:ReadData() 用于输入Num, Name和Sex, OutputData() 用于输出Num, Name和Sex。
在学生类的基础上采用公用继承派生出研究生类Graduate, 在Graudate类中增加私有数据成员研究方向(R_Interest),增加成员函数Set_Research()设置研究方向,增加成员函数ResearchWork()用于输出学号和研究方向。
#include <iostream>
#include <string>
using namespace std;
class Student{
protected:
int num ;
string name ;
string sex ;
public:
Student(int n=0,string m="xu",string s="nan"):num(n),name(m),sex(s){}
void ReadData(){
cout << "输入学生的信息: "<<endl;
cin>> num >> name >> sex ;
}
void OutputData(){
cout << "该学生的信息是: "<<endl;
cout << "学号: " << num << endl <<"姓名: "<< name << endl <<"性别: "<< sex << endl;
}
void display(){
cout << num ;
}
};
class Graduate : public Student{
string R_Interest ;
public:
Graduate(string r="mu"):R_Interest(r){}
void Set_Research(){
cout << "输入这个研究生的发展方向: " ;
cin >> R_Interest ;
}
void ResearchWork(Student &s){
cout << "输出学生的学号和研究方向: "<< endl;
cout << "学号: ";s.display();cout<<endl;
cout << "研究方向: " << R_Interest << endl;
}
};
int main(){
Student s ;
s.ReadData() ;
s.OutputData() ;
cout << "读到研究生后: " << endl;
Graduate g ;
g.Set_Research() ;cout<<endl;
g.ResearchWork(s) ;
}
其运行结果如下: