【Educoder作业】C++ 面向对象 - 类的继承与派生
关于继承,更多的是关注它的意义。在代码量很少的时候用处不大,大不了再写一个。
但是当完成一些工作量比较大的工程时,继承就会避免我们重复的写很多东西。
T1 公有继承 —— 学生信息类
关注一下如何继承即可。
#include "people.h" // People 类定义在这里面
#include <string>
#include <iostream>
using namespace std;
/********** Begin **********/
//公有继承 People
class Student : public People
{
public:
void PrintSID();
int Sid;
};
/********** 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 *********/
}
T2 保护继承 —— 学生信息类
这些受保护的,就要通过成员函数来访问了。
#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 NameSet(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->NameSet(name);
ptr->SID = sid;
/********* End *********/
}
T3 研究生信息类
#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 SidSet(int sid) {
SID = sid;}
//添加一个 Set 函数来调用父类的 SetName 函数
void SetName_Graduate(string name) {
SetName(name);}
};
/********* End *********/
void Graduate::PrintResearchID()
{
/********* Begin *********/
//输出研究方向 ResearchID
cout << "研究方向:" << this -> ResearchID << endl ;
/********* End *********/
}
void Set(string name,int sid,int rid,Graduate *ptr)
{
/********* Begin *********/
//设置 ptr 所指对象的三个成员
ptr->SetName_Graduate(name);
ptr->SidSet(sid);
ptr->ResearchID = rid;
/********* End *********/
}
T4 狼人类
#include <string>
#include <iostream>
using namespace std;
/********* Begin *********/
class Wolf
{
//狼类成员的声明
public :
string Name;
int Shape;
void PrintStat();
};
//狼类成员的定义
void Wolf :: PrintStat() {
cout << "姓名:" << Name << ",爪子锋利度为:" << Shape << endl ;
}
class Human
{
//人类成员的声明
public :
string Name;
int Intell;
void PrintStat();
};
//人类成员的定义
void Human :: PrintStat() {
cout << "姓名:" << Name << ",智力为:" << Intell << endl ;
}
// 记得在这里写上要继承的类
class Werewolf : public Wolf, public Human
{
//狼人类成员的声明
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) {
Shape = shape, Intell = intell;
}
void Werewolf :: PrintAllState() {
Wolf :: PrintStat();
Human :: PrintStat();
}
/********* End *********/