基于c++多态,多重继承,以及虚基类,设计一个小型公司信息管理系统
时间: 2023-08-19 12:31:06 浏览: 175
好的,我会根据您的要求进行设计。首先,我们需要确定公司信息管理系统中需要管理哪些信息。一般而言,一个小型公司的信息包括员工信息、部门信息、工资信息等。我们可以先定义一个基类,称为Company,它包括了公司的基本信息,比如公司名称、地址等。
```c++
class Company {
public:
Company(const std::string& name, const std::string& address) : name_(name), address_(address) {}
virtual ~Company() {}
virtual std::string GetInfo() const {
return "Company name: " + name_ + " Address: " + address_;
}
private:
std::string name_;
std::string address_;
};
```
在Company的基础上,我们可以定义Employee(员工)、Department(部门)和Salary(工资)类。这三个类都是从Company类派生而来。
```c++
class Employee : virtual public Company {
public:
Employee(const std::string& name, const std::string& address, const std::string& id) :
Company(name, address), id_(id) {}
virtual ~Employee() {}
virtual std::string GetInfo() const {
return Company::GetInfo() + " Employee ID: " + id_;
}
private:
std::string id_;
};
class Department : virtual public Company {
public:
Department(const std::string& name, const std::string& address, const std::string& depName) :
Company(name, address), depName_(depName) {}
virtual ~Department() {}
virtual std::string GetInfo() const {
return Company::GetInfo() + " Department Name: " + depName_;
}
private:
std::string depName_;
};
class Salary : virtual public Company {
public:
Salary(const std::string& name, const std::string& address, double salary) :
Company(name, address), salary_(salary) {}
virtual ~Salary() {}
virtual std::string GetInfo() const {
return Company::GetInfo() + " Salary: " + std::to_string(salary_);
}
private:
double salary_;
};
```
我们可以看到,这三个类都重载了基类Company的GetInfo()函数,使得每个类可以返回自身的信息。
接下来,我们需要定义一个管理系统类,称为CompanyManager。该类可以对公司信息进行添加、修改、查询等操作。
```c++
class CompanyManager {
public:
void AddEmployee(const Employee& employee) {
employees_.push_back(employee);
}
void AddDepartment(const Department& department) {
departments_.push_back(department);
}
void AddSalary(const Salary& salary) {
salaries_.push_back(salary);
}
std::vector<Employee> SearchEmployee(const std::string& id) const {
std::vector<Employee> result;
for (const auto& employee : employees_) {
if (employee.GetID() == id) {
result.push_back(employee);
}
}
return result;
}
std::vector<Department> SearchDepartment(const std::string& depName) const {
std::vector<Department> result;
for (const auto& department : departments_) {
if (department.GetDepartmentName() == depName) {
result.push_back(department);
}
}
return result;
}
std::vector<Salary> SearchSalary(double salary) const {
std::vector<Salary> result;
for (const auto& s : salaries_) {
if (s.GetSalary() == salary) {
result.push_back(s);
}
}
return result;
}
private:
std::vector<Employee> employees_;
std::vector<Department> departments_;
std::vector<Salary> salaries_;
};
```
在CompanyManager中,我们定义了三个向量分别存储员工、部门和工资信息。同时,我们还定义了三个查询函数,分别用于查询员工、部门和工资信息。
最后,我们可以在main函数中测试我们的程序。
```c++
int main() {
CompanyManager company;
Employee e1("Tom", "Beijing", "001");
Employee e2("Jerry", "Shanghai", "002");
Department d1("HR", "Beijing", "HR Department");
Department d2("IT", "Shanghai", "IT Department");
Salary s1("Tom", "Beijing", 10000.0);
Salary s2("Jerry", "Shanghai", 12000.0);
company.AddEmployee(e1);
company.AddEmployee(e2);
company.AddDepartment(d1);
company.AddDepartment(d2);
company.AddSalary(s1);
company.AddSalary(s2);
auto employeeList = company.SearchEmployee("001");
for (const auto& e : employeeList) {
std::cout << e.GetInfo() << std::endl;
}
auto departmentList = company.SearchDepartment("IT Department");
for (const auto& d : departmentList) {
std::cout << d.GetInfo() << std::endl;
}
auto salaryList = company.SearchSalary(12000.0);
for (const auto& s : salaryList) {
std::cout << s.GetInfo() << std::endl;
}
return 0;
}
```
这样,我们就完成了一个小型的公司信息管理系统。
阅读全文