5-3 多級派生類的構造函數
Problem Description
要求定義一個基類3個name(char *類型sex(char類型age(int類型創建Employee,增加兩個數據成員 基本工資 int類型) 請假天數int型);爲它定義初始化成員信息的構造函數,和顯示數據成員信息的成員函數創建Manager;增加一個成員 業績 );爲它定義初始化成員信息的構造函數,和顯示數據成員信息的成員函數共如示例數據所示,共<font face="\"Times" new="" roman,="" serif\"="" style="box-sizing: border-box;">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>
using namespace std;
class Person
{
protected:
string name;
char sex;
int age;
public:
Person(string n, char s, int a) //基類構造函數
{
name = n;
sex = s;
age = a;
}
};
class Employee : public Person
{
private:
int basicSalary;
int leaveDays;
public:
Employee(string n, char s, int a, int b, int l) : Person(n, s, a) //派生類構造函數
{
basicSalary = b;
leaveDays = l;
}
void display() //派生類輸出
{
cout << "name:" << name << endl;
cout << "age:" << age << endl;
cout << "sex:" << sex << endl;
cout << "basicSalary:" << basicSalary << endl;
cout << "leavedays:" << leaveDays << endl; //這裏有個坑,注意以後輸出有樣例,一定要賦值樣例
}
};
class Manager : public Employee
{
private:
float performance;
public:
Manager(string n, char s, int a, int b, int l, float p) : Employee(n, s, a, b, l)
{
performance = p;
}
void show()
{
display();
cout << "performance:" << performance << endl;
}
};
int main()
{
string n;
char s;
int a, b, l;
float p;
cin >> n >> s >> a >> b >> l >> p;
Manager m(n, s, a, b, l, p);
m.show();
return 0;
}