c++实现:小型公司的信息管理系统(关于多态)

前言:

介绍员工信息:一个小型公司的人员信息管理系统
某小型公司,主要有四类人员:经理、技术人员、销售经理和推销员。现在,需要存储这些人员的姓名、编号、级别、当前薪水。计算月薪总额并显示全部信息人员编号基数为1000 每输入一个人员信息编号顺序加1,程序员要有对所有人员提升别的功能。本例中为简单起见,所有人员的初始级别均为1级。然后进行升级,经理升为4级,技术人员和销售经理升为3级,推销员仍为1级
月薪计算方法是:经理拿固定月薪8000元;技术人员按每小时100元领取月薪;推销员的月薪按该推销员当前售额的4%提成;销售经理既拿固定月薪也领取销售提成,固定月薪为5000,销售提成为所管辖部门当前销售总额的5%

一、分析题意

1.进行画图分析人员关系

注意:技术人员、经理、销售人员的基类都是Employee,SalesManager继承两个类:Manager和SalesMan,这里Employee、Manager、SalesMan、SalesManager构成菱形,形成了虚继承,所以在Manager、SalesMan继承Employee的时候应加上virtual

2.类的设计

二、创建基类--Employee

1.头文件--Employee.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
class Employee
{
public:
	Employee();
	virtual ~Employee();
	//提供一个显示的初始化员工的函数
	virtual void init()=0;

	//提供员工薪资的计算方法
	virtual void getPay() = 0;

	//显示员工信息方法
	void displayStatus();

	//升级的方法
	virtual void upLevel(int level) = 0;
protected:
	string name;//姓名
	int id;//编号
	double salary;//薪资
	int level;//级别
	
	//员工的编号基础值
	static int starNum;
};
static int starNum;

2. .cpp文件--Employee.cpp

#include "Employee.h"
int Employee::starNum = 1000;
Employee::Employee()
{
	cout << "Employee()..." << endl;
	id=starNum++;
	level = 1;
	salary = 0.0;
}
Employee::~Employee()
{
	cout << "~Employee()...." << endl;
}
void Employee::displayStatus()//显示员工信息
{
	cout << "该员工的姓名为:"<<this->name << endl;
	cout << "该员工的薪资为:" << this->salary << endl;
	cout << "该员工的编号为:" << this->id << endl;
	cout << "该员工的级别为" << this->level << endl;
}

三、技术人员类--Tecnicion

1.头文件--Tecnicion.h

#pragma once
#include "Employee.h"
class Tecnicion :
    public Employee
{
public:
    Tecnicion();
    ~Tecnicion();
    virtual void init();
    virtual void getPay();
    virtual void upLevel(int level);
private:
    //一个月工作了多少小时
    int workhour;
    //每小时多少钱
    double perHourMoney;
};

2. .cpp文件--Tecnicion.cpp

#include "Tecnicion.h"
Tecnicion::Tecnicion()
{
	cout << "Tecnicion()...." << endl;
	
}
void Tecnicion::init()
{
	cout << "请输入员工的姓名:" << endl;
	cin >> name;
	perHourMoney = 100;//规定员工每小时赚100元
}
Tecnicion::~Tecnicion()
{
	cout << "~Tecnicion()..." << endl;
}
void Tecnicion::getPay()
{
	cout << "请输入该员工工作了多少小时:" << endl;
	cin >> workhour;
	this->salary = this->workhour * this->perHourMoney;
}
void Tecnicion::upLevel(int level)
{
	this->level += level;
}

三、经理类--Manger

1.头文件--Manager.h

#pragma once
#include "Employee.h"
class Manager :
    virtual public Employee
{
public:
    ~Manager();
    Manager();
    virtual void init();
    virtual void getPay();
    virtual void upLevel(int level);
protected:
    double fixSalary;
};

2.cpp文件--Manager.cpp

#include "Manager.h"
Manager::~Manager()
{
	cout << "~Manager()..." << endl;
}
Manager::Manager()
{
	cout << "Manager()..." << endl;
}
void Manager::init()
{
	cout << "请输入该经理的姓名:" << endl;
	cin >> this->name;
}
void Manager::getPay()
{
	this->salary = 8000;
}
void Manager::upLevel(int level)
{
	this->level += level;
	if (this->level == 1)
	{
		this->salary = 8000;
	}
	else if (this->level == 2)
	{
		this->salary = 10000;
	}
}

