20240904继承

#include <iostream>
using namespace std;

class Human
{
private:
    string name;
    int age;
public:
    Human(string n, int a)//有参构造函数
    {
        name = n;
        age = a;
    }
    Human()//无参构造函数
    {
        name = "unknown";
        age = 0;
    }
    ~Human()//析构函数
    {
        cout << "析构函数" << endl;
    }
    void show()//show函数
    {
        cout << "姓名:" << name << endl;
        cout << "年龄:" << age << endl;
    }
};

class Student : public Human//学生类继承自人类
{
private:
    int score;
public:
    Student(string n, int a, int s) : Human(n, a)//派生类有参构造函数
    {
        score = s;
    }
    Student() : Human()//派生类无参构造函数
    {
        score = 0;
    }
    ~Student()//派生类析构函数
    {
        cout << "学生析构函数" << endl;
    }
    void show()//派生类show函数
    {
        cout << "分数:" << score << endl;
        Human::show();
    }
};

class Pary : public Human//党员类继承自人类
{
private:
    string activity;
    string organization;
public:
    Pary(string n, int a, string act, string org) : Human(n, a)//派生类有参构造函数
    {
        activity = act;
        organization = org;
    }
    Pary() : Human()//派生类无参构造函数
    {
        activity = "unknown";
        organization = "unknown";
    }
    ~Pary()//派生类析构函数
    {
        cout << "党员析构函数" << endl;
    }
    void show()//派生类show函数
    {
        cout << "党组织活动:" << activity << endl;
        cout << "组织:" << organization << endl;
        Human::show();
    }
};

class StudentLeader : public Student, public Pary//学生干部类继承自学生类和党员类
{
private:
    string position;
public:
    StudentLeader(string n, int a, int s, string act, string org, string pos) : Student(n, a, s), Pary(n, a, act, org)//派生类有参构造函数
    {
        position = pos;
    }
    StudentLeader() : Student(), Pary()//派生类无参构造函数
    {
        position = "unknown";
    }
    ~StudentLeader()//派生类析构函数
    {
        cout << "学生干部析构函数" << endl;
    }
    void show()//派生类show函数
    {
        cout << "职位:" << position << endl;
        Student::show();
        Pary::show();
    }
};

int main()
{
    StudentLeader sl("张三", 20, 90, "学习", "学生会", "干事");

    cout << "学生干部信息:" << endl;

    sl.show();

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值