c++ 实现人员管理

功能:设计某小型公司的employee(职员)类

  • (1)employee类:
    基本信息:编号、姓名、性别、出生日期、职位等
    出生日期采用自定义的Date类
    其中:基本信息为private属性,成员函数为public属性
    employee类有多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数;可以从外部访问类成员的友元函数
    (2)Date类
    成员变量:年、月、日
    成员函数:SetYear(int year)、SetMonth(int month)、SetDay(int day)、GetYear()、GetMonth()、GetDay()
  • 基本功能
    (1)职工信息的录入
    (2)职工信息的显示
    (3)用对象数组保存已输入的职工对象
    (4)可以修改人员的基本信息,如:姓名、职位等
    (5)可以通过编号或姓名进行人员查询
注意:
//1、注意带参数的构造函数和带默认参数的构造函数的定义与声明
//2、定义employee类的成员变量时,应注意变量类型的声明
//3、在查询时,通过声明成emplyee的友元函数来访问类的成员变量
代码:
#include<iostream>
#include<string.h>
#define N 100   //总的职员个数 
using namespace std;

//Date类 
class Date
{
	public:
		Date();
		Date(int year, int month, int day);
		~Date(void);
		void SetYear(int year);
		void SetMonth(int month);
		void SetDay(int day);
		int GetYear();
		int GetMonth();
		int GetDay();
		
	private:
		int year;
		int month;
		int day;
};

//employee类
class Employee
{
	public:
		Employee();
		Employee( int ,int, int, string, string, char, string);
		~Employee(void);
		void SetInfo(int year, int month, int day, string number, string name, char sex, string position);
		void Show();
		
		friend int searchInfo(Employee* obj, string index)
		{
			if(index==obj->name||index==obj->number){
				cout<<"查询到关于"<<index<<"的相关信息如下:"<<endl;
				cout<<"姓名:"<<obj->name<<endl;
				cout<<"编号:"<<obj->number<<endl;
				cout<<"性别:"<<obj->sex<<endl;
				cout<<"出生日期:"<<obj->birthDay.GetYear()<<"-"<<obj->birthDay.GetMonth()<<"-"<<obj->birthDay.GetDay()<<endl;
				cout<<"职位:"<<obj->position<<endl;
				return 1;
			}
			else  return -1;
		}
	
	private:
		string number;
		string name;
		char sex;
		Date birthDay;
		string position;
 };
 
Employee::Employee()
{
	cout<<"Employee object is being created by constructor without parameters!"<<endl;
}

Employee::Employee(int year, int month, int day, string number, string name, char sex, string position):birthDay(year,month,day),number(number),name(name),sex(sex),position(position)
{
	cout<<"Employee object is being created by constructor with parameters!"<<endl;
}

Employee::~Employee(void)
{
	cout<<"Employee object is being deleted!"<<endl;
}

void Employee::SetInfo(int year, int month, int day, string number, string name, char sex, string position)
{
	this->birthDay.SetYear(year);
	this->birthDay.SetMonth(month);
	this->birthDay.SetDay(day); 
	this->number=number;
	this->name=name;
	this->sex=sex;
	this->position=position; 
}

void Employee::Show()
{
	cout<<"职员"<<this->name<<"的基本信息如下:"<<endl;
	cout<<"\t编号:"<<this->number<<endl;
	cout<<"\t性别:"<<this->sex<<endl;
	cout<<"\t出生日期:"<<this->birthDay.GetYear()<<"-"<<this->birthDay.GetMonth()<<"-"<<this->birthDay.GetDay()<<endl;
	cout<<"\t职位:"<<this->position<<endl; 
}


Date::Date(void)
{
	cout<<"Date object is being created by constructor without parameters!"<<endl;
}

Date::Date(int year, int month, int day):year(year),month(month),day(day)
{
	cout<<"Date object is being created by constructor with paramters!"<<endl;
}

