5-3 多级派生类的构造函数
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
通过本题目的练习可以掌握派生类构造函数的定义和使用方法。
要求定义一个基类Person,它有3个protected的数据成员:姓名name(char *类型)、性别 sex(char类型)、年龄age(int类型);一个构造函数用于对数据成员初始化。
创建Person类的公有派生类Employee,增加两个数据成员 基本工资 basicSalary(int类型) 请假天数leaveDays(int型);为它定义初始化成员信息的构造函数,和显示数据成员信息的成员函数show()。
创建Employee类的公有派生类Manager;增加一个成员 业绩 performance(float类型);为它定义初始化成员信息的构造函数,和显示数据成员信息的成员函数show()。
Input
共6个数据,分别代表姓名、性别、年龄、基本工资、请假天数、业绩。每个数据之间用一个空格间隔。
Output
如示例数据所示,共5行,分别代表姓名、年龄、性别、基本工资、请假天数、业绩
Example Input
Jerry m 32 4200 1 100
Example Output
name:Jerry
age:32
sex:m
basicSalary:4200
leavedays:1
performance:100
Hint
Author
黄晶晶
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;
class person
{
protected:
char *name = new char[25];
char sex;
int age;
public:
person() {}
person(char *na, char s, int a) :name(na), sex(s), age(a) {}
void show()
{
cout << "name:" << name << endl
<< "age:" << age << endl
<< "sex:" << sex << endl;
}
};
class employee :public person
{
protected:
int basicsalary;
int leavedays;
public:
employee() {}
employee(char *na, char s, int age, int b, int l) :person(na, s, age), basicsalary(b), leavedays(l) {}
void show()
{
cout << "basicSalary:" << basicsalary << endl
<< "leavedays:" << leavedays << endl;
}
};
class manager :public employee
{
public:
float performance;
manager(char *na, int a, char s, int ba, int le, float per) :employee(na, s, a, ba, le), performance(per) {}
void show()
{
cout << "name:" << name << endl
<< "age:" << age << endl
<< "sex:" << sex << endl
<< "basicSalary:" << basicsalary << endl
<< "leavedays:" << leavedays << endl
<< "performance:" << performance << endl;
}
};
int main()
{
char *n = new char[25];
char s;
int a, b, l;
float per;
cin >> n >> s >> a >> b >> l >> per;
manager perc(n, a, s, b, l, per);
perc.show();
return 0;
}