3开发一个简单的大学人员管理程序,该程序可以管理大学的一些基本人员:学生(student)、教师(teacher)、教授(professor)。首先设计一个虚基类person。通过该类保存人员的最

本文通过一个具体的C++程序示例介绍了多重继承的概念及应用。该程序定义了`person`基类,并从该基类派生出`student`和`teacher`两个子类。进一步地,又创建了一个`stuteacher`类,它同时继承自`student`和`teacher`,展示了如何在一个类中实现多种角色的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<string.h>
using namespace std;
class person
{
private:
    string name,sex;
    int age;
public:
     person  (string n,string s,int a)
    {
        name=n;
        sex=s;
        age=a;
    }
 virtual  void print()
    {
        cout<<"姓名: "<<name<<endl;
        cout<<"性别: "<<sex<<endl;
        cout<<"年龄: "<<age<<endl;

    }
};
class student:public person
{
private:
    string speciality;
public:
    friend class stuacher;
    student (string n,string s,int a,string sp):person(n,s,a)
    {
        speciality=sp;
    }
     void print()
    {
        person::print();
        cout<<"专业: "<<speciality<<endl;
    }
};
class teacher:public person
{
private:
    string department;
public:
    friend class stuteacher;
   teacher (string n,string s,int a,string d):person(n,s,a)
    {
        department=d;
    }
    string   show(){

            cout<<"系别: "<<department<<endl;
    }
   virtual void print()
    {
        person::print();
           cout<<"系别: "<<department<<endl;
    }
};
class stuteacher:public student,public teacher
{
public:
  stuteacher(string n,string s,int a,string sp,string d):student(n,s,a,sp),teacher(n,s,a,d)
    {

    }
   virtual void print(){
        student::print();
        teacher::show();
   }
};
int main()
{
    student s("王二麻子","男",20,"物联网");
    s.print();
    teacher t("二嘎子","男",10,"计算机");
     t.print();
    stuteacher st("栋宝宝","男",40,"物联网","在计算机学院教物联网");
    st.print();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值