【无标题】C++ day5(继承)(学生类习题)

这篇文章展示了C++中类的定义和使用,包括无参构造、有参构造、拷贝构造和拷贝赋值函数。同时,它探讨了类的继承概念,展示了一个学生类和党员类如何派生出学生干部类,并且处理了拷贝和赋值操作。代码示例详细说明了对象的生命周期和内存管理。
摘要由CSDN通过智能技术生成

#include <iostream>
#include <cstring>

using namespace std;

//学生类
class Student
{
protected:
    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(const Student &other):name(other.name),age(other.age),score(other.score)
    {
        cout<<"Student::拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Student &operator=(const Student &other)
    {
        if(this != &other)
        {
            this->name = other.name;
            this->age = other.age;
            this->score = other.score;
        }
        cout<<"Student::拷贝赋值函数"<<endl;
        return *this;
    }
    //析构函数
    ~Student()
    {
        cout<<"Student::析构函数"<<endl;
    }
    //show函数
    void show()
    {
        cout<<"name = "<<name<<endl;
        cout<<"age = "<<age<<endl;
        cout<<"score = "<<score<<endl;
    }
};

//党员类
class Party
{
private:
    string activity;
    string orgz;
public:
    //无参构造
    Party(){cout<<"Party::无参构造"<<endl;}
    //有参构造
    Party(string a,string o):activity(a),orgz(o)
    {
        cout<<"Party::有参构造"<<endl;
    }
    //拷贝构造函数
    Party(const Party &other):activity(other.activity),orgz(other.orgz)
    {
        cout<<"Party::拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Party &operator=(const Party &other)
    {
        if(this != &other)
        {
            this->activity= other.activity;
            this->orgz = other.orgz;
        }
        cout<<"Party::拷贝赋值函数"<<endl;
        return *this;
    }
    //析构函数
    ~Party()
    {
        cout<<"Party::析构函数"<<endl;
    }
    //show函数
    void show()
    {
        cout<<"activity = "<<activity<<endl;
        cout<<"orgz= "<<orgz<<endl;
    }
};

//派生的学生干部类
class Stu_leader:public Student,public Party
{
private:
    string position;
public:
    //无参构造
    Stu_leader(){cout<<"Stu_leader::无参构造"<<endl;}
    //有参构造
    Stu_leader(string n,int a ,double s,string a1,string o,string p):Student(n,a,s),Party(a1, o),position(p)
    {
        cout<<"Stu_leader::有参构造"<<endl;
    }
    //拷贝构造函数
    Stu_leader(const Stu_leader &other):Student(other),Party(other)
    {
        cout<<"Stu_leader::拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Stu_leader &operator=(const Stu_leader &other)
    {
        if(this != &other)
        {
            this->position = other.position;
            //调用学生类拷贝赋值函数
            Student::operator=(other);
            //调用党员类拷贝赋值函数
            Party::operator=(other);
        }
        cout<<"Stu_leader::拷贝赋值函数"<<endl;
        return *this;
    }
    //析构函数
    ~Stu_leader()
    {
        cout<<"Stu_leader::析构函数"<<endl;
    }
    //show函数
    void show()
    {
        cout<<"Student::name = "<<name<<endl;
        cout<<"Student::age = "<<age<<endl;
        cout<<"Student::score = "<<score<<endl;

        cout<<"Stu_leader::position = "<<position<<endl;
    }

};

int main()
{

    Student s1("sss",20,99);
    Student s2 = s1;
    Student s3;
    s3 = s1;
    s1.show();
    s2.show();
    s3.show();

    cout<<"*******************************************"<<endl;
    Party l1("运动会","体育部");
    Party l2 = l1;
    Party l3;
    l3 = l1;
    l1.show();
    l2.show();
    l3.show();

    cout<<"*******************************************"<<endl;
    Stu_leader p1("xkw",19,88,"运动会","体育部","干事");
    p1.show();
    Stu_leader p2 = p1;
    p2.show();
    Stu_leader p3;
    p3 = p1;
    p3.show();
    return 0;}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值