C++类的多重继承的简单应用

1、作业要求

        封装一个学生类(Student):包括受保护成员:姓名、年龄、分数,完成其相关函数,以及show。
        在封装一个领导类(Leader):包括受保护成员:岗位、工资,完成其相关函数及show
        由以上两个类共同把派生出学生干部类:引入私有成员:管辖人数,完成其相关函数以及show函数。
        在主程序中,测试学生干部类:无参构造、有参构造、拷贝构造、拷贝赋值、show。

2、实现过程

1).inherit.h文件

#ifndef INHERIT_H
#define INHERIT_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(const Student &other);

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

    //打印函数
    void show();
};


class Leader        //领导类
{
protected:
    string post;    //岗位
    double wages;   //工资

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

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

    //拷贝构造
    Leader(const Leader &other);
    //拷贝赋值
    Leader &operator=(const Leader &other);

    //打印函数
    void show();
};


class Stu_cadres:public Student, public Leader
{
private:
    int manage;     //管理人数

public:
    //无参构造
    Stu_cadres();
    //有参构造
    Stu_cadres(string n,int a,double s,string p,double w,int m);

    //拷贝构造
    Stu_cadres(const Stu_cadres &other);

    //拷贝赋值
    Stu_cadres &operator=(const Stu_cadres &other);

    //打印函数
    void show();
};

#endif // INHERIT_H

2).inherit.cpp文件

#include <iostream>
#include <inherit.h>

using namespace std;

//学生类的函数
//无参构造
Student::Student() {cout<<"Student::无参构造"<<endl;}

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

//拷贝构造
Student::Student(const Student &other):name(other.name),age(other.age),score(other.score)
{
    cout<<"Student::拷贝构造"<<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<<"姓名 = "<<name<<endl;
    cout<<"年龄 = "<<age<<endl;
    cout<<"分数 = "<<score<<endl;
}


//领导类的函数
//无参构造
Leader::Leader() {cout<<"Leader::无参构造"<<endl;}

//有参构造
Leader::Leader(string p, double w):post(p), wages(w)
{cout<<"Leader::有参构造"<<endl;}

//拷贝构造
Leader::Leader(const Leader &other):post(other.post), wages(other.wages)
{
    cout<<"Leader::拷贝构造"<<endl;
}

//拷贝赋值
Leader &Leader::operator=(const Leader &other)
{
    cout<<"Leader::拷贝赋值"<<endl;
    this->post = other.post;
    this->wages = other.wages;

    return *this;
}

//打印函数
void Leader::show()
{
    cout<<"岗位 = "<<post<<endl;
    cout<<"工资 = "<<wages<<endl;
}


//学生干部类的函数
//无参构造
Stu_cadres::Stu_cadres() {cout<<"Stu_cadres::无参构造"<<endl;}

//有参构造
Stu_cadres::Stu_cadres(string n,int a,double s,string p,double w,int m):Student(n,a,s),Leader(p,w),manage(m)
{
    cout<<"Stu_cadres::有参构造"<<endl;

}

//拷贝构造
Stu_cadres::Stu_cadres(const Stu_cadres &other):Student(other),Leader(other),manage(other.manage)
{
    cout<<"Stu_cadres::拷贝构造"<<endl;
}

//拷贝赋值
Stu_cadres &Stu_cadres::operator=(const Stu_cadres &other)
{
    cout<<"Stu_cadres::拷贝赋值"<<endl;
    this->name = other.name;
    this->age = other.age;
    this->score = other.score;
    this->post = other.post;
    this->wages = other.wages;
    this->manage = other.manage;

    return *this;
}

//打印函数
void Stu_cadres::show()
{
    cout<<"姓名 = "<<name<<endl;
    cout<<"年龄 = "<<age<<endl;
    cout<<"分数 = "<<score<<endl;
    cout<<"岗位 = "<<post<<endl;
    cout<<"工资 = "<<wages<<endl;
    cout<<"管理人数 = "<<manage<<endl;
}

3).main.cpp文件

#include <iostream>
#include <inherit.h>

using namespace std;

int main()
{
    Stu_cadres s1("张三", 23, 145, "主席", 3000, 50);
    Stu_cadres s2 = s1;
    s2.show();

    cout<<endl<<"**************************************"<<endl<<endl;

    Stu_cadres s3("李四", 22, 130, "副主席", 2500, 20);
    Stu_cadres s4;
    s4 = s3;
    s4.show();

    return 0;
}

3、效果截图

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值