4/23作业继承

该代码示例展示了C++中如何使用类来表示学生、党员和学生干部,包括无参构造、有参构造和析构函数。学生类(Stu)包含姓名、年龄和分数属性,党员类(Party)包含活动和组织属性,而学生干部类(Leader)继承自Stu和Party,添加了职位属性。在main函数中创建并展示了不同对象的属性。
摘要由CSDN通过智能技术生成
#include <iostream>
 
using namespace std;
 
//学生类
class Stu
{
private:
    string name;
    int age;
    double score;
 
public:
    //无参构造
    Stu()
    {cout << "Stu::无参构造" << endl;}
    //有参构造
    Stu(string n,int a,double s):name(n),age(a),score(s)
    {cout << "Stu::有参构造" << endl;}
    //析构函数
    ~Stu() {}
    //展示
    void show()
    {
        cout << "name = " << name << " age = " << age << " score = " << score << endl;
    }
};
 
//党员类
class Party
{
private:
    string activity;
    string organization;
 
public:
    //无参构造
    Party()
    {cout << "Party::无参构造" << endl;}
    //有参构造
    Party(string act,string org):activity(act),organization(org)
    {cout << "Party::有参构造" << endl;}
    //析构函数
    ~Party() {}
    void show()
    {
        cout << "activity = " << activity << " organization = " << organization << endl;
    }
};
 
//学生干部
class Leader:public Stu,public Party
{
private:
    string postion;
public:
    //无参构造
    Leader():Stu("孙凡凡",18,89),Party("采访","新闻部")
    {cout << "Leader::无参构造" << endl;}
    //有参构造
    Leader(string n,int a,double s,string act,string org,string pos):Stu(n,a,s),Party(act,org),postion(pos)
    {cout << "Leader::有参构造" << endl;}
    //析构函数
    ~Leader() {}
    //展示
    void show()
    {
        cout << "postion = " << postion << endl;
    }
};
int main()
{                                                               
    Leader s1;                                             
 
    Leader s2;                                               
    s2.Stu::show();                                           
    s2.Party::show();                                        
 
    Leader s3("邓金宝",19,98,"支部大会","卫生部","部长");       
    s3.Stu::show();                                              
    s3.Party::show();                                            
    s3.show();                                                   
 
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

malingshu404

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值