Date::~Date(void)
{
	cout<<"Date object is being deleted!"<<endl;
}

void Date::SetYear(int year)
{
	this->year=year;
}

void Date::SetMonth(int month)
{
	this->month=month;
}

void Date::SetDay(int day)
{
	this->day=day;
}

int Date::GetYear()
{
	return year;
}

int Date::GetMonth()
{
	return month;
}

int Date::GetDay()
{
	return day;
}

//程序的主函数
int main()
{
	string number;
	string name;
	char sex;
	int year;
	int month;
	int day;
	string position;
	int input; //存放此次要录入信息的职员的个数 
	int total=0; //存放已录入信息的职员总数 
	int choice;//用户选择项 
	char flag; //判断释放需要修改信息 
	bool tag=true;//判断是否查询成功 
	string index;
	Employee *emp[N];
	while(1){
		cout<<"*****************************"<<endl;
		cout<<"*     1、职工信息录入       *"<<endl;
		cout<<"*     2、职工信息的显示     *"<<endl;
		cout<<"*     3、查询职工信息       *"<<endl;
		cout<<"*     0、退出               *"<<endl;
		cout<<"*****************************"<<endl;
		cout<<"请输入您的选项:"<<endl;
		cin>>choice;
		switch(choice){
			case 1:
				cout<<"请输入您要录取信息的职工个数:"<<endl;
				cin>>input;
				for(int i=0;i<input;i++){
					cout<<endl<<"请输入职工"<<i+1<<"的信息:"<<endl;
					cout<<"编号:";
					cin>>number;
					cout<<flush<<"姓名:";
					cin>>name;
					cout<<flush<<"性别(F/M):";
					cin>>sex; 
					cout<<flush<<"出生年月日:";
					cin>>year>>month>>day; 
					cout<<flush<<"职位:";
					cin>>position;
					emp[total+i]=new Employee(year,month,day,number,name,sex,position); 
					cout<<endl<<"是否需要对职工"<<name<<"的信息进行修改?Y/N"<<endl;
                    cin>>flag;
                    if(flag=='Y'||flag=='y'){
					    cout<<"请输入修改后职工"<<name<<"的信息:"<<endl;
						cout<<"编号:";
						cin>>number;
						cout<<flush<<"姓名:";
						cin>>name;
						cout<<flush<<"性别(F/M):";
						cin>>sex; 
						cout<<flush<<"出生年月日:";
						cin>>year>>month>>day;
						cout<<flush<<"职位:";
						cin>>position;
				        emp[total+i]->SetInfo(year,month,day,number,name,sex,position);
				        cout<<"信息修改成功!"<<endl;
					}
				}
				total=total+input;
				cout<<endl;
				break;
			case 2:
				cout<<"\n以下是已录入的所有职工的信息,共"<<total<<"个:\n"<<endl;
				for(int i=0;i<total;i++){
					emp[i]->Show();
				}
				cout<<endl;
				break;
			case 3:
				cout<<"\n请输入您要查询的职工名字或者编号:"<<endl;
				cin>>index;
				for(int i=0;i<total;i++){
					if( searchInfo(emp[i], index)>0 ){
						tag=false; 
						continue;
					}
				}
				if(tag){
					cout<<"很抱歉,没有查找到相关的信息,也许是信息还未录入!" <<endl;
				}
				tag=true;   //标志型的数据,在用完之后一定要恢复原来的值! 
				cout<<endl;
				break;
			case 0:
				for(int i=0;i<total;i++){
		            delete emp[i];
	            }
				exit(0);
			default:
				cout<<"输入不正确!"<<endl; 
		}
	}
	return 0;
 } 
运行结果截屏:

在这里插入图片描述

参考资料:

c++构造函数的默认参数
c++动态对象数组的知识总结
c++对象数组中delete 和 delete[] 的区别
c++对象数组调用构造/析构函数的方法

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值