第1关:公有继承 —— 学生信息类
#include "people.h" // People 类定义在这里面
#include <string>
#include <iostream>
using namespace std;
/********** Begin **********/
//公有继承 People
class Student:public People
{
public:
int SID;
void PrintSID();
};
/********** End **********/
void Student::PrintSID()
{
/********* Begin *********/
//输出 SID
cout<<"学号:"<<SID<<endl;
/********* End *********/
}
void Set(int sid,string name,Student *ptr)
{
/********* Begin *********/
//给 ptr 对象的两个属性赋值
ptr->SID=sid;
ptr->Name=name;
/********* End *********/
}
第2关:保护继承 —— 学生信息类
#include "people.h" // People 类定义在这里面
#include <string>
#include <iostream>
using namespace std;
/********** Begin **********/
//保护继承 People
class Student:protected People
{
public:
int SID;
void PrintSID();
//添加一个 Set 函数来设置父类的 Name 成员
void Setname(string name)
{
Name=name;
}
};
/********* End *********/
void Student::PrintSID()
{
/********* Begin *********/
//输出学号 SID
cout<<"学号:"<<SID<<endl;
/********* End *********/
}
void Set(int sid,string name,Student *ptr)
{
/********* Begin *********/
//给 ptr 对象的两个属性赋值
ptr->SID=sid;
ptr->Setname(name);
/********* End *********/
}
第3关:研究生信息类
#include "people.h" //People类定义在这里面
#include <string>
#include <iostream>
using namespace std;
/********* Begin *********/
//私有继承 People 类
class Student:private People
{
public:
int SID;
void PrintSID();
//添加一个 Set 函数来设置父类的 Name 成员
void SetName(string name)
{
Name=name;
}
};
/********* End *********/
void Student::PrintSID()
{
/********* Begin *********/
//输出学号 SID
cout<<"学号:"<<SID<<endl;
/********* End *********/
}
/********* Begin *********/
// 公有继承 Student 类
class Graduate:public Student
{
public:
int ResearchID;
void PrintResearchID();
//添加一个 Set 函数来设置父类的 SID 成员
void Setsid(int sid)
{
SID=sid;
}
//添加一个 Set 函数来调用父类的 SetName 函数
void Setname(string name)
{
SetName(name);
}
};
/********* End *********/
void Graduate::PrintResearchID()
{
/********* Begin *********/
//输出研究方向 ResearchID
cout<<"研究方向:"<<ResearchID<<endl;
/********* End *********/
}
void Set(string name,int sid,int rid,Graduate *ptr)
{
/********* Begin *********/
//设置 ptr 所指对象的三个成员
ptr->Setname(name);
ptr->Setsid(sid);
ptr->ResearchID=rid;
/********* End *********/
}
第4关:狼人类
#include <string>
#include <iostream>
using namespace std;
/********* Begin *********/
class Wolf
{
//狼类成员的声明
public:
void PrintState();
string Name;
int Shape;
};
//狼类成员的定义
void Wolf::PrintState()
{
cout<<"姓名:"<<Name<<",爪子锋利度为:"<<Shape<<endl;
}
class Human
{
//人类成员的声明
public:
string Name;
int Intell;
void PrintState();
};
//人类成员的定义
void Human::PrintState()
{
cout<<"姓名:"<<Name<<",智力为:"<<Intell<<endl;
}
// 记得在这里写上要继承的类
class Werewolf : public Human,public Wolf
{
//狼人类成员的声明
public:
void SetName(string name);
void SetState(int shape,int intell);
void PrintAllState();
};
//狼人类成员的定义
void Werewolf::SetName(string name)
{
Wolf::Name=name;
Human::Name=name;
}
void Werewolf::SetState(int shape,int intell)
{
Wolf::Shape=shape;
Human::Intell=intell;
}
void Werewolf::PrintAllState()
{
Wolf::PrintState();
Human::PrintState();
}
/********* End *********/