C++ 面向对象 - 类的继承与派生

第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 *********/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ଲଇଉକ ଲ ̊ଳ

多谢大哥赏赐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值