2.3作业————封装类别

 

#include <iostream>

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 Leader
{
protected:
    string post;
    int salary;
public:
    //无参构造
    Leader(){cout<<"Leader::无参构造"<<endl;}
    //有参构造
    Leader(string p,int s):post(p),salary(s)
    {
        cout<<"Leader::有参构造"<<endl;
    }
    //拷贝构造函数
    Leader(const Leader &other):post(other.post),salary(other.salary)
    {
        cout<<"Leader::拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Leader &operator=(const Leader &other)
    {
        if(this != &other)
        {
            this->post = other.post;
            this->salary = other.salary;
        }
        cout<<"Leader::拷贝赋值函数"<<endl;
        return *this;
    }
    //析构函数
    ~Leader()
    {
        cout<<"Leader::析构函数"<<endl;
    }
    //show函数
    void show()
    {
        cout<<"post = "<<post<<endl;
        cout<<"salary = "<<salary<<endl;
    }
};

//派生的学生干部类
class Stu_leader:public Student,public Leader
{
private:
    int num;     //管理人数
public:
    //无参构造
    Stu_leader(){cout<<"Stu_leader::无参构造"<<endl;}
    //有参构造
    Stu_leader(string n,int a,double s,string p,int sa,int nu):Student(n,a,s),Leader(p,sa),num(nu)
    {
        cout<<"Stu_leader::有参构造"<<endl;
    }
    //拷贝构造函数
    Stu_leader(const Stu_leader &other):Student(other),Leader(other)
    {
        cout<<"Stu_leader::拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Stu_leader &operator=(const Stu_leader &other)
    {
        if(this != &other)
        {
            this->num = other.num;
            //调用学生类拷贝赋值函数
            Student::operator=(other);
            //调用领导类拷贝赋值函数
            Leader::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<<"Lead::post = "<<post<<endl;
        cout<<"Lead::salary = "<<salary<<endl;
        cout<<"Stu_leader::num = "<<num<<endl;
    }

};

int main()
{

    Student s1("csy",23,95);
    Student s2 = s1;
    Student s3;
    s3 = s1;
    s1.show();

    cout<<endl;
    Leader l1("校长",5000);
    Leader l2 = l1;
    Leader l3;
    l3 = l1;
    l1.show();
    l2.show();
    l3.show();

    Stu_leader p1("yy",22,99,"学生会主席",5000,12);
    p1.show();
    Stu_leader p2 = p1;
    p2.show();
    Stu_leader p3;
    p3 = p1;
    p3.show();
    return 0;
}

执行结果

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值