设计要求:
某小型公司,主要有四类人员:经理、技术人员、销售经理和推销员。现在,需要存储这些人员的姓名、编号、级别、当月薪水。计算月薪总额并显示全部信息。人员编号基数为 1000,每输入一个人员信息编号顺序加 1。程序要有对所有人员提升级别的功能。 本例中为简单起见, 所有人员的初始级别均为 1级。然后进行升级,经理升为 4 级,技术人员和销售经理升为 3 级,推销员仍为 1 级。 月薪计算办法是:
- 经理拿固定月薪 8000 元;
- 技术人员按每小时 100 元领取月薪;
- 推销员的月薪按该推销员当月销售额的 4%提成;
- 销售经理既拿固定月薪也领取销售提成,固定月薪为 5000 元,销售提成为所管辖部门当月销售总额的 5%。
设计思路
根据要求,先设置一个基类employee,在这之上派生出经理类,技术人员类,销售员类,销售经理类。而在基类中,除了定义构造函数和析构函数以外,还应统一定义对各类人员信息都应有的譬如定义姓名,编号,设置等级等操作,这样可以规范各派生类的基本行为。但是由于各类人员的月薪计算方法不同,各类人员信息的显示内容也不同,不能统一在基类中定义。因此,在employee类中用纯虚函数的方式定义了计算月薪函数Salary()和显示信息函数Print(),然后在派生类中再根据各自的同名函数实现具体的功能。
staff.h
#ifndef __STAFF_H__
#define __STAFF_H__
#include <iostream>
#include <cstring>
using namespace std;
class employee
{
protected:
char *name;
int id;
int rank;
float salary;
static int num;
public:
employee();
~employee();
void SetName();
void Setrank(int);
virtual void Salary();
virtual void Print();
};
class manager : public employee
{
protected:
int m_salary;
public:
manager();
void Salary();
void Print();
};
class technicist : public employee
{
protected:
int h_salary;
int hours;
public:
technicist();
void Salary();
void Print();
};
class salesman : public employee
{
protected:
float rate;
float sale;
public:
salesman();
void Salary();
void Print();
};
class salesmanager : public employee
{
protected:
int m_salary;
float rate;
float sale;
public:
salesmanager();
void Salary();
void Print();
};
#endif
staff.cpp
#include "staff.h"
int employee::num = 1000;
employee::employee()
{
id = ++num;
rank = 1;
}
employee::~employee()
{
delete name;
}
void employee::SetName()
{
char m_name[20];
cout << "请输入职工姓名:" << endl;
cin >> m_name;
name = new char[strlen(m_name)];
strcpy(name, m_name);
}
void employee::Setrank(int r)
{
rank += r;
}
void employee::Salary() {}
void employee::Print() {}
manager::manager()
{
m_salary = 8000;
}
void manager::Salary()
{
salary = m_salary;
}
void manager::Print()
{
cout << "职位:经理,姓名:" << name << ",编号:" << id << endl;
cout << "级别:" << rank << ",当月薪水:" << salary << endl
<< endl;
}
technicist::technicist()
{
h_salary = 100;
}
void technicist ::Salary()
{
cout << "输入技术人员" << name << "当月工作总小时数:" << endl;
cin >> hours;
salary = hours * h_salary;
}
void technicist::Print()
{
cout << "职位:技术人员,姓名:" << name << ",编号:" << id << endl;
cout << "级别:" << rank << ",当月薪水:" << salary << endl
<< endl;
}
salesman::salesman()
{
rate = 0.04f;
}
void salesman::Salary()
{
cout << "输入销售员" << name << "当月销售总额:" << endl;
cin >> sale;
salary = sale * rate;
}
void salesman::Print()
{
cout << "职位:销售员,姓名:" << name << ",编号:" << id << endl;
cout << "级别:" << rank << ",当月薪水:" << salary << endl
<< endl;
}
salesmanager::salesmanager()
{
m_salary = 5000;
rate = 0.05f;
}
void salesmanager::Salary()
{
cout << "输入销售经理" << name << "所管辖部门当月销售总额:" << endl;
cin >> sale;
salary = sale * rate + m_salary;
}
void salesmanager::Print()
{
cout << "职位:销售经理,姓名:" << name << ",编号:" << id << endl;
cout << "级别:" << rank << ",当月薪水:" << salary << endl
<< endl;
}
main.cpp
#include "staff.h"
int main()
{
manager m1;
technicist t1;
salesman s1;
salesmanager s2;
m1.SetName();
m1.Setrank(3);
m1.Salary();
m1.Print();
t1.SetName();
t1.Setrank(2);
t1.Salary();
t1.Print();
s1.SetName();
s1.Setrank(0);
s1.Salary();
s1.Print();
s2.SetName();
s2.Setrank(2);
s2.Salary();
s2.Print();
return 0;
}
改进空间
由于销售经理(salesmanager)既是经理又是销售人员,兼具两类人员的特点,因此同时继承manager和salesman两个类的方法会更加简便。注意要将employee类设计为虚基类来避免二义性。但我的虚拟机在这样改的时候老是会报基类不明确的错误,就直接使用继承普通员工类的笨办法了。
|