20230423作业(类的继承)

 作业:

#include <iostream>

using namespace std;

class Student  //学生类
{
private:   //私有权限
    string name;    //姓名
    int age;        //年龄
    double score;   //分数
public:   //公共权限
    Student() {cout<<"Student::无参构造"<<endl;}    //无参构造
    Student(string n,int a,double s):name(n),age(a),score(s)    //有参构造
    {
        cout<<"Student::有参构造"<<endl;
    }
    //析构函数
    ~Student()
    {
        cout<<"Student::析构函数"<<endl;
    }

    //成员函数
    void show()
    {
        cout<<"Student::name = "<<name<<endl;
        cout<<"Student::age = "<<age<<endl;
        cout<<"Student::score = "<<score<<endl;
    }
};

class Party       //党员类
{
private:    //私有权限
    string activity;    //党组织活动
    string organization;    //组织
public:     //公共权限
    //无参构造
    Party()
    {cout<<"Party::无参构造"<<endl;}
    //有参构造
    Party(string ac,string org):activity(ac),organization(org)
    {
        cout<<"Party::有参构造"<<endl;
    }
    //析构函数
    ~Party()
    {
        cout<<"Party::析构函数"<<endl;
    }
    //成员函数
    void show()
    {
        cout<<"Party::activity = "<<activity<<endl;
        cout<<"Party::organization = "<<organization<<endl;
    }
};

class Stuleader:public Student,public Party     //派生出的学生干部类
{
private:    //私有权限
    string position;
public:     //公共权限
    //无参构造函数
    Stuleader()
    {
        cout<<"Stuleader::无参构造"<<endl;
    }
    //有参构造函数
    Stuleader(string n,int a,double s,string ac,string org,string p):Student(n,a,s),Party(ac,org),position(p)
    {
        cout<<"Stuleader::有参构造"<<endl;
    }
    //析构函数
    ~Stuleader()
    {
        cout<<"Stuleader::析构函数"<<endl;
    }

    //成员函数
    void show()
    {
        this->Student::show();          //父子类同名,调用父类(Student)继承下来的成员
        this->Party::show();            //父子类同名,调用父类(Party)继承下来的成员
        cout<<"Stuleader::position = "<<position<<endl;  //自己类中的私有成员,类内可以访问
    }
};

int main()
{
    Stuleader s1;   //无参构造
    Stuleader s2("aaa",18,98.5,"bbb","ccc","ddd");  //有参构造
    s2.show();
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值