需求
某小型公司,主要有四类人员:经理、技术人员、推销员和销售经理。现在,需要
存储这些人员的姓名、编号、级别、当月薪水.计算月薪总额并显示全部信息。
人员编号基数为 1000,每输入一个人员信息编号顺序加 1。
程序要有对所有人员提升级别的功能。本例中为简单起见,所有人员的初始级别均为 1 级。
然后进行升级,经理升为 4 级,技术人员和销售经理升为 3 级,推销员仍为 1级。
月薪计算办法是:
经理拿固定月薪 8000 元;
技术人员按每小时 100 元领取月薪;
推销员的月薪按该推销员当月销售额的 4%提成;
销售经理既拿固定月薪也领取销售提成,固定月薪为 5000 元,销售提成为所管辖部门当月销售总额的 5%。
employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
using namespace std;
class Employee
{
public:
Employee();
virtual ~Employee();
virtual void init() = 0;
virtual void promote() = 0;
virtual void calcSalary() = 0;
virtual void disInfor() = 0;
protected:
string _name;
int _num;
int _level;
float _salary;
static int _startNumber;
};
#endif // EMPLOYEE_H
manager.h
#ifndef MANAGER_H
#define MANAGER_H
#include <employee.h>
class Manager:virtual public Employee
{
public:
Manager();
virtual ~Manager();
virtual void init();
virtual void promote() ;
virtual void calcSalary();
virtual void disInfor() ;
void addSalary(float money);
protected:
int _fixedSalary;
};
#endif // MANAGER_H
salemanager.h
#ifndef SALEMANAGER_H
#define SALEMANAGER_H
#include <manager.h>
#include <salesman.h>
class Salemanager:public Manager,public Salesman
{
public:
Salemanager();
virtual ~Salemanager();
virtual void init();
virtual void promote() ;
virtual void calcSalary() ;
virtual void disInfor() ;
protected:
};
#endif // SALEMANAGER_H
salesman.h
#ifndef SALESMAN_H
#define SALESMAN_H
#include <employee.h>
class Salesman:virtual public Employee
{
public:
Salesman();
virtual ~Salesman();
virtual void init();
virtual void promote();
virtual void calcSalary();
virtual void disInfor();
protected:
float _saleAmount;
float _rate;
};
#endif // SALESMAN_H
technician.h
#ifndef TECHNICIAN_H
#define TECHNICIAN_H
#include <employee.h>
class Technician:public Employee
{
public:
Technician();
virtual ~Technician();
virtual void init();
virtual void promote();
virtual void calcSalary();
virtual void disInfor();
protected:
int _amountHour;
int _moneyPerHour;
};
#endif // TECHNICIAN_H
emoloyee.cpp
#include "employee.h"
int Employee::_startNumber = 0;
Employee::Employee()
{
}
Employee:: ~Employee()
{
}
manager.cpp
#include "manager.h"
Manager::Manager()
{
}
Manager::~Manager()
{
}
void Manager::init()
{
_fixedSalary = 8000;
_num = _startNumber++;
_level = 1;
cout<<"请输入经理的姓名:";
cin>>_name;
_salary = 0;
}
void Manager::promote()
{
_level += 4;
}
void Manager::calcSalary()
{
_salary += _fixedSalary;
}
void Manager::disInfor()
{
cout<<"姓名 :"<<_name<<endl;
cout<<"工号 :"<<_num<<endl;
cout<<"级别 :"<<_level<<endl;
cout<<"本月的结算的薪水:"<<_salary<<endl;
cout<<"==============================="<<endl;
}
salemanager.cpp
#include "salemanager.h"
Salemanager::Salemanager()
{
}
Salemanager::~Salemanager()
{
}
void Salemanager::init()
{
_fixedSalary = 5000;
_num = _startNumber++;
_level = 1;
_rate= 0.05;
cout<<"请输入销售经理的姓名:";
cin>>_name;
cout<<"请输入本月销售额: ";
cin>>_saleAmount;
}
void Salemanager::promote()
{
_level += 3;
}
void Salemanager::calcSalary()
{
_salary = _fixedSalary + _saleAmount *_rate;
}
void Salemanager::disInfor()
{
cout<<"姓名 :"<<_name<<endl;
cout<<"工号 :"<<_num<<endl;
cout<<"级别 :"<<_level<<endl;
cout<<"本月固定的薪水:"<<_fixedSalary<<endl;
cout<<"每个的销售额度:"<<_saleAmount<<endl;
cout<<"本月的提成比率:"<<_rate<<endl;
cout<<"本月的结算薪水:"<<_salary<<endl;
cout<<"==============================="<<endl;
}
salesman.cpp
#include "salesman.h"
Salesman::Salesman()
{
}
Salesman:: ~Salesman()
{
}
void Salesman::init()
{
_rate= 0.04;
_num = _startNumber++;
_level = 1;
cout<<"请输销售员的姓名:";
cin>>_name;
cout<<"请输入本月销售额: ";
cin>>_saleAmount;
}
void Salesman::promote()
{
_level += 1;
}
void Salesman::calcSalary()
{
_salary = _saleAmount * _rate;
}
void Salesman::disInfor()
{
cout<<"姓名 :"<<_name<<endl;
cout<<"工号 :"<<_num<<endl;
cout<<"级别 :"<<_level<<endl;
cout<<"本月的销售额度:"<<_saleAmount<<endl;
cout<<"本月的提成比率:"<<_rate<<endl;
cout<<"本月结算的薪水:"<<_salary<<endl;
cout<<"==============================="<<endl;
}
technician.cpp
#include "technician.h"
Technician::Technician()
{
}
Technician::~Technician()
{
}
void Technician::init()
{
_num = _startNumber++;
_level = 1;
_moneyPerHour = 100;
cout<<"请输入技术人员的姓名:";
cin>>_name;
cout<<"请输入本月工作的小时数:";
cin>>_amountHour;
}
void Technician::promote()
{
_level += 3;
}
void Technician::calcSalary()
{
_salary = _amountHour * _moneyPerHour;
}
void Technician::disInfor()
{
cout<<"姓名 :"<<_name<<endl;
cout<<"工号 :"<<_num<<endl;
cout<<"级别 :"<<_level<<endl;
cout<<"本月工作的小时数:"<<_amountHour<<endl;
cout<<"每个工作时的薪水:"<<_moneyPerHour<<endl;
cout<<"本月的结算的薪水:"<<_salary<<endl;
cout<<"==============================="<<endl;
}
main.cpp
#include <iostream>
using namespace std;
#include "employee.h"
#include "technician.h"
#include "manager.h"
#include "salesman.h"
#include "salemanager.h"
int main()
{/*
Technician t;
Salesman s;
Manager m;
Salemanager sm;
Employee *p1 = new Technician;
Employee *p2 = new Salesman;
Employee *p3 = new Manager;
Employee *p4 = new Salemanager;
*/
//多态机制的利用
Employee *empArr[] = {new Technician,new Salesman,new Manager,new Salemanager};
for(int i = 0;i <4;i++)
{
empArr[i]->init();
empArr[i]->promote();
empArr[i]->calcSalary();
empArr[i]->disInfor();
}
//多态发生的前提下,使用运行时类型信息区分出经理,给经理加工资.
Employee *empArr[] = {new Manager,new Technician,new Salesman,new Salesman,new Manager,new Salemanager};
for(int i = 0;i<sizeof(empArr)/sizeof(*empArr);i++)
{
empArr[i]->init();
empArr[i]->promote();
empArr[i]->calcSalary();
empArr[i]->disInfor();
if(typeid(**(empArr+i))== typeid(Manager))
{
cout<<"他提出要涨工资"<<endl;
if(pm = dynamic_cast<Manager*>(emp))
{
pm->addSalary(2000);
}
}
empArr[i]->disInfor();
}
/*
* //多态发生的前提下,使用运行时类型信息区分出经理,给经理加工资.
Employee * empArr[] = {new Manager, new Technician,
new SalesMan,new SalesMan ,new SaleManager};
Employee *emp;
Manager *pm;
for(int i=0; i<sizeof(empArr)/sizeof(*empArr);i++)
{
emp = empArr[i];
emp->init();
if(typeid(*emp) == typeid(Manager))
{
cout<<"他提出要涨工资"<<endl;
if(pm = dynamic_cast<Manager*>(emp))
{
pm->addSalary(2000);
}
}
emp->promote();
emp->calcSalary();
emp->disInfor();
}
*/
return 0;
}