四、销售人员--SalesMan

1.头文件--SalesMan.h

#pragma once
#include "Employee.h"
class SalesMan :
    virtual public Employee
{
public:
    SalesMan();
    ~SalesMan();
    virtual void init();
    virtual void getPay();
    virtual void upLevel(int level);
protected:
    //提成的比率
    double salePercent;
    
    //当月销售额
    int saleAmount;
};

2.cpp文件--SalesMan.cpp

#include "SalesMan.h"
SalesMan::SalesMan()
{
	cout << "SalesMan()..." << endl;
	
}
void SalesMan::init()
{
	cout << "请输入销售员工的姓名:" << endl;
	cin >> this->name;
	this->salePercent = 0.04;
}
SalesMan::~SalesMan()
{
	cout << "~SalesMan()..." << endl;
}
void SalesMan::getPay()
{
	cout << "请输入当月销售额:" << endl;
	cin >> this->saleAmount;

	//计算当月的月薪
	this->salary = (this->saleAmount) * (this->salePercent);
}
void SalesMan::upLevel(int level)
{
	this->level += level;
	if (this->level == 1)
	{
		this->salePercent = 0.04;
	}
}

五、销售经理--SalesManager

1.头文件--SalesManager.h

#pragma once
#include"Manager.h"
#include"SalesMan.h"
class SalesManager:public Manager,public SalesMan
{
public:
	SalesManager();
	~SalesManager();
	virtual void init();
	virtual void getPay();
	virtual void upLevel(int level);
private:
};

2.cpp文件--SalesManager.cpp

#include "SalesManager.h"
SalesManager::SalesManager()
{
	cout << "SalesManager()..." << endl;
}
void SalesManager::init()
{
	cout << "请输入销售经理的姓名:" << endl;
	cin >> this->name;
	this->fixSalary = 5000;
	this->salePercent = 0.05;
}
SalesManager::~SalesManager()
{
	cout << "~SalesManager()..." << endl;
}
void SalesManager::getPay()
{
	cout << "请输入当月的销售额" << endl;
	cin >> this->saleAmount;
	this->salary = this->saleAmount * this->salePercent + this->fixSalary;
}
void SalesManager::upLevel(int level)
{
	this->level += level;

}

六、主函数--main.cpp

#include"Employee.h"
#include<iostream>
using namespace std;
#include<string>
#include"Tecnicion.h"
#include"Manager.h"
#include"SalesMan.h"
#include"SalesManager.h"
int main(void)
{
#if 0
	Employee* em1 = new Tecnicion;//科技人员

	em1->getPay();
	em1->upLevel(1);
	em1->displayStatus();
	delete em1;
#endif

#if 0
	Employee* em2 = new Manager;//普通经理
	em2->getPay();
	em2->upLevel(0);
	em2->displayStatus();
	delete em2;
#endif

#if 0
	Employee* em3 = new SalesMan;
	em3->upLevel(0);
	em3->getPay();
	em3->displayStatus();
	delete em3;
#endif 

#if 0
	Employee* em4 = new SalesManager;//这里会出现差错  原因:在每个类的构造函数中只能初始化对象 而不能整一些业务
	em4->init();
	em4->upLevel(1);
	em4->getPay();
	em4->displayStatus();
	delete em4;
#endif

	Employee* person_array[] = { new Tecnicion,new Manager,new SalesMan,new SalesManager };
	for (int i = 0; i < sizeof(person_array) / sizeof(person_array[0]); i++)
	{
		person_array[i]->init();
		person_array[i]->upLevel(0);
		person_array[i]->getPay();
		person_array[i]->displayStatus();
	}
	for (int i = 0; i < sizeof(person_array) / sizeof(person_array[0]); i++)
	{
		delete person_array[i];
	}
	return 0;
}

源文件:多态案例/多态案例 · 李晓蕊/project.c - 码云 - 开源中国 (gitee.com)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写不出bug的小李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值