人员管理
设计某小型公司的 employee(人员)类
1、类
1)employee 类:
基本信息:编号、姓名、性别、出生日期、职位等;
出生日期使用自定义的 Date(日期)类;
其中:基本信息为 private 属性,成员函数为 public 属性;
多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数;
可以从外部访问类成员的友员函数;
2)Date 类:
成员变量:年、月、日
成员函数:SetYear(int year)、SetMonth(int month)、SetDay(int day)
GetYear()、GetMonth()、GetDay()
2、基本功能:
1)职工信息的录入;
2)职工信息的显示;
3)用对象数组保存已输入的职工对象;
4)可以修改人员的基本信息,如:姓名、职位等;
5)可以通过编号或姓名进行人员查询;
本人是个小菜鸡,最近在做老师布置的作业。里面有很多bug,哈哈希望大家小心别掉坑里了!
以后改bug不用devc++了,太难受了,还是用回vs爽。
希望大家看后能有一点点收获!
#include<iostream>
#include<string>
using namespace std;
class Data {
public:
Data(int y = 1900, int m = 1, int d = 1) {
year = y;
month = m;
day = d;
}
void SetYear(int y) {
year = y;
}
void SetMonth(int m) {
month = m;
}
void SetDay(int d) {
day = d;
}
int GetYear() {
return year;
}
int GetMonth() {
return month;
}
int GetDay() {
return day;
}
private:
int year;
int month;
int day;
};
class employee {
public:
employee();//缺省构造函数
employee(int, string, char, Data&, string);//带参数构造函数。
//employee(int ,string ,char,Data&,string="worker");//默认职位worker。
void SetInfo(string nam, string pos) {
name = nam;
position = pos;
}
void Show(employee* p, int n);
Data& operator = (Data&);
friend istream& operator >>(istream&, employee&);
friend ostream& operator <<(ostream&, employee&);
int GetNum() { return num; }
string GetName() { return name; }
void SearchNum(int, employee*, int);
void SearchName(string, employee*, int);
private:
int num;
string name;
char sex;
Data birthday;
string position;
};
employee::employee() {
num = 1000;
name = "xiaoming";
sex = 'f';
Data birthday(1900, 1, 1);
position = "worker";
}
employee::employee(int n, string nam, char s, Data& bir, string pos) {
num = n;
name = nam;
sex = s;
Data birthay = bir;
position = pos;
}
void employee::Show(employee* p, int n) {
cout << "编号、姓名、性别、出生年、月、日、职位为:" << endl;
for (int i = 0; i < n; i++) {
cout << p[i] << endl;
}
}
Data& employee::operator =(Data& bir) {
bir.SetYear(this->birthday.GetYear());
bir.SetMonth(this->birthday.GetMonth());
bir.SetDay(this->birthday.GetDay());
return bir;
}
istream& operator >>(istream& input, employee& emp) {
int year, month, day;
input >> emp.num >> emp.name >> emp.sex;
input >> year >> month >> day;
input >> emp.position;
emp.birthday.SetYear(year);
emp.birthday.SetMonth(month);
emp.birthday.SetDay(day);
return input;
}
ostream& operator <<(ostream& output, employee& emp) {
output << emp.num << " " << emp.name << " " << emp.sex << " ";
output << emp.birthday.GetYear() << " " << emp.birthday.GetMonth() << " " << emp.birthday.GetDay() << " ";
output << emp.position;
return output;
}
void employee::SearchNum(int num, employee* p, int n) {
bool flag = 1;
for (int i = 0; i < n; i++) {
if (num == p[i].GetNum()) {
cout << "所找的员工的信息为:" << endl;
cout << p[i];
flag = 0;
break;
}
}
if (flag)cout << "没有编号为" << num << "的员工,请确认编号输入是否正确!" << endl;
}
void employee::SearchName(string name, employee* p, int n) {
bool flag = 1;
for (int i = 0; i < n; i++) {
if (name==p[i].GetName()) {
cout << "所找的员工的信息为:" << endl;
cout << p[i];
flag = 0;
break;
}
}
if (flag)cout << "没有姓名为" << name << "的员工,请确认姓名输入是否正确!" << endl;
}
int main() {
cout << "请输入你想输入的员工的个数" << endl;
int n;
cin >> n;
cout << endl;
//创立员工数组:
employee emp[100];
for (int i = 0; i < n; i++) {
cout << "请输入第" << i + 1 << "个员工的编号、姓名、性别、出生年、月、日、职位";
cin >> emp[i];
cout << endl;
}
emp[0].Show(emp, n);
//修改信息:
cout << "请输入你想修改的上面员工的序号,并输入新的姓名和职位"<<endl;
int x;
while (cin >> x && x) {
string name, position;
cin >> name >> position;
emp[x - 1].SetInfo(name, position);
cout << "若您还想修改信息,请输入上面展示的员工的序号,并输入新的姓名和职位,否则输入0结束" << endl;
}
cout << "---------------------------------------------------" << endl << endl;
emp[0].Show(emp, n);
cout << endl;
cout << "输入1用编号查询员工信息" << endl;
cout << "输入2用姓名查询员工信息" << endl;
cout << "输入其他值,结束。" << endl;
cin >> x;
switch (x) {
case 1: {
int num;
cout << "请输入你要找的员工的编号:";
cin >> num;
emp[0].SearchNum(num, emp, n);
break;
}
case 2: {
string name;
cout << "请输入你要找的员工的姓名:";
cin >> name;
emp[0].SearchName(name, emp, n);
break;
}
}
return 0;
}
运行结果: