/* Employee.h文件 */
# include <string>
using namespace std;
class Employee
{
protected:
string m_name;
int m_time;
public:
Employee(string name, int time) //构造函数
: m_name(name), m_time(time)
{
}
string getName()
{
return m_name;
}
virtual int getSalary() = 0; //纯虚数,强制子类重新定义虚函数
};
class Manager: public Employee
{
public:
Manager(string name, int time)
: Employee(name, time) //使用基类的构造函数,完成对属性的初始化工作
{
}
virtual int getSalary()
{
return 10000 + m_time*5000;
}
};
class Stuff: public Employee
{
public:
Stuff(string name, int time)
: Employee(name, time) //使用基类的构造函数,完成对属性的初始化工作
{
}
virtual int getSalary()
{
return 2000 + m_time*200;
}
};
==========================================================================================================================================================
/* SalarySys.h文件 */
# include "Employee.h"
# include <iostream>
const int MAX = 1000; //最大员工数定义为100
class SalarySys
{
private:
int m_count; //员工人数
Employee* m_employee[MAX]; //每个员工对象指针存入数组中
public:
SalarySys() //构造函数
{
m_count = 0;
}
~SalarySys() //析构函数
{
for(int i=0; i<m_count; ++i)
{
Employee* tmp = m_employee[i];
delete tmp;
m_employee[i] = NULL;
}
}
void input() //输入员工信息存入对象,对象指针放入数组中
{
while(m_count < MAX)
{
cin.clear();
string name = " ";
int time = -1;
bool isManager = false;
Employee* temp = NULL;
cin>>name;
if(name == "end")
break;
cin>>time>>isManager;
if(isManager)
{
temp = new Manager(name, time);
}
else
{
temp = new Stuff(name, time);
}
m_employee[m_count++] = temp;
}
}
double getAve()
{
int sum = 0;
for(int i=0; i<m_count; ++i)
{
sum += m_employee[i]->getSalary();
}
return (double)sum / m_count;
}
void output()
{
cout<<"当前员工总数:"<<m_count<<endl;
cout<<"平均工资是:"<<getAve()<<endl;
cout<<"员工具体信息如下:"<<endl;
for(int i=0; i<m_count; ++i)
{
Employee* tmp = m_employee[i];
cout<<tmp->getName()<<"\t"<<tmp->getSalary()<<endl;
}
}
};
==========================================================================================================================================================
/* main.cpp文件 */
# include "SalarySys.h"
int main()
{
SalarySys sys;
sys.input();
sys.output();
return 0;
}
实战面向对象_工资管理系统
最新推荐文章于 2020-05-07 00:56:33 发布