继承与多态C++实验2.3

/*3.开发一个简单的大学人员管理程序,该程序可以管理大学的一些基本人员:
学生(student)、教师(teacher)、教授(professor)。首先设计一个虚基类person。
通过该类保存人员的最基本信息:姓名(name)、年龄(age)和性别(sex)。
然后使用该类派生出学生类student和教师类teacher,在其中添加各自的特性,
如在student类中添加如下信息:专业(speciality),在teacher类中添加院系(department)等。
还有部分教师在工作的同时在职修读学位,因此同时具有教师和学生双重身份,
所以由student类和teacher类再次派生出stuTeacher类。为每个类定义一个输出函数print(),输出该类相关信息。 */
#include<iostream>
using namespace std;

class person
{
private:
    string name;
    int age;
    string sex;
public://fengzhuang
    void setname(string name2)
    {
        name = name2;
    }
    string getname()
    {
        return name;
    }
    void setage(int age2)
    {
        age = age2;
    }
    int getage()
    {
        return age;
    }
    void setsex(string sex2)
    {
        sex = sex2;
    }
    string getsex()
    {
        return sex;
    }
    void print();
};


void person::print()
{
    cout << "姓名:" << getname() << ",年龄:" << getage() <<",性别:" <<  getsex() << endl;
}


class student:public person
{
private:
    string speciality;
public:
    student(){};
    ~student()
    {
        cout << "student Disconstruct" << endl;
    }
    student(string name2,int age2,string sex2,string speciality2)
    {
        setname(name2);
        setage(age2);
        setsex(sex2);
        setspeciality(speciality2);
    }
    void setspeciality(string speciality2)
    {
        speciality = speciality2;
    }
    string getspeciality()
    {
        return speciality;
    }
    void print();
};


void student::print()
{
    cout << "学生的姓名是:" << getname() << ",年龄:" << getage() << ",性别:" << getsex() << ",专业:" << getspeciality() <<endl;
}

class teacher:public person
{
private:
    string department;
public:
    teacher(){};
    ~teacher()
    {
        cout << "teacher Disconstruct" << endl;
    }
    teacher(string name2,int age2,string sex2,string department2)
    {
        setname(name2);
        setage(age2);
        setsex(sex2);
        setdepartment(department2);
    }
    void setdepartment(string department2)
    {
        department = department2;
    }
    string getdepartment()
    {
        return department;
    }
   void print();
};


void teacher::print()
{
    cout << "老师的姓名是:" << getname() << ",年龄:" << getage() << ",性别:" << getsex() << ",部门:" << getdepartment() << endl;
}


class stuteacher:public student,public teacher
{
public:
    stuteacher(string name2,string sex2, int age2,string speciality2,string department2)
    {
        teacher::setname(name2);
        teacher::setsex(sex2);
        teacher::setage(age2);
        setspeciality(speciality2);
        setdepartment(department2);
    }
    void print();
    ~stuteacher(){};
};


void stuteacher::print()
{
   cout<<"姓名:"<<teacher::getname()<<",年龄:"<<teacher::getage()<<",性别:"<<teacher::getsex()<<",专业:"<<getspeciality()<<"部门 :"<<getdepartment()<<endl;
}

int main()
{
    cout << "欢迎进入大学人员管理系统:" << endl;
    student s("王通",20,"男","物联网");
    s.print();
    teacher t("王老师",20,"男","计算机与信息工程学院");
    t.print();
    stuteacher st("老师兼学生","男",20,"计算机与信息工程学院","计科");
    st.print();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值