设计一个员工类CEmployee,其结构如下私有数据成员No (工号),Name (姓名),Age (年龄),Salary (薪资); 重载构造函数,实现对类实例对象的带参数初始化和无参数初始化; 公有成员函数SetData ();实现键盘随机输入对数据成员赋值; 公有成员函数Display();实现屏幕输出对数据成员; 在main0中定义对象,给出成员函数调用实例。在此基础上,派生出类经理类CManager和工程师类CEngineer,假定经理需比员工额外考虑行政级别,工程师额外考虑专业和学位
时间: 2024-03-14 13:44:23 浏览: 66
CEmployee类的定义如下:
```cpp
#include<iostream>
#include<string>
using namespace std;
class CEmployee {
private:
int No;
string Name;
int Age;
double Salary;
public:
CEmployee() : No(0), Name(""), Age(0), Salary(0.0) { }
CEmployee(int no, string name, int age, double salary) : No(no), Name(name), Age(age), Salary(salary) { }
void SetData();
void Display();
};
void CEmployee::SetData() {
cout << "请输入工号:";
cin >> No;
cout << "请输入姓名:";
cin >> Name;
cout << "请输入年龄:";
cin >> Age;
cout << "请输入薪资:";
cin >> Salary;
}
void CEmployee::Display() {
cout << "工号:" << No << endl;
cout << "姓名:" << Name << endl;
cout << "年龄:" << Age << endl;
cout << "薪资:" << Salary << endl;
}
```
CManager类的定义如下:
```cpp
#include<iostream>
#include<string>
#include"CEmployee.h"
using namespace std;
class CManager : public CEmployee {
private:
string Department;
int Level;
public:
CManager() : Department(""), Level(0) { }
CManager(int no, string name, int age, double salary, string department, int level) : CEmployee(no, name, age, salary), Department(department), Level(level) { }
void SetData();
void Display();
};
void CManager::SetData() {
CEmployee::SetData();
cout << "请输入部门:";
cin >> Department;
cout << "请输入行政级别:";
cin >> Level;
}
void CManager::Display() {
CEmployee::Display();
cout << "部门:" << Department << endl;
cout << "行政级别:" << Level << endl;
}
```
CEngineer类的定义如下:
```cpp
#include<iostream>
#include<string>
#include"CEmployee.h"
using namespace std;
class CEngineer : public CEmployee {
private:
string Speciality;
string Degree;
public:
CEngineer() : Speciality(""), Degree("") { }
CEngineer(int no, string name, int age, double salary, string speciality, string degree) : CEmployee(no, name, age, salary), Speciality(speciality), Degree(degree) { }
void SetData();
void Display();
};
void CEngineer::SetData() {
CEmployee::SetData();
cout << "请输入专业:";
cin >> Speciality;
cout << "请输入学位:";
cin >> Degree;
}
void CEngineer::Display() {
CEmployee::Display();
cout << "专业:" << Speciality << endl;
cout << "学位:" << Degree << endl;
}
```
在主函数中,可以这样定义对象并调用成员函数:
```cpp
#include<iostream>
#include"CEmployee.h"
#include"CManager.h"
#include"CEngineer.h"
using namespace std;
int main() {
CEmployee emp1(1001, "张三", 25, 5000);
cout << "员工1的信息:" << endl;
emp1.Display();
cout << endl;
CManager mgr1;
mgr1.SetData();
cout << "经理1的信息:" << endl;
mgr1.Display();
cout << endl;
CEngineer eng1;
eng1.SetData();
cout << "工程师1的信息:" << endl;
eng1.Display();
cout << endl;
return 0;
}
```
阅读全文