类的继承 例题

①定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法(无参构造、有参构造、析构函数、show函数)​
②再定义一个党员类(Party):私有成员属性(党组织活动,组织),成员方法(无参构造、有参构造、析构函数、show函数)。
③由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,然后调用其show函数进行测试

#include <iostream>

using namespace std;
class Student
{
private:
    string name;
    int age;
    double score;
public:
    //无参构造
    Student()
    {
        cout<<"Student::无参构造"<<endl;
    }
    //有参构造
    Student(string name,int age,double score):name(name),age(age),score(score)
    {
        cout<<"Student::有参构造"<<endl;
    }
    //析构函数
    ~Student()
    {
        cout<<"Student::析构函数"<<endl;
    }
    void show()
    {
        cout<<"学生姓名为:"<<name<<endl;
        cout<<"学生年龄为:"<<age<<endl;
        cout<<"学生分数为:"<<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<<"党组织活动:"<<activity<<endl;
        cout<<"组织:"<<organization<<endl;
    }
};
class Cadres :public Student,public Party
{
private:
    string position;
public:
    //无参构造
    Cadres()
    {
        cout<<"Cadres::无参构造"<<endl;
    }
    //有参构造
    Cadres(string name,int age,double score,string activity,string organization,string position):
    Student(name,age,score),Party(activity,organization),position(position)
    {
        cout<<"Cadres::有参构造"<<endl;
    }
    //析构函数
    ~Cadres()
    {
        cout<<"Cadres::析构函数"<<endl;
    }
    void show()
    {
        Student::show();
        Party::show();
        cout<<"职位:"<<position<<endl;
    }
};
int main()
{
    Cadres c("lzp",22,99.9,"人大","计科","代表");
    cout<<"*********************************"<<endl;
    c.show();
    cout<<"*********************************"<<endl;
    return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值