实现公司员工的管理,设计employee 类,包含员工基本信息:编号,姓名,性别,出生日期和职务。出生日期使用自定义的Date 类。Employee 类有可以从外部访问类成员的友元函数。程序的基本功能有:1.职工信息的录入2.职工信息的显示。3.用对象数据保存已输入的职工对象4.可以修改人员的基本信息5.可以通过编号或姓名进行人员查询
时间: 2023-06-20 09:04:17 浏览: 165
以下是一个可能的实现:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Date {
private:
int year;
int month;
int day;
public:
Date(int y, int m, int d) : year(y), month(m), day(d) {}
void display() const {
cout << year << "/" << month << "/" << day;
}
};
class Employee {
private:
int id;
string name;
char gender;
Date birthdate;
string position;
public:
Employee(int i, const string& n, char g, const Date& b, const string& p) :
id(i), name(n), gender(g), birthdate(b), position(p) {}
friend void displayEmployee(const Employee& e);
friend bool modifyEmployee(Employee& e);
friend int searchEmployeeById(const vector<Employee>& employees, int id);
friend int searchEmployeeByName(const vector<Employee>& employees, const string& name);
};
void displayEmployee(const Employee& e) {
cout << "编号: " << e.id << endl;
cout << "姓名: " << e.name << endl;
cout << "性别: " << e.gender << endl;
cout << "出生日期: ";
e.birthdate.display();
cout << endl;
cout << "职务: " << e.position << endl;
}
bool modifyEmployee(Employee& e) {
cout << "请选择要修改的项目: " << endl;
cout << "1. 编号" << endl;
cout << "2. 姓名" << endl;
cout << "3. 性别" << endl;
cout << "4. 出生日期" << endl;
cout << "5. 职务" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入新的编号: ";
cin >> e.id;
break;
case 2:
cout << "请输入新的姓名: ";
cin >> e.name;
break;
case 3:
cout << "请输入新的性别: ";
cin >> e.gender;
break;
case 4:
cout << "请输入新的出生日期(年 月 日): ";
int year, month, day;
cin >> year >> month >> day;
e.birthdate = Date(year, month, day);
break;
case 5:
cout << "请输入新的职务: ";
cin >> e.position;
break;
default:
cout << "无效的选择" << endl;
return false;
}
return true;
}
int searchEmployeeById(const vector<Employee>& employees, int id) {
for (int i = 0; i < employees.size(); i++) {
if (employees[i].id == id) {
return i;
}
}
return -1;
}
int searchEmployeeByName(const vector<Employee>& employees, const string& name) {
for (int i = 0; i < employees.size(); i++) {
if (employees[i].name == name) {
return i;
}
}
return -1;
}
int main() {
vector<Employee> employees;
while (true) {
cout << "请选择操作: " << endl;
cout << "1. 录入职工信息" << endl;
cout << "2. 显示职工信息" << endl;
cout << "3. 保存职工信息" << endl;
cout << "4. 修改职工信息" << endl;
cout << "5. 查询职工信息" << endl;
cout << "6. 退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入职工信息: " << endl;
int id;
string name;
char gender;
int year, month, day;
string position;
cout << "编号: ";
cin >> id;
cout << "姓名: ";
cin >> name;
cout << "性别: ";
cin >> gender;
cout << "出生日期(年 月 日): ";
cin >> year >> month >> day;
cout << "职务: ";
cin >> position;
employees.push_back(Employee(id, name, gender, Date(year, month, day), position));
break;
case 2:
cout << "职工信息如下: " << endl;
for (const auto& e : employees) {
displayEmployee(e);
cout << endl;
}
break;
case 3:
// 保存到文件或数据库
cout << "职工信息已保存" << endl;
break;
case 4:
cout << "请输入要修改的职工编号或姓名: ";
string keyword;
cin >> keyword;
int index;
if (isdigit(keyword[0])) {
// 根据编号查找
index = searchEmployeeById(employees, stoi(keyword));
} else {
// 根据姓名查找
index = searchEmployeeByName(employees, keyword);
}
if (index == -1) {
cout << "未找到职工" << endl;
} else {
if (modifyEmployee(employees[index])) {
cout << "职工信息已修改" << endl;
} else {
cout << "职工信息未修改" << endl;
}
}
break;
case 5:
cout << "请输入要查询的职工编号或姓名: ";
cin >> keyword;
if (isdigit(keyword[0])) {
// 根据编号查找
index = searchEmployeeById(employees, stoi(keyword));
} else {
// 根据姓名查找
index = searchEmployeeByName(employees, keyword);
}
if (index == -1) {
cout << "未找到职工" << endl;
} else {
displayEmployee(employees[index]);
}
break;
case 6:
cout << "程序已退出" << endl;
return 0;
default:
cout << "无效的选择" << endl;
}
}
}
```
这个程序使用了 vector 容器来保存职工信息,使用了友元函数来访问类的私有成员。用户可以进行录入、显示、保存、修改和查询职工信息的操作。其中修改和查询可以根据编号或姓名进行。
阅读全文