用c++设计一个employee类,包含员工基本信息:编号、姓名、出生日期、性别和职务等。出生日期使用自定义的Date类。employee类有可以从外部访问类的友元函数。程序基本职能如下:1.职工信息的录入。2.职工信息的显示;3.用对象数组保存已输入的职工对象;4.可以修改人员的基本信息;5.可以通过编号或姓名进行人员查询

时间: 2024-02-24 20:57:45 浏览: 21
好的,下面是代码实现: ```cpp #include <iostream> #include <string> using namespace std; // 自定义 Date 类 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() { cout << year << "-" << month << "-" << day << endl; } }; // 员工类 class Employee { private: int id; // 员工编号 string name; // 员工姓名 Date birthdate; // 出生日期 string gender; // 性别 string position; // 职务 public: Employee(int i, string n, Date b, string g, string p): id(i), name(n), birthdate(b), gender(g), position(p) {} // 友元函数,可以从外部访问私有成员变量 friend void displayEmployee(Employee e); // 修改员工信息 void modify(int i, string n, Date b, string g, string p) { id = i; name = n; birthdate = b; gender = g; position = p; } // 查询员工信息 bool match(int i) { return id == i; } bool match(string n) { return name == n; } }; // 显示员工信息 void displayEmployee(Employee e) { cout << "编号:" << e.id << endl; cout << "姓名:" << e.name << endl; cout << "出生日期:"; e.birthdate.display(); cout << "性别:" << e.gender << endl; cout << "职务:" << e.position << endl; } int main() { const int MAX_SIZE = 100; // 最大员工数 Employee employees[MAX_SIZE]; // 员工数组 int count = 0; // 已录入员工数 while (true) { // 显示功能菜单 cout << "请选择操作:\n"; cout << "1. 录入员工信息\n"; cout << "2. 显示员工信息\n"; cout << "3. 修改员工信息\n"; cout << "4. 查询员工信息\n"; cout << "5. 退出程序\n"; int option; cin >> option; if (option == 1) { // 录入员工信息 cout << "请输入员工信息:\n"; int id; string name; int year, month, day; string gender; string position; cout << "编号:"; cin >> id; cout << "姓名:"; cin >> name; cout << "出生日期(用空格隔开):"; cin >> year >> month >> day; Date birthdate(year, month, day); cout << "性别:"; cin >> gender; cout << "职务:"; cin >> position; Employee e(id, name, birthdate, gender, position); employees[count] = e; count++; } else if (option == 2) { // 显示员工信息 for (int i = 0; i < count; i++) { displayEmployee(employees[i]); } } else if (option == 3) { // 修改员工信息 cout << "请输入员工编号:"; int id; cin >> id; bool found = false; for (int i = 0; i < count; i++) { if (employees[i].match(id)) { found = true; cout << "请输入修改后的员工信息:\n"; string name; int year, month, day; string gender; string position; cout << "姓名:"; cin >> name; cout << "出生日期(用空格隔开):"; cin >> year >> month >> day; Date birthdate(year, month, day); cout << "性别:"; cin >> gender; cout << "职务:"; cin >> position; employees[i].modify(id, name, birthdate, gender, position); break; } } if (!found) { cout << "未找到该员工!\n"; } } else if (option == 4) { // 查询员工信息 cout << "请选择查询方式:\n"; cout << "1. 按编号查询\n"; cout << "2. 按姓名查询\n"; int queryOption; cin >> queryOption; if (queryOption == 1) { cout << "请输入员工编号:"; int id; cin >> id; bool found = false; for (int i = 0; i < count; i++) { if (employees[i].match(id)) { found = true; displayEmployee(employees[i]); break; } } if (!found) { cout << "未找到该员工!\n"; } } else if (queryOption == 2) { cout << "请输入员工姓名:"; string name; cin >> name; bool found = false; for (int i = 0; i < count; i++) { if (employees[i].match(name)) { found = true; displayEmployee(employees[i]); } } if (!found) { cout << "未找到该员工!\n"; } } else { cout << "无效的操作!\n"; } } else if (option == 5) { // 退出程序 cout << "程序已退出!\n"; break; } else { // 无效的操作 cout << "无效的操作!\n"; } } return 0; } ``` 使用了一个 `Employee` 类和一个 `Date` 类,其中 `Employee` 类包含了员工的基本信息,并且实现了录入、显示、修改和查询员工信息的功能。其中,`Employee` 类的 `displayEmployee` 函数是一个友元函数,可以从外部访问类的私有成员变量。 在 `main` 函数中,先声明了一个最大员工数 `MAX_SIZE` 和一个员工数组 `employees`,以及一个已录入员工数 `count`。主要功能实现是一个循环,每次根据用户选择的选项进行相应的操作,直到用户选择退出程序。在每个选项中,通过读入用户输入的参数和调用类的成员函数实现相应的功能。 注:以上代码仅供参考,使用时需要根据具体情况进行修改和完善。

最新推荐

recommend-type

C++实现日期类(Date类)的方法

下面小编就为大家带来一篇C++实现日期类(Date类)的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

C++自定义封装socket操作业务类完整实例

主要介绍了C++自定义封装socket操作业务类,结合完整实例形式分析了Linux环境下C++操作socket的封装业务类,可实现基本的socket连接、参数设置、发送请求等基本功能,需要的朋友可以参考下
recommend-type

C++使用MFC获取PC硬件配置信息

主要为大家详细介绍了C++使用MFC获取PC硬件配置信息,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C++ 中boost::share_ptr智能指针的使用方法

主要介绍了C++ 中boost::share_ptr智能指针的使用方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
recommend-type

C++通过自定义函数找出一个整数数组中第二大数的方法

主要介绍了C++通过自定义函数找出一个整数数组中第二大数的方法,涉及C++针对数组的遍历操作相关技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。