自考新教材-p185

源程序:

#include<iostream>
#include<string>
using namespace std;

class CStudent //基类
{
private:
string name; //姓名
string id; //学号
char gender; //性别,‘F’代表女生,‘M’代表男生
int age;
public:
void PrintInfo();
void SetInfo(const string &, const string &, int, char);
void SetName(string);
string GetName();
void SetId(string);
string GetId();
};

void CStudent::PrintInfo()
{
cout << "姓名:\t" << name << endl;
cout << "学号:\t" << id << endl;
cout << "年龄:\t" << age << endl;
cout << "性别:\t" << gender << endl;
}

void CStudent::SetInfo(const string & name_, const string & id_, int age_, char gender_)
{
name = name_;
id = id_;
age = age_;
gender = gender_;
}

void CStudent::SetName(string name)
{
this->name = name;
}

string CStudent::GetName()
{
return name;
}

void CStudent::SetId(string id)
{
this->id = id;
}

string CStudent::GetId()
{
return id;
}

class CUndergraduateStudent :public CStudent //本科生类,继承于类CStudent
{
private:
string department; //学生所属的名
public:
void PrintInfo();
void SetInfo(const string &, const string &, int, char, const string &);
};

void CUndergraduateStudent::PrintInfo()
{
CStudent::PrintInfo(); //调用基类的公有PrintInfo函数
cout << "院系:\t" << department << endl << endl; //访问的是本派生类的成员变量

}

void CUndergraduateStudent::SetInfo(const string & name_, const string & id_,
int age_, char gender_, const string & department_)
{
CStudent::SetInfo(name_, id_, age_, gender_); //调用基类的公有SetInfo函数
department = department_; //访问的是本派生类中的成员变量
}

class CGraduatedStudent :public CStudent //研究生类,继承于类CStudent
{
private:
string department; //学生所属的系名
string advisor; //导师
public:
void PrintInfo();
void SetInfo(const string & name_, const string & id_, int age_, char gender_,
const string & department_, const string &advisor_);
};

void CGraduatedStudent::PrintInfo()
{
CStudent::PrintInfo(); //调用基类的公有PrintInfo函数
cout << "院系:\t" << department << endl; //访问本派生类中的成员变量
cout << "导师:\t" << advisor << endl << endl; //访问本派生类中的成员变量
}

void CGraduatedStudent::SetInfo(const string & name_, const string & id_, int age_,
char gender_, const string & department_, const string & advisor_)
{
CStudent::SetInfo(name_, id_, age_, gender_);
department = department_;
advisor = advisor_;
}

int main()
{
CStudent s1;
CUndergraduateStudent s2;
CGraduatedStudent s3;
s2.SetInfo("小张", "2018-6-2-4", 19, 'M', "数学系");
s3.SetInfo("小李", "M2017-9", 23, 'F', "计算机学院", "孟教授");
s2.PrintInfo();
s3.PrintInfo();
//cout<<s2.name<<endl;
//s2.name="张一";
cout << s2.GetName() << endl;
s2.SetName("张一");
s2.PrintInfo();
cout << s2.GetName() << "," << s2.GetId() << endl;
system("pause");
return 1;
}

运行结果:

 

posted @ 2020-02-04 16:01  bobo哥  阅读(169)  评论(0编辑  收藏  举报