公司管理项目模拟
要求:
一个小型公司的人员信息管理系统
某小型公司,主要有四类人员:经理、技术人员、销售经理和推销员。
现在,需要存储这些人员的姓名、编号、级别、当月薪水.计算月薪总额并显示全部信息。
人员编号基数为 1000,每输入一个人员信息编号顺序加 1。
程序要有对所有人员提升级别的功能。本例中为简单起见,所有人员的初始级别均为 1
级。然后进行升级,经理升为 4 级,技术人员和销售经理升为 3 级,推销员仍为 1 级。
定义虚函数是为了允许用基类的指针来调用子类的这个函数。
定义纯虚函数是为了实现一个接口,起到一个规范的作用,规范继承这个类的程序员必须实现这个函数。
月薪计算办法是: 经理拿固定月薪 8000 元;技术人员按每小时 100 元领取月薪; 推
销员的月薪按该推销员当月销售额的 4%提成;销售经理既拿固定月薪也领取销售提成,
固定月薪为 5000 元,销售提成为所管辖部门当月销售总额的 5%。
分析:
分析各个职员的关系,可以简单画出下列的UML图。首先抽象化出基类employee,然后有技术人员,经理,销售人员,分别继承员工类。销售经理继承销售人员和经理。1.1.在第二层的继承中会发生二义性的问题,因为在经理和销售人员都有继承来自员工的姓名,年龄等变量属性,所以在此要选择虚继承,解决二义性的问题
2.2由于每个员工都要计算工资,而每个员工计算工资方式都是不一样的,所以这是一种多态行为,在父本中定义纯虚函数,从而在子类中去实现各个功能,从而实现了多态
具体实现
1.创建父类employee
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include<string>
#include<iostream>
using namespace std;
class Employee
{
public:
Employee();
~Employee();
virtual void display()=0;
virtual void getSalary()=0;
virtual void getLeavel(int l=1)=0;
virtual void init()=0;
protected:
string name;
int age;
char sex;
float salary;
int leavel;
static int startNum;
int num;
};
#endif // EMPLOYEE_H
//.cpp文件 类成员的实现
#include "employee.h"
int Employee::startNum=1000;
Employee::Employee()
{
}
Employee::~Employee()
{
}
2.创建各个子类technician
#ifndef TECHNICIAN_H
#define TECHNICIAN_H
#include"employee.h"
class Technician:public Employee
{
public:
Technician();
~Technician();
virtual void display();
virtual void getSalary();
virtual void getLeavel(int l=1);
virtual void init();
void setParaments();
private:
float hour;
float oneHourSalary;
};
#endif // TECHNICIAN_H
//.cpp 文件 类的实现
#include "technician.h"
#include<iostream>
using namespace std;
Technician::Technician()
{
}
Technician::~Technician()
{
}
void Technician::display()
{
cout<<"name:"<<this->name<<endl;
cout<<"sex:"<<this->sex<<endl;
cout<<"age:"<<this->age<<endl;
cout<<"Number:"<<this->num<<endl;
cout<<"salary:"<<this->salary<<endl;
cout<<"leavel"<<this->leavel<<endl;
}
void Technician::getSalary()
{
setParaments();
this->salary = this->hour*this->oneHourSalary;
}
void Technician::getLeavel(int l)
{
this->leavel = l;
}
void Technician::init()
{
cout<<"please input your name:";
cin>>this->name;
cout<<"please input your age:";
cin>>this->age;
cout<<"please input your sex:";
cin>>this->sex;
cout<<"please input your working time:";
cin>>this->hour;
num = this->startNum++;
}
void Technician::setParaments()
{
this->oneHourSalary =100;
}
3.salesMan
#ifndef SALESMAN_H
#define SALESMAN_H
#include"employee.h"
class SalesMan:public virtual Employee
{
public:
SalesMan();
~SalesMan();
virtual void display();
virtual void getSalary();
virtual void getLeavel(int l=1);
virtual void init();
protected:
float salesCount;
float percent;
};
#endif // SALESMAN_H
//.cpp 文件 类的实现
#include "salesman.h"
#include<iostream>
using namespace std;
SalesMan::SalesMan()
{
}
SalesMan::~SalesMan()
{
}
void SalesMan::display()
{
cout<<"name:"<<this->name<<endl;
cout<<"sex:"<<this->sex<<endl;
cout<<"age:"<<this->age<<endl;
cout<<"Number:"<<this->num<<endl;
cout<<"salary:"<<this->salary<<endl;
cout<<"leavel:"<<this->leavel<<endl;
}
void SalesMan::getSalary()
{
percent =0.03;
this->salary = this->salesCount*percent;
}
void SalesMan::getLeavel(int l)
{
this->leavel = l;
}
void SalesMan::init(){
cout<<"please input your name:";
cin>>this->name;
cout<<"please input your age:";
cin>>this->age;
cout<<"please input your sex:";
cin>>this->sex;
cout<<"please input your sale account:";
cin>>this->salesCount;
this->num = this->startNum++;
}
4.manager
#define MANAGER_H
#include"employee.h"
class Manager:public virtual Employee
{
public:
Manager();
~Manager();
virtual void init();
virtual void display();
virtual void getSalary();
virtual void getLeavel(int l=1);
protected:
float fixSalary;
};
#endif // MANAGER_H
//.cpp
#include "manager.h"
#include<iostream>
using namespace std;
Manager::Manager()
{
}
Manager::~Manager()
{
}
void Manager::init()
{
cout<<"please input your name:";
cin>>this->name;
cout<<"please input your age:";
cin>>this->age;
cout<<"please input your sex:";
cin>>this->sex;
this->num=this->startNum++;
}
void Manager::display()
{
cout<<"name:"<<this->name<<endl;
cout<<"sex:"<<this->sex<<endl;
cout<<"age:"<<this->age<<endl;
cout<<"Number:"<<this->num<<endl;
cout<<"salary:"<<this->salary<<endl;
cout<<"leavel"<<this->leavel<<endl;
}
void Manager::getSalary()
{
this->fixSalary=8000;
this->salary = this->fixSalary;
}
void Manager::getLeavel(int l)
{
this->leavel=l;
}
5.salesManager
#ifndef SALESMANAGER_H
#define SALESMANAGER_H
#include"salesman.h"
#include"manager.h"
class SalesManager:public Manager,public SalesMan
{
public:
SalesManager();
~SalesManager();
virtual void init();
virtual void display();
virtual void getSalary();
virtual void getLeavel(int l=1);
};
#endif // SALESMANAGER_H
//.cpp
#include "salesmanager.h"
#include<iostream>
using namespace std;
SalesManager::SalesManager()
{
}
SalesManager::~SalesManager()
{
}
void SalesManager::init()
{
cout<<"please input your name:";
cin>>this->name;
cout<<"please input your age:";
cin>>this->age;
cout<<"please input your sex:";
cin>>this->sex;
cout<<"please input your sales account:";
cin>>this->salesCount;
this->num=this->startNum;
}
void SalesManager::display()
{
cout<<"name:"<<this->name<<endl;
cout<<"sex:"<<this->sex<<endl;
cout<<"age:"<<this->age<<endl;
cout<<"Number:"<<this->num<<endl;
cout<<"salary:"<<this->salary<<endl;
cout<<"leavel:"<<this->leavel<<endl;
}
void SalesManager::getSalary()
{
this->fixSalary=5000;
this->percent =0.02;
salary = this->salesCount*this->percent+this->fixSalary;
}
void SalesManager::getLeavel(int l)
{
this->leavel = l;
}
6.主函数
#include <iostream>
#include"manager.h"
#include"employee.h"
#include"technician.h"
#include"salesman.h"
#include"salesmanager.h"
using namespace std;
int main()
{
Employee *emp[]= {new Technician,new Manager,new SalesMan,new SalesManager};
for(int i=0;i<(sizeof(emp)/sizeof(emp[1]));i++)
{
emp[i]->init();
emp[i]->getSalary();
emp[i]->getLeavel(2);
emp[i]->display();
}
return 0;
}
小结
1.虚函数主要实现面向对象三大特点中的多态,而虚继承主要解决多继承中二义性问题或者叫做三角问题,钻石问题。
2.此blog仅仅作为学习记录内容,希望能对你有一定的帮助。该篇主要是C++的入门。
3.如有任何问题,请多多指正。