运行效果如下:
头文件:
#include
#include
using namespace std;
员工类:
class Cemployee
{
private:
string name;
int age;
string sex;
string ID;
string worknumber;
float salary;
string position;
public:
Cemployee();
float get_salary();
string get_name();
string get_worknumber();
void show_employee();
};
员工类函数实现如下:
Cemployee::Cemployee()
{
cout << "请输入员工信息" << endl;
cout << "员工姓名: " << endl;
cin >>name;
cout << "员工年龄: " << endl;
cin >>age;
cout<<"员工性别:"<<endl;
cin>>sex;
cout << "员工身份证号: " << endl;
cin >>ID;
cout << "员工工号: " << endl;
cin >>worknumber;
cout << "员工工资: " << endl;
cin >>salary;
}
void Cemployee::show_employee()
{
cout <<"姓名: " <<name<<'\t' << "年龄: " <<age<<'\t'<<
"性别:"<<sex<<'\t'<< "身份证号: "<<ID << '\t'<< "工号: "
<<worknumber<<'\t'<< "工资: " << salary<<'\b'<<endl;
}
float Cemployee::get_salary()
{
return salary;
}
string Cemployee::get_name()
{
return name;
}
string Cemployee::get_worknumber()
{
return worknumber;
}
公司类:
class Ccompany
{
private:
string comname;
string comrelevant;
float comexpend;
float comincome;
public:
int number;
Ccompany();//构造函数
~Ccompany(); //析构函数
Cemployee *emp[100];
void show_company();//显示公司信息
void plus_salary();//显示员工工资总和
void search_name();//按照姓名查询员工信息
void delete_name();//删除员工
void add_employee();//增加员工
};
公司类函数实现如下:
Ccompany::Ccompany()
{
cout<<"请输入公司的相关信息"<<endl;
cout<<"公司名:";
cin>>comname;
cout<<"公司信息:";
cin>>comrelevant;
cout<<"公司员工工资支出:";
cin>>comexpend;
cout<<"公司收入:";
cin>>comincome;
cout<<"公司员工个数:";
cin>>number;
} //构造函数初始化
void Ccompany::show_company()//显示公司信息
{
cout<<"******************"<<endl;
cout<<"公司信息"<<endl;
cout<<"公司名字:"<<comname<<'\t'<<"公司相关信息:"<<comrelevant<<'\t'<<
"公司员工工资总支出:"<<comexpend<<'\t'<<"公司收入:"<<comincome<<'\t'<<
"公司员工个数:"<<number<<endl;
cout<<"员工信息"<<endl;
}
void Ccompany::plus_salary()//显示员工工资总和
{
float sum;
for(int i=0;i<number;i++)
sum+=emp[i]->get_salary() ;
cout<<"员工工资总和:"<<sum<<endl;
}
void Ccompany::search_name()//按照姓名查询员工信息
{
string n;
int i,count=0;
cout<<"请输入员工姓名:";
cin>>n;
for(i=0;i<number;i++)
{
if(emp[i]!=NULL)
{
if(emp[i]->get_name() ==n)
{
emp[i]->show_employee();
count++;
}
}
else
cout<<"对不起,查无此人!"<<endl;
}
if(count==0)
cout<<"对不起,查无此人!"<<endl;
}
void Ccompany::delete_name()//删除员工
{
string n;
cin>>n;
for(int i=0;i<number;i++)
{
if(emp[i]->get_name() ==n)
emp[i]=NULL;
}
}
void Ccompany::add_employee()//增加员工
{
int n,i;
cout<<"请输入增加员工的数量:";
cin>>n;
for(i=number;i<number+n;i++)
emp[i]=new Cemployee;
number+=n;
}
Ccompany::~Ccompany()
{
}
main()函数:
int main()
{
Ccompany company;
cout<<endl;
for(int i=0;i<company.number;i++)
company.emp[i]=new Cemployee;
company.show_company();
for(int i=0;i<company.number;i++)
company.emp[i]->show_employee();
cout<<endl;
cout<<"******************"<<endl;
while(1)
{
cout <<"---------------------"<< endl;
cout << "1.按照姓名查询员工信息" << endl;
cout << "2.按姓名删除员工信息" << endl;
cout << "3.所有员工的工资总额"<<endl;
cout << "4.增加新员工"<<endl;
cout << "0.退出" << endl;
cout <<"---------------------"<< endl;
cout << "请输入你想选择的功能:" << endl;
int num;
cin >> num;
cout<<"--------------------"<<endl;
if(num==0)
break;
switch(num)
{
case 1:
company.search_name();break;
case 2:
company.delete_name();break;
case 3:
company.plus_salary();break;
case 4:
company.add_employee();break;
default:
cout<<"请输入正确的操作指令"<<endl;
cout<<"**************************"<<endl;
}
}
return 0;
}
新手发博,纯原创