定义一个职工类Employee 数据成员 (private): 职工姓名 (char *name)、性别 (bool sex)、年龄、工龄、工资 成员函数 (public): 构造函数:职工基本信息的初始化(姓名、性别、年龄、工龄、工资) 析构函数:释放系统资源(堆空间) 修改职工姓名 修改工资 年薪计算(12×工资+年终奖) 工龄增加 显示职工信息c++
时间: 2023-12-19 09:25:28 浏览: 118
以下是职工类Employee的定义:
```c++
#include <iostream>
#include <cstring>
using namespace std;
class Employee {
private:
char* name;
bool sex;
int age;
int workYears;
double salary;
public:
Employee(const char* _name, bool _sex, int _age, int _workYears, double _salary) {
name = new char[strlen(_name) + 1];
strcpy(name, _name);
sex = _sex;
age = _age;
workYears = _workYears;
salary = _salary;
}
~Employee() {
delete[] name;
}
void setName(const char* _name) {
delete[] name;
name = new char[strlen(_name) + 1];
strcpy(name, _name);
}
void setSalary(double _salary) {
salary = _salary;
}
double getAnnualSalary() const {
return salary * 12 + workYears * 1000;
}
void increaseWorkYears(int years) {
workYears += years;
}
void displayInfo() const {
cout << "Name: " << name << endl;
cout << "Sex: " << (sex ? "Male" : "Female") << endl;
cout << "Age: " << age << endl;
cout << "Work Years: " << workYears << endl;
cout << "Salary: " << salary << endl;
}
};
```
其中,构造函数用于初始化职工基本信息,析构函数用于释放系统资源(堆空间),setName和setSalary函数用于修改姓名和工资,getAnnualSalary函数用于计算年薪,increaseWorkYears函数用于增加工龄,displayInfo函数用于显示职工信息。
阅读全文