用C++语言写出解决下列问题的代码,某小型公司有四类人员:总经理、技术人员、销售经理、推销员。设计一个基类employee, 派生出manager(总经理)、technician(技术人员)、salesmanager(销售经理)、saleman(推销员)。销售经理既是经理又是销售人员,兼具两类人员的特点,因此同时继承manager 和salesman 两个类。 1、类定义 1)employee 类: 基本信息:编号、姓名、性别、出生日期、职位、薪水等; 出生日期使用自定义的 Date(日期)类; 其中:基本信息为 private 属性,成员函数为public 属性; 多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数; 可以从外部访问类成员的友员函数; 2)Date 类: 成员变量:年、月、日 成员函数:SetYear(int year)、SetMonth(int month)、SetDay(int day) GetYear()、GetMonth()、GetDay() 3)派生类technician:新增属性:工作时间 派生类saleman: 新增属性:销售额、所属部门 2、实现人员信息的录入与显示; 3、计算并显示个人月薪: 月薪计算办法:总经理拿固定月薪8000 元,技术人员按每小时25 元领取月薪;推销员 的月薪按当月销售额的4%提成;销售经理固定月薪5000 元加所管辖部门当月销售总额的5 ‰ 。
时间: 2023-11-27 18:03:45 浏览: 102
以下是基于题目要求的代码实现:
```
#include <iostream>
#include <string>
using namespace std;
class Date {
private:
int year;
int month;
int day;
public:
Date() {};
Date(int year, int month, int day) : year(year), month(month), day(day) {};
void SetYear(int year) { this->year = year; }
void SetMonth(int month) { this->month = month; }
void SetDay(int day) { this->day = day; }
int GetYear() { return year; }
int GetMonth() { return month; }
int GetDay() { return day; }
};
class employee {
private:
int id;
string name;
string gender;
Date birth;
string position;
double salary;
public:
employee() {};
employee(int id, string name, string gender, Date birth, string position, double salary) : id(id), name(name), gender(gender), birth(birth), position(position), salary(salary) {};
employee(int id, string name, string gender, Date birth, string position) : id(id), name(name), gender(gender), birth(birth), position(position) {};
friend void show_employee(employee emp);
virtual double calc_salary() { return salary; }
};
class manager : public employee {
public:
manager() {};
manager(int id, string name, string gender, Date birth) : employee(id, name, gender, birth, "manager", 8000) {};
};
class technician : public employee {
private:
int work_time;
public:
technician() {};
technician(int id, string name, string gender, Date birth, int work_time) : employee(id, name, gender, birth, "technician", 0), work_time(work_time) {};
double calc_salary() { return work_time * 25; }
};
class salesman : public employee {
private:
double sales;
string department;
public:
salesman() {};
salesman(int id, string name, string gender, Date birth, double sales, string department) : employee(id, name, gender, birth, "salesman", 0), sales(sales), department(department) {};
double calc_salary() { return sales * 0.04; }
};
class salesmanager : public manager, public salesman {
public:
salesmanager() {};
salesmanager(int id, string name, string gender, Date birth, double sales, string department) : employee(id, name, gender, birth, "salesmanager", 5000), salesman(id, name, gender, birth, sales, department) {};
double calc_salary() { return employee::calc_salary() + salesman::calc_salary(); }
};
void show_employee(employee emp) {
cout << "ID: " << emp.id << endl;
cout << "Name: " << emp.name << endl;
cout << "Gender: " << emp.gender << endl;
cout << "Birth: " << emp.birth.GetYear() << "-" << emp.birth.GetMonth() << "-" << emp.birth.GetDay() << endl;
cout << "Position: " << emp.position << endl;
cout << "Salary: " << emp.calc_salary() << endl;
}
int main() {
Date birth1(1990, 1, 1);
manager m1(1, "John", "male", birth1);
show_employee(m1);
Date birth2(1995, 3, 15);
technician t1(2, "Lucy", "female", birth2, 160);
show_employee(t1);
Date birth3(1998, 5, 20);
salesman s1(3, "Tom", "male", birth3, 10000, "IT department");
show_employee(s1);
Date birth4(1992, 8, 10);
salesmanager sm1(4, "Mike", "male", birth4, 20000, "Sales department");
show_employee(sm1);
return 0;
}
```
在这个代码中,我们首先定义了一个 `Date` 类来存储日期信息。接着定义了一个 `employee` 基类,包含了题目所要求的员工基本信息,以及一个虚函数 `calc_salary()` 来计算月薪。之后,我们按照题目要求分别定义了 `manager`、`technician`、`salesman` 和 `salesmanager` 四个派生类,分别重写了 `calc_salary()` 函数来计算不同职位员工的月薪。
在 `main()` 函数里,我们创建了四个员工对象,并分别调用 `show_employee()` 函数来展示他们的信息和月薪。
阅读全文