实现公司员工的管理,设计employee类,包含员工基本信息:编号,姓名,性命,出生日期和职务等。出生日期使用自定义的date类,employee类有可能从外部访问类成员的友元函数,程序基本功能有:1职工信息的录入,2职工信息的显示3.用对象数组保存已输入的职工对象4.可以修改人员的基本信息5.可以通过编号或姓名进行人员查询
时间: 2023-06-23 20:07:58 浏览: 190
实现某部门职工信息管理。职工信息包括:职工号、姓名、性别、年龄、学历、基本工资、岗位津贴、住址、电话等(职工号不重复)。
4星 · 用户满意度95%
好的,可以参考下面的代码实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Date {
private:
int year, month, day;
public:
Date() {}
Date(int y, int m, int d) : year(y), month(m), day(d) {}
void setDate(int y, int m, int d) { year = y; month = m; day = d; }
void showDate() { cout << year << "-" << month << "-" << day; }
};
class Employee {
private:
static int count;
int id;
string name;
char gender;
Date birthdate;
string job;
public:
Employee() {}
Employee(string n, char g, int y, int m, int d, string j) : id(++count), name(n), gender(g), birthdate(y, m, d), job(j) {}
void display() {
cout << "编号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "出生日期:";
birthdate.showDate();
cout << endl;
cout << "职务:" << job << endl;
}
void modify(string n, char g, int y, int m, int d, string j) {
name = n;
gender = g;
birthdate.setDate(y, m, d);
job = j;
}
friend class Manager;
};
int Employee::count = 0;
class Manager {
private:
int num;
Employee* emps;
public:
Manager(int n) : num(n) { emps = new Employee[n]; }
~Manager() { delete[] emps; }
void input() {
string name, job;
char gender;
int year, month, day;
for (int i = 0; i < num; i++) {
cout << "请输入第" << i + 1 << "个职工的信息:" << endl;
cout << "姓名:";
cin >> name;
cout << "性别:";
cin >> gender;
cout << "出生日期(年 月 日,用空格隔开):";
cin >> year >> month >> day;
cout << "职务:";
cin >> job;
emps[i] = Employee(name, gender, year, month, day, job);
}
}
void display() {
for (int i = 0; i < num; i++) {
emps[i].display();
cout << endl;
}
}
void modify(int id) {
for (int i = 0; i < num; i++) {
if (emps[i].id == id) {
string name, job;
char gender;
int year, month, day;
cout << "请输入修改后的职工信息:" << endl;
cout << "姓名:";
cin >> name;
cout << "性别:";
cin >> gender;
cout << "出生日期(年 月 日,用空格隔开):";
cin >> year >> month >> day;
cout << "职务:";
cin >> job;
emps[i].modify(name, gender, year, month, day, job);
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到编号为" << id << "的职工!" << endl;
}
void modify(string name) {
for (int i = 0; i < num; i++) {
if (emps[i].name == name) {
string newname, job;
char gender;
int year, month, day;
cout << "请输入修改后的职工信息:" << endl;
cout << "姓名:";
cin >> newname;
cout << "性别:";
cin >> gender;
cout << "出生日期(年 月 日,用空格隔开):";
cin >> year >> month >> day;
cout << "职务:";
cin >> job;
emps[i].modify(newname, gender, year, month, day, job);
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到姓名为" << name << "的职工!" << endl;
}
};
int main() {
int n;
cout << "请输入职工人数:";
cin >> n;
Manager manager(n);
manager.input();
int choice;
while (true) {
cout << "请选择功能:" << endl;
cout << "1.职工信息的显示" << endl;
cout << "2.修改职工信息(按编号)" << endl;
cout << "3.修改职工信息(按姓名)" << endl;
cout << "0.退出" << endl;
cin >> choice;
switch (choice) {
case 1:
manager.display();
break;
case 2:
int id;
cout << "请输入需要修改信息的职工编号:";
cin >> id;
manager.modify(id);
break;
case 3:
string name;
cout << "请输入需要修改信息的职工姓名:";
cin >> name;
manager.modify(name);
break;
case 0:
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
}
}
return 0;
}
```
该程序通过Manager类来管理Employee类的对象数组,实现了职工信息的录入、显示、修改、查询等基本功能。其中,date类用来表示出生日期,Employee类包括了员工基本信息,Manager类则负责管理Employee类的对象数组。友元函数的使用使得Manager类可以访问Employee类的私有成员。
阅读全文