定义一个职工类Employee 数据成员 (private): 职工姓名 (char name[20])、性别 (bool sex)、年龄、工龄、工资 成员函数 (public): 设置职工的基本信息:姓名、性别、年龄、工龄、工资 修改职工姓名 修改工资 年薪计算(12×工资+年终奖) 工龄增加 显示职工信息
时间: 2023-12-19 08:24:40 浏览: 48
好的,以下是一个基本的职工类Employee的定义,包括您所要求的数据成员和成员函数:
```cpp
class Employee {
private:
char name[20];
bool sex;
int age;
int workYears;
double salary;
public:
// 构造函数
Employee(const char* name, bool sex, int age, int workYears, double salary);
// 设置职工的基本信息:姓名、性别、年龄、工龄、工资
void setInfo(const char* name, bool sex, int age, int workYears, double salary);
// 修改职工姓名
void setName(const char* name);
// 修改工资
void setSalary(double salary);
// 年薪计算(12×工资+年终奖)
double calcAnnualSalary(double bonus);
// 工龄增加
void addWorkYears(int years);
// 显示职工信息
void displayInfo();
};
// 构造函数
Employee::Employee(const char* name, bool sex, int age, int workYears, double salary) {
setInfo(name, sex, age, workYears, salary);
}
// 设置职工的基本信息:姓名、性别、年龄、工龄、工资
void Employee::setInfo(const char* name, bool sex, int age, int workYears, double salary) {
strcpy(this->name, name);
this->sex = sex;
this->age = age;
this->workYears = workYears;
this->salary = salary;
}
// 修改职工姓名
void Employee::setName(const char* name) {
strcpy(this->name, name);
}
// 修改工资
void Employee::setSalary(double salary) {
this->salary = salary;
}
// 年薪计算(12×工资+年终奖)
double Employee::calcAnnualSalary(double bonus) {
return 12 * salary + bonus;
}
// 工龄增加
void Employee::addWorkYears(int years) {
workYears += years;
}
// 显示职工信息
void Employee::displayInfo() {
cout << "姓名:" << name << endl;
cout << "性别:" << (sex ? "男" : "女") << endl;
cout << "年龄:" << age << endl;
cout << "工龄:" << workYears << endl;
cout << "工资:" << salary << endl;
}
```
这样,您就可以创建一个Employee对象,调用相应的成员函数来设置和显示职工信息了。例如:
```cpp
Employee emp("张三", true, 30, 5, 5000);
emp.displayInfo(); // 显示初始信息
emp.setSalary(6000); // 修改工资
emp.addWorkYears(1); // 工龄增加1年
emp.displayInfo(); // 显示修改后的信息
double annualSalary = emp.calcAnnualSalary(10000); // 计算年薪(假设年终奖为10000元)
cout << "年薪:" << annualSalary << endl;
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)