2023-2-3作业

封装一个学生类(Student):包括受保护成员:姓名、年龄、分数,完成其相关函数,以及show

在封装一个领导类(Leader):包括受保护成员:岗位、工资,完成其相关函数及show

由以上两个类共同把派生出学生干部类:引入私有成员:管辖人数,完成其相关函数以及show函数

在主程序中,测试学生干部类:无参构造、有参构造、拷贝构造、拷贝赋值、show


header.h:

#ifndef HEADER_H
#define HEADER_H

#include <iostream>

using namespace std;

class Student
{
protected:
    string name;
    int age;
    double score;

public:
    Student();    //无参构造

    Student(string n, int a, double s);     //有参构造

    ~Student();   //析构函数

    Student(const Student &other);      //拷贝构造函数

    Student &operator=(const Student &other);       //拷贝赋值函数

    void show();
};

class Leader
{
protected:
    string job;
    double wage;

public:
    Leader();    //无参构造

    Leader(string j, double w);     //有参构造

    ~Leader();   //析构函数

    Leader(const Leader &other);    //拷贝构造函数

    Leader &operator=(const Leader &other);     //拷贝赋值函数

    void show();
};

class Stu_leader:public Student,public Leader
{
private:
    int num;

public:
    Stu_leader();  //无参构造

    Stu_leader(string n, int a, double s, string j, double w, int m);   //有参函数

    Stu_leader(const Stu_leader &other);    //拷贝构造函数

    Stu_leader &operator=(const Stu_leader &other);     //拷贝赋值函数

    void show();
};

#endif // HEADER_H

stu_leader.cpp:

#include <iostream>
#include <header.h>

using namespace std;

Student::Student()
{
    cout << "Stude::无参构造" << endl;
}

Student::Student(string n, int a, double s):name(n), age(a), score(s)
{
    cout << "Stude::有参构造" << endl;
}

Student::~Student()
{
    cout << "Stude::析构函数" << endl;
}

Student::Student(const Student &other):name(other.name), age(other.age), score(other.score)
{
    cout << "Stude::拷贝构造函数" << endl;
}

Student &Student::operator=(const Student &other)
{
    cout << "Student::拷贝赋值函数" << endl;
    this->name = other.name;
    this->age = other.age;
    this->score = other.score;

    return *this;
}

void Student::show()
{
    cout << "Student::name = " << name << endl;
    cout << "Student::age = " << age << endl;
    cout << "Student::score = " << score << endl;
}

Leader::Leader()
{
    cout << "Leader::无参构造" << endl;
}

Leader::Leader(string j, double w):job(j), wage(w)
{
    cout << "Leader::有参构造" << endl;
}

Leader::~Leader()
{
    cout << "Leader::析构函数" << endl;
}

Leader::Leader(const Leader &other):job (other.job), wage(other.wage)
{
    cout << "Leader::拷贝构造函数" << endl;
}

Leader &Leader::operator=(const Leader &other)
{
    cout << "Leader::拷贝赋值函数" << endl;
    this->job = other.job;
    this->wage = other.wage;

    return *this;
}

void Leader::show()
{
    cout << "Leader::job = " << job << endl;
    cout << "Leader::wage = " << wage << endl;
}

Stu_leader::Stu_leader()
{
    cout << "Stu_leader::无参构造" << endl;
}

Stu_leader::Stu_leader(string n, int a, double s, string j, double w, int m):Student(n,a,s), Leader(j,w), num(m)
{
    cout << "Stu_leader::有参函数" << endl;
}

Stu_leader::Stu_leader(const Stu_leader &other):Student(other), Leader(other), num(other.num)
{
    cout << "Stu_leader::拷贝构造函数" << endl;
}

Stu_leader &Stu_leader::operator=(const Stu_leader &other)
{
    cout << "Stu_leader::拷贝赋值函数" << endl;
    Student::operator=(other);
    Leader::operator=(other);
    this->num = other.num;

    return *this;
}

void Stu_leader::show()
{
    cout << "Stu_leader::name = " << name << endl;
    cout << "Stu_leader::age = " << age << endl;
    cout << "Stu_leader::score = " << score << endl;
    cout << "Stu_leader::job = " << job << endl;
    cout << "Stu_leader::wage = " << wage << endl;
    cout << "Stu_leader::num = " << num << endl;
}

main.cpp:

#include <iostream>
#include <header.h>

using namespace std;

int main()
{
    Stu_leader s;
    s.show();
    cout << "***************************" << endl;

    Stu_leader s1("jin",23,99,"会长",10000,10);
    s1.show();
    cout << "***************************" << endl;

    Stu_leader s2 = s1;
    s2.show();
    cout << "***************************" << endl;

    s = s1;
    s.show();
    cout << "***************************" << endl;

    return 0;
}

测试结果如下:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值