c++编码。某公司需要存储雇员的编号、姓名、性别、所在部门,级别,并进行工资的计算。其中的雇员分为经理、技术人员。月薪计算方法如下:经理拿固定目薪;技术人员按小时领取月薪。功能需求: (1)设计一程序能够对公司人员进行管理,应用到继承、抽象类、虚函数、虚基类、多态和文件的输入/输出等内容。
时间: 2023-06-27 13:04:16 浏览: 110
以下是一份基本的实现代码,实现了上述功能需求:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// 雇员类
class Employee {
protected:
int id;
string name;
char gender;
string department;
int level;
public:
Employee() {}
Employee(int i, string n, char g, string d, int l) : id(i), name(n), gender(g), department(d), level(l) {}
virtual void printInfo() {}
virtual double getSalary() { return 0; }
};
// 经理类
class Manager : public virtual Employee {
protected:
double salary;
public:
Manager() {}
Manager(int i, string n, char g, string d, int l, double s) : Employee(i, n, g, d, l), salary(s) {}
void printInfo() {
cout << "经理信息:" << endl;
cout << "编号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "部门:" << department << endl;
cout << "级别:" << level << endl;
cout << "工资:" << salary << endl;
}
double getSalary() { return salary; }
};
// 技术人员类
class Technician : public virtual Employee {
protected:
double hourlyPay;
int hoursWorked;
public:
Technician() {}
Technician(int i, string n, char g, string d, int l, double h, int hw) : Employee(i, n, g, d, l), hourlyPay(h), hoursWorked(hw) {}
void printInfo() {
cout << "技术人员信息:" << endl;
cout << "编号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "部门:" << department << endl;
cout << "级别:" << level << endl;
cout << "工资:" << getSalary() << endl;
}
double getSalary() { return hourlyPay * hoursWorked; }
};
// 经理兼技术人员类
class ManagerTechnician : public Manager, public Technician {
public:
ManagerTechnician() {}
ManagerTechnician(int i, string n, char g, string d, int l, double s, double h, int hw) : Employee(i, n, g, d, l), Manager(i, n, g, d, l, s), Technician(i, n, g, d, l, h, hw) {}
void printInfo() {
cout << "经理兼技术人员信息:" << endl;
cout << "编号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "部门:" << department << endl;
cout << "级别:" << level << endl;
cout << "工资:" << getSalary() << endl;
}
double getSalary() { return Manager::getSalary() + Technician::getSalary(); }
};
// 公司类
class Company {
private:
vector<Employee*> employees;
public:
void addEmployee(Employee* e) {
employees.push_back(e);
}
void printAllInfo() {
for (auto e : employees) {
e->printInfo();
cout << endl;
}
}
void saveToFile(string filename) {
ofstream file(filename);
for (auto e : employees) {
file << e->getSalary() << "," << e->id << "," << e->name << "," << e->gender << "," << e->department << "," << e->level << endl;
}
file.close();
}
void loadFromFile(string filename) {
ifstream file(filename);
if (!file.is_open()) {
cout << "文件打开失败!" << endl;
return;
}
string line;
while (getline(file, line)) {
double salary;
int id, level;
string name, department;
char gender;
sscanf(line.c_str(), "%lf,%d,%[^,],%c,%[^,],%d", &salary, &id, &name[0], &gender, &department[0], &level);
if (level == 0) {
Manager* m = new Manager(id, name, gender, department, level, salary);
addEmployee(m);
}
else if (level == 1) {
Technician* t = new Technician(id, name, gender, department, level, salary, 0);
addEmployee(t);
}
else {
double h;
int hw;
sscanf(line.c_str(), "%lf,%d,%[^,],%c,%[^,],%d,%lf,%d", &salary, &id, &name[0], &gender, &department[0], &level, &h, &hw);
ManagerTechnician* mt = new ManagerTechnician(id, name, gender, department, level, salary, h, hw);
addEmployee(mt);
}
}
file.close();
}
};
int main() {
Company c;
Employee* e1 = new Manager(1001, "张三", 'M', "市场部", 0, 8000);
Employee* e2 = new Technician(1002, "李四", 'F', "技术部", 1, 100, 120);
Employee* e3 = new ManagerTechnician(1003, "王五", 'M', "生产部", 2, 10000, 120, 200);
c.addEmployee(e1);
c.addEmployee(e2);
c.addEmployee(e3);
c.printAllInfo();
c.saveToFile("employees.txt");
c.loadFromFile("employees.txt");
c.printAllInfo();
return 0;
}
```
这个程序定义了一个`Employee`雇员类,以及它的两个子类:`Manager`经理类和`Technician`技术人员类。其中,经理类有一个固定的月薪,技术人员类按小时领取月薪。为了实现经理兼技术人员这种情况,定义了`ManagerTechnician`类,继承了经理类和技术人员类。
程序还定义了一个`Company`公司类,负责管理所有的雇员。它提供了添加雇员、打印所有雇员信息、将雇员信息保存到文件、从文件中加载雇员信息等功能。
在`main`函数中,首先创建了三个雇员对象,并添加到公司中。然后打印所有雇员信息,并将雇员信息保存到文件。最后从文件中加载雇员信息,并再次打印所有雇员信息,以验证文件读写功能是否正常。
阅读全文