求可运行C++程序关于雇员类

2025-01-08 09:49:29
推荐回答(1个)
回答1:

#include
using namespace std;

class employee
{
protected:
char name[15];
int age;
double salary;
public:
employee()
{
name[0] = '\0';
age = 0;
salary = 0;
}
~employee(){}
void create()
{
cout << "enter name:";
cin >> name;
cout << "enter age:";
cin >> age;
cout << "enter salary:";
cin >> salary;
}
void show()
{
cout << "name: " << name << endl
<< "age: " << age << endl
<< "salary: " << salary << endl;
}
};

class manager: public employee
{
protected:
char level[10];
public:
manager():employee(){level[0] = '\0';}
~manager(){}
void create()
{
employee::create();
cout << "enter level:";
cin >> level;
}
void show()
{
employee::show();
cout << "level: " << level << endl;
}
};

class engineer: public employee
{
protected:
char profession[20];
char degree[20];
public:
engineer():employee()
{
profession[0] = '\0';
degree[0] = '\0';
}
~engineer(){}
void create()
{
employee::create();
cout << "enter profession:";
cin >> profession;
cout << "enter degree:";
cin >> degree;
}
void show()
{
employee::show();
cout << "profession: " << profession << endl
<< "degree: " << degree << endl;
}
};

class director: public engineer
{
protected:
char position[20];
public:
director():engineer(){position[0] = '\0';}
~director(){}
void create()
{
engineer::create();
cout << "enter position:";
cin >> position;
}
void show()
{
engineer::show();
cout << "position: " << position << endl;
}
};

int main()
{
director example;
example.create();
example.show();
return 0;
}

/* // 测试输入输出
enter name:a
enter age:123
enter salary:123.2
enter profession:c++
enter degree:master
enter position:projectmanager
name: a
age: 123
salary: 123.2
profession: c++
degree: master
position: projectmanager

*/