c++某公司需要存储雇员的编号、姓名、性别、所在部门,级别,并进行工资的计算。其中的雇员分为经理、技术人员。月薪计算方法如下:经理拿固定目薪;技术人员按小时领取月薪。功能需求: (1)设计一程序能够对公司人员进行管理,应用到继承、抽象类、虚函数、虚基类、多态和文件的输入/输出等内容。 (2)添加功能:根据用户选择的人员类别,添加经理或者技术人员。要求员工的编号要唯一,如果添加了重复编号的记录时,则提示数据添加重复,并取消添加。 (3)查询功能:可根据编号、姓名等信息对已添加的记录进行查询,如果未找到,给出相应的提示信息,如果找到,则显示相应的记录信息;可显示当前系统中所有记录,每条记录占据一行。
时间: 2023-06-27 20:04:37 浏览: 377
这是一个比较完整的程序设计题目,需要综合运用到继承、抽象类、虚函数、虚基类、多态和文件的输入/输出等知识点。下面是一份可能的实现方案,仅供参考。
首先,我们可以定义一个抽象基类`Employee`,用于描述雇员的基本信息和月薪计算方法。该类中定义了纯虚函数`calculateSalary()`,用于计算月薪,以及一些基本的成员变量,如编号、姓名、性别、所在部门和级别等。
```cpp
class Employee {
public:
virtual double calculateSalary() = 0;
int getId() const { return id; }
void setId(int id) { this->id = id; }
const std::string& getName() const { return name; }
void setName(const std::string& name) { this->name = name; }
char getGender() const { return gender; }
void setGender(char gender) { this->gender = gender; }
const std::string& getDepartment() const { return department; }
void setDepartment(const std::string& department) { this->department = department; }
int getLevel() const { return level; }
void setLevel(int level) { this->level = level; }
protected:
int id;
std::string name;
char gender;
std::string department;
int level;
};
```
接下来,我们可以定义两个具体派生类,分别是`Manager`和`Technician`,用于描述经理和技术人员。这两个类都继承自`Employee`,并实现了`calculateSalary()`函数。
```cpp
class Manager : public Employee {
public:
double calculateSalary() override { return baseSalary; }
private:
static constexpr double baseSalary = 10000.0;
};
class Technician : public Employee {
public:
double calculateSalary() override { return hourlyWage * hoursWorked; }
double getHourlyWage() const { return hourlyWage; }
void setHourlyWage(double hourlyWage) { this->hourlyWage = hourlyWage; }
double getHoursWorked() const { return hoursWorked; }
void setHoursWorked(double hoursWorked) { this->hoursWorked = hoursWorked; }
private:
double hourlyWage;
double hoursWorked;
};
```
在实现查询功能时,可以定义一个`EmployeeDatabase`类,用于管理所有雇员记录。该类中包含一个`std::vector<Employee*>`类型的成员变量,用于存储所有雇员的指针。同时,该类还提供了一些接口函数,用于添加、查询和显示雇员记录。
```cpp
class EmployeeDatabase {
public:
Employee* addEmployee(Employee* employee);
Employee* findEmployeeById(int id) const;
std::vector<Employee*> findEmployeesByName(const std::string& name) const;
void displayAllEmployees() const;
private:
std::vector<Employee*> employees;
};
```
其中,`addEmployee()`函数用于添加一个新的雇员记录,需要先检查该雇员的编号是否已经存在于记录中。如果是,则返回`nullptr`,表示添加失败。否则,将该雇员指针添加到`employees`中,并返回该指针。
```cpp
Employee* EmployeeDatabase::addEmployee(Employee* employee) {
for (auto e : employees) {
if (e->getId() == employee->getId()) {
return nullptr;
}
}
employees.push_back(employee);
return employee;
}
```
`findEmployeeById()`函数用于根据编号查找雇员记录,如果找到,则返回该雇员指针,否则返回`nullptr`。
```cpp
Employee* EmployeeDatabase::findEmployeeById(int id) const {
for (auto e : employees) {
if (e->getId() == id) {
return e;
}
}
return nullptr;
}
```
`findEmployeesByName()`函数用于根据姓名查找雇员记录,返回所有姓名匹配的雇员指针组成的向量。
```cpp
std::vector<Employee*> EmployeeDatabase::findEmployeesByName(const std::string& name) const {
std::vector<Employee*> result;
for (auto e : employees) {
if (e->getName() == name) {
result.push_back(e);
}
}
return result;
}
```
`displayAllEmployees()`函数用于显示所有雇员记录,每条记录占据一行。
```cpp
void EmployeeDatabase::displayAllEmployees() const {
for (auto e : employees) {
std::cout << e->getId() << " " << e->getName() << " " << e->getGender() << " "
<< e->getDepartment() << " " << e->getLevel() << " " << e->calculateSalary() << std::endl;
}
}
```
最后,在主函数中,我们可以根据用户选择的人员类别,添加经理或者技术人员。同时,还可以根据编号、姓名等信息对已添加的记录进行查询,如果未找到,给出相应的提示信息,如果找到,则显示相应的记录信息。
```cpp
int main() {
EmployeeDatabase db;
while (true) {
std::cout << "1. Add Manager" << std::endl;
std::cout << "2. Add Technician" << std::endl;
std::cout << "3. Find Employee by ID" << std::endl;
std::cout << "4. Find Employees by Name" << std::endl;
std::cout << "5. Display All Employees" << std::endl;
std::cout << "6. Quit" << std::endl;
int choice;
std::cin >> choice;
if (choice == 1) {
Manager* manager = new Manager();
std::cout << "Enter ID: ";
std::cin >> manager->setId;
std::cout << "Enter Name: ";
std::cin >> manager->setName;
std::cout << "Enter Gender: ";
std::cin >> manager->setGender;
std::cout << "Enter Department: ";
std::cin >> manager->setDepartment;
std::cout << "Enter Level: ";
std::cin >> manager->setLevel;
db.addEmployee(manager);
} else if (choice == 2) {
Technician* technician = new Technician();
std::cout << "Enter ID: ";
std::cin >> technician->setId;
std::cout << "Enter Name: ";
std::cin >> technician->setName;
std::cout << "Enter Gender: ";
std::cin >> technician->setGender;
std::cout << "Enter Department: ";
std::cin >> technician->setDepartment;
std::cout << "Enter Level: ";
std::cin >> technician->setLevel;
std::cout << "Enter Hourly Wage: ";
std::cin >> technician->setHourlyWage;
std::cout << "Enter Hours Worked: ";
std::cin >> technician->setHoursWorked;
db.addEmployee(technician);
} else if (choice == 3) {
int id;
std::cout << "Enter ID: ";
std::cin >> id;
Employee* employee = db.findEmployeeById(id);
if (employee == nullptr) {
std::cout << "Employee not found" << std::endl;
} else {
std::cout << employee->getId() << " " << employee->getName() << " " << employee->getGender() << " "
<< employee->getDepartment() << " " << employee->getLevel() << " " << employee->calculateSalary() << std::endl;
}
} else if (choice == 4) {
std::string name;
std::cout << "Enter Name: ";
std::cin >> name;
std::vector<Employee*> employees = db.findEmployeesByName(name);
if (employees.empty()) {
std::cout << "No employees found" << std::endl;
} else {
for (auto e : employees) {
std::cout << e->getId() << " " << e->getName() << " " << e->getGender() << " "
<< e->getDepartment() << " " << e->getLevel() << " " << e->calculateSalary() << std::endl;
}
}
} else if (choice == 5) {
db.displayAllEmployees();
} else if (choice == 6) {
break;
}
}
return 0;
}
```
注意,在实际应用中,可能还需要考虑更多的异常处理和数据验证等问题,这里省略了一些细节。
阅读全文