5-3 多级派生类的构造函数
Time Limit: 1000MS
Memory Limit: 65536KB
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
#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
cin>>name>>sex>>age;
}
void show()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
}
protected:
char name[64];
char sex;
int age;
};
class Employee:public Person
{
public:
Employee():Person()
{
cin>>basicSalary>>leaveDays;
}
void show1()
{
show();
cout<<"basicSalary:"<<basicSalary<<endl;
cout<<"leavedays:"<<leaveDays<<endl;
}
private:
int basicSalary;
int leaveDays;
};
class Manager:public Employee
{
public:
Manager()
{
cin>>performance;
}
void show2()
{
show1();
cout<<"performance:"<<performance<<endl;
}
private:
float performance;
};
int main()
{
Manager m;
m.show2();
return 0;
}
using namespace std;
class Person
{
public:
Person()
{
cin>>name>>sex>>age;
}
void show()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
}
protected:
char name[64];
char sex;
int age;
};
class Employee:public Person
{
public:
Employee():Person()
{
cin>>basicSalary>>leaveDays;
}
void show1()
{
show();
cout<<"basicSalary:"<<basicSalary<<endl;
cout<<"leavedays:"<<leaveDays<<endl;
}
private:
int basicSalary;
int leaveDays;
};
class Manager:public Employee
{
public:
Manager()
{
cin>>performance;
}
void show2()
{
show1();
cout<<"performance:"<<performance<<endl;
}
private:
float performance;
};
int main()
{
Manager m;
m.show2();
return 0;
}