1.定义一个人员类Person,包括姓名、编号、性别等数据成员和用于输入、输出的成员函数,在此基础上派生出学生类Student类(增加成绩)和教师类Teacher(增加教龄),并实现对学生和教师信息的

#include <iostream>
#include<string.h>
using namespace std;
class  person
{   private:
    string name,sex;
    int number;
    public:
    void setperson(string n,string s,int nu)
    {
        name = n;
        sex = s;
        number = nu;
    }
    void output();
};
void person::output()
{
    cout<<"name: "<<name<<endl;
    cout<<"sex: "<<sex<<endl;
    cout<<"number: "<<number<<endl;
}
class student:public person
{
    private:
    int grade;
    public:
    student(){}
    student(string n,string s,int nu,int g)
    {
        person::setperson(n,s,nu);
        grade=g;
    }
   void output()
   {
       person::output();
       cout<<"grade: "<<grade<<endl;
   }
};
class teacher:public person
{
    private:
    int teachage;
    public:
    teacher(){}
    teacher(string n,string s,int nu,int t)
    {
        person::setperson(n,s,nu);
       teachage=t ;
    }
   void output()
   {
       person::output();
       cout<<"teachage: "<<teachage<<endl;
   }
};
int main()
{
    student m("李磊","男",16064,100);
    m.output();
     teacher n("赵宝宝","男",1141,20);
    n.output();
}

    原文作者:内涵段子TVb
    原文地址: https://blog.csdn.net/qq_41973128/article/details/80038493
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