用c++定义一个职工类Employee 数据成员 (private): 职工姓名 (char *name)、性别 (bool sex)、年龄、工龄、工资 成员函数 (public): 构造函数:职工基本信息的初始化(姓名、性别、年龄、工龄、工资) 析构函数:释放系统资源(堆空间) 修改职工姓名 修改工资 年薪计算(12×工资+年终奖) 工龄增加 显示职工信息 要求:将类的定义与类成员函数的实现分
时间: 2023-12-19 10:25:39 浏览: 40
别写在同一个文件中。
以下是 `Employee.h` 的代码:
```cpp
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
class Employee {
private:
char* name;
bool sex;
int age;
int workYears;
double salary;
public:
Employee(char* name, bool sex, int age, int workYears, double salary);
~Employee();
void setName(char* name);
void setSalary(double salary);
double getAnnualSalary();
void increaseWorkYears();
void displayInfo();
};
#endif
```
以下是 `Employee.cpp` 的代码:
```cpp
#include "Employee.h"
#include <iostream>
#include <cstring>
Employee::Employee(char* name, bool sex, int age, int workYears, double salary) {
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->sex = sex;
this->age = age;
this->workYears = workYears;
this->salary = salary;
}
Employee::~Employee() {
delete[] name;
}
void Employee::setName(char* name) {
delete[] this->name;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
void Employee::setSalary(double salary) {
this->salary = salary;
}
double Employee::getAnnualSalary() {
return 12 * salary + workYears * 1000;
}
void Employee::increaseWorkYears() {
workYears++;
}
void Employee::displayInfo() {
std::cout << "Name: " << name << std::endl;
std::cout << "Sex: " << (sex ? "Male" : "Female") << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Work years: " << workYears << std::endl;
std::cout << "Salary: " << salary << std::endl;
std::cout << "Annual salary: " << getAnnualSalary() << std::endl;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)