5-3 多级派生类的构造函数
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
Person,它有name(char *类型)、年龄创建basicSalary(int型);为它定义初始化成员信息的构造函数,和显示数据成员信息的成员函数Employee类的公有派生类);为它定义初始化成员信息的构造函数,和显示数据成员信息的成员函数6个数据,分别代表姓名、性别、年龄、基本工资、请假天数、业绩。每个数据之间用一个空格间隔。
Output
<p class='\"cjk\"' style='\"margin-bottom:' 0cm\"="">如示例数据所示,共<font face='\"Times' new="" roman,="" serif\"="">
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 <cstring>
using namespace std;
class Person
{
private:
char name[200];
char sex;
int age;
public:
Person(char *n, char s, int a);//基类构造函数
void show();
};
Person::Person(char *n, char s, int a)
{
strcpy(name,n);
sex = s;
age = a;
}
void Person::show()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
}
class Employee :public Person
{
private:
int basicSalary,leaveDays;
public:
Employee(char *n, char s, int a,int b,int l):Person(n,s,a)//派生类的构造函数
{
basicSalary = b;
leaveDays = l;
}
void show();
};
void Employee::show()
{
Person::show();//在派生类中调用基类的成员函数
cout<<"basicSalary:"<<basicSalary<<endl;
cout<<"leavedays:"<<leaveDays<<endl;
}
class Manager:public Employee//定义基类(Person)的派生类(Employee)的派生类
{
private:
double performance;
public:
Manager(char *n, char s, int a,int b,int l,double p):Employee(n,s,a,b,l)//派生类的构造函数(和Employee的一个套路)
{
performance = p;
}
void show();
};
void Manager::show()
{
Employee::show();//一个套路
cout<<"performance:"<<performance<<endl;
}
int main()
{
char n[200];
char s;
int a,b,l;
double p;
cin>>n>>s>>a>>b>>l>>p;
Manager m(n,s,a,b,l,p);
m.show();
return 0;
}