用c++虚基类设计一个小型公司的信息管理系统
时间: 2023-12-26 15:04:55 浏览: 88
好的,我可以为您解答这个问题。
首先,我们需要确定该信息管理系统需要管理哪些信息,例如员工信息、部门信息、薪资信息等等。接着,我们可以设计一个虚基类 `Employee`,来包含员工的共同属性和方法,例如姓名、工号、工资等等。
```cpp
class Employee {
public:
virtual ~Employee() {}
virtual std::string getName() const = 0;
virtual int getId() const = 0;
virtual double getSalary() const = 0;
virtual void display() const = 0;
};
```
接下来,我们可以定义一些派生类,例如 `Manager`、`Engineer`、`Salesman`,来表示不同类型的员工,并实现虚基类 `Employee` 中的纯虚函数。
```cpp
class Manager : public virtual Employee {
public:
Manager(const std::string& name, int id, double salary, std::string department)
: name_(name), id_(id), salary_(salary), department_(department) {}
std::string getName() const override { return name_; }
int getId() const override { return id_; }
double getSalary() const override { return salary_; }
std::string getDepartment() const { return department_; }
void display() const override {
std::cout << "Manager: " << getName() << ", ID: " << getId() << ", Department: " << getDepartment()
<< ", Salary: " << getSalary() << std::endl;
}
private:
std::string name_;
int id_;
double salary_;
std::string department_;
};
class Engineer : public virtual Employee {
public:
Engineer(const std::string& name, int id, double salary, std::string project)
: name_(name), id_(id), salary_(salary), project_(project) {}
std::string getName() const override { return name_; }
int getId() const override { return id_; }
double getSalary() const override { return salary_; }
std::string getProject() const { return project_; }
void display() const override {
std::cout << "Engineer: " << getName() << ", ID: " << getId() << ", Project: " << getProject()
<< ", Salary: " << getSalary() << std::endl;
}
private:
std::string name_;
int id_;
double salary_;
std::string project_;
};
class Salesman : public virtual Employee {
public:
Salesman(const std::string& name, int id, double salary, double commission)
: name_(name), id_(id), salary_(salary), commission_(commission) {}
std::string getName() const override { return name_; }
int getId() const override { return id_; }
double getSalary() const override { return salary_ + commission_; }
double getCommission() const { return commission_; }
void display() const override {
std::cout << "Salesman: " << getName() << ", ID: " << getId() << ", Commission: " << getCommission()
<< ", Salary: " << getSalary() << std::endl;
}
private:
std::string name_;
int id_;
double salary_;
double commission_;
};
```
接下来,我们可以设计一个管理类 `Company`,来管理公司的信息。例如,我们可以使用 `std::vector` 来存储所有员工,并提供添加、删除、查询员工等操作。注意,我们需要使用虚继承,来避免多次继承 `Employee` 导致的二义性问题。
```cpp
class Company {
public:
void addEmployee(Employee* employee) {
employees_.push_back(employee);
}
void removeEmployee(int id) {
for (auto it = employees_.begin(); it != employees_.end(); ++it) {
if ((*it)->getId() == id) {
employees_.erase(it);
break;
}
}
}
Employee* findEmployee(int id) const {
for (auto employee : employees_) {
if (employee->getId() == id) {
return employee;
}
}
return nullptr;
}
void displayAll() const {
for (auto employee : employees_) {
employee->display();
}
}
private:
std::vector<Employee*> employees_;
};
```
最后,我们可以在 `main` 函数中创建一些员工,并添加到公司信息管理系统中。
```cpp
int main() {
Company company;
Employee* manager = new Manager("Alice", 1001, 10000.0, "HR");
Employee* engineer = new Engineer("Bob", 1002, 8000.0, "Project A");
Employee* salesman = new Salesman("Charlie", 1003, 5000.0, 2000.0);
company.addEmployee(manager);
company.addEmployee(engineer);
company.addEmployee(salesman);
company.displayAll();
company.removeEmployee(1002);
Employee* employee = company.findEmployee(1001);
if (employee) {
std::cout << "Found employee: " << employee->getName() << std::endl;
} else {
std::cout << "Employee not found" << std::endl;
}
return 0;
}
```
这样,我们就实现了一个简单的小型公司信息管理系统。
阅读全文