定义一个职工类Employee 数据成员 (private): 职工姓名 (char name[20])、性别 (bool sex)、年龄、工龄、工资 成员函数 (public): 设置职工的基本信息:姓名、性别、年龄、工龄、工资 修改职工姓名 修改工资 年薪计算(12×工资+年终奖) 工龄增加 显示职工信息
时间: 2023-12-19 16:24:36 浏览: 29
好的,下面是一个简单的实现:
```c++
#include <iostream>
#include <cstring> // for strcpy()
using namespace std;
class Employee {
private:
char name[20];
bool sex;
int age;
int yearsOfService;
double salary;
public:
void setBasicInfo(char n[], bool s, int a, int yos, double sal) {
strcpy(name, n);
sex = s;
age = a;
yearsOfService = yos;
salary = sal;
}
void setName(char n[]) {
strcpy(name, n);
}
void setSalary(double sal) {
salary = sal;
}
double calculateAnnualSalary(double bonus) {
return 12 * salary + bonus;
}
void increaseYearsOfService() {
yearsOfService++;
}
void displayInfo() {
cout << "Name: " << name << endl;
cout << "Sex: " << (sex ? "Male" : "Female") << endl;
cout << "Age: " << age << endl;
cout << "Years of service: " << yearsOfService << endl;
cout << "Salary: " << salary << endl;
}
};
int main() {
Employee emp;
emp.setBasicInfo("John Smith", true, 30, 5, 5000);
emp.displayInfo();
cout << "Annual salary with bonus: " << emp.calculateAnnualSalary(10000) << endl;
emp.increaseYearsOfService();
emp.setSalary(6000);
emp.setName("John Doe");
emp.displayInfo();
return 0;
}
```
这个程序定义了一个名为`Employee`的类,用于表示一个职工。`Employee`类有五个私有成员变量`name`、`sex`、`age`、`yearsOfService`和`salary`,分别表示职工姓名、性别、年龄、工龄和工资。`Employee`类还有六个公共成员函数:`setBasicInfo()`用于设置职工的基本信息;`setName()`和`setSalary()`用于修改职工姓名和工资;`calculateAnnualSalary()`用于计算职工的年薪(包括12个月的工资和年终奖);`increaseYearsOfService()`用于增加职工的工龄;`displayInfo()`用于显示职工的信息。
在`main()`函数中,我们首先创建一个职工对象`emp`,并设置它的基本信息。然后我们显示职工的信息,并计算职工的年薪,增加职工的工龄,修改职工的工资和姓名,并再次显示职工的信息。