首页 > 代码库 > 5-3 多级派生类的构造函数

5-3 多级派生类的构造函数

                                                                                                       5-3  多级派生类的构造函数   

Time Limit: 1000MS    Memory limit: 65536K

题目描述

要求定义一个基类protected的数据成员:姓名sex(char类型);一个构造函数用于对数据成员初始化。

Employee,增加两个数据成员 基本工资leaveDaysEmployee类的公有派生类);为它定义初始化成员信息的构造函数,和显示数据成员信息的成员函数如示例数据所示,共<font face=‘\"Times‘ new="" roman,="" serif\"="">5行,分别代表姓名、年龄、性别、基本工资、请假天数、业绩

示例输入

Jerry m 32 4200 1 100

示例输出

name:Jerry
age:32
sex:m
basicSalary:4200
leavedays:1
performance:100
 
<span style="font-size:18px;">#include<iostream>
#include<string>
using namespace std;
class person
{
 protected:
     string name;
     string sex;
     int age;
 public:
    person(string n,string s,int a):name(n),sex(s),age(a){}
    void display()
    {
        cout<<"name:"<<name<<endl;
        cout<<"age:"<<age<<endl;
        cout<<"sex:"<<sex<<endl;
    }
};
class emp:public person
{
private:
    int salary;
    int day;
public:
    emp(string n,string s,int a,int sa,int d):person(n,s,a),salary(sa),day(d){}
    void show()
    {
        cout<<"basicSalary:"<<salary<<endl;
        cout<<"leavedays:"<<day<<endl;
    }
};
class manager:public emp
{
private:
    int per;
public:
    manager(string n,string s,int a,int sa,int d,int p):emp(n,s,a,sa,d),per(p){}
    void show()
    {
        cout<<"performance:"<<per<<endl;
    }
};
int main()
{
    string name,sex;
    int age,salary,day,per;
    cin>>name>>sex>>age>>salary>>day>>per;
    person p(name,sex,age);
    emp e(name,sex,age,salary,day);
    manager  m(name,sex,age,salary,day,per);
    p.display();
    e.show();
    m.show();
}






</span>

5-3 多级派生类的构造函数