嵌入式学习Day36(C++)

 

作业

定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法(无参构造、有参构造、析构函数、show函数)​

再定义一个党员类(Party):私有成员属性(党组织活动,组织),成员方法(无参构造、有参构造、析构函数、show函数)。​

由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,然后调用其show函数进行测试

 

#include <iostream>
#include <cstring>

using namespace std;

class student
{
private:
    string name;
    int age;
    int score;
public:
    //无参构造
    student() {}

    //有参构造
    student(string n,int a,int s):name(n),age(a),score(s) {}

    //析构函数
    ~student(){cout << "析构函数" << endl;}
    void show()
    {
        cout << "姓名:" << name << " ";
        cout << "年龄:" << age << " ";
        cout << "分数:" << score << endl;

    }
    student &operator=(const student &R)
    {
        if(this!=&R)
        {
            this->name=R.name;
            this->age=R.age;
            this->score=R.score;
        }
        return *this;

    }
};



class party
{
private:
    string activity;
    string orgza;
public:
    //无参函数
    party(){}
    //有参函数
    party(string a,string o):activity(a),orgza(o){}
    //析构函数
    ~party(){cout << "析构函数" <<endl;}

    void show()
    {
        cout << "活动:" << activity << " ";
        cout << "组织:" << orgza << endl;
    }

    party & operator=(const party &R)
    {
        if(this!=&R)
        {
            this->activity = R.activity;
            this->orgza = R.orgza;
        }
        return *this;

    }
};

class stuofficer:public student,public party
{
private:
    string position;
public:
    //无参构造
    stuofficer(){}
    //有参构造
    stuofficer(string n,int a,int s,string  act,string o,string p):student(n,a,s),party(act,o),position(p){}
    //析构函数
    ~stuofficer(){cout << "析构函数" << endl;}
    void show()
    {
        student::show();
        party::show();
        cout << "职位:" << position << endl;

    }
    stuofficer & operator=(const stuofficer &R)
    {
        if(this!=&R)
        {
            this->student ::operator=(R);
            this->party::operator=(R);
            this->position=R.position;

        }
        return *this;
    }

};
int main()
{
    student s("zhangsan",18,98);
    s.show();
    party p("game","game player");
    p.show();
    stuofficer stu("zpp",18,98,"活动","共青团","团员");
    stu.show();
    stuofficer stu1("zpp1",19,99,"活动","共产党","党员");
    stu=stu1;
    stu.show();
    return 0;

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值