题目介绍

  • 企业有四类人,分别是技术人员、经理、销售、销售经理
  • 编号:公司每添加一个人,编号加一
  • 薪资计算方法:
  1. 技术人员:按小时计算,分等级,等级默认为1,每小时100;然后每提高一个等级,时薪+100,等级时薪上限分别:4和400
  2. 经理:固定工资:8000,默认等级1,然后每提高一个等级,工资加200,上限12000
  3. 销售:当月销售额的百分比,默认等级1,百分比为0.4,然后每提高一个等级,百分比加0.01,上限0.06
  4. 销售经理:固定工资:5000+当月销售额的百分比;默认等级1,百分比为0.4,然后每提高一个等级,百分比加0.01,上限0.06
  • 整体的架构设计图:

企业员工薪资管理-多态案例



代码

Employee

Employee.h

#pragma once
#include <iostream>
#include <string>

using namespace std;


class Employee
{
public:
	Employee();
	virtual ~Employee();

	virtual void Init() = 0 ;
	virtual void UpLevel(int level) = 0;
	virtual void GetPay()=0;
			void Display();

	
protected:
	string m_name;
	int m_ID;
	int m_level;
	double m_salary;


	static int id_start;
};

Employee.cpp

#include "stdafx.h"
#include "Employee.h"

int Employee::id_start=1000;

Employee::Employee()
{
	cout << "Employee()" << endl;
	m_ID = id_start++;
	m_salary = 0;
	m_level = 1;
}



Employee::~Employee()
{
	cout << "~Employee()" << endl;
}




 void Employee::Display()
{
	 cout << "name :" << m_name << endl;
	 cout << " ID:" << m_ID << endl;
	 cout << "level :" <<  m_level << endl;
	 cout << "salary :" << m_salary << endl;
}



Tech

Tech.h

#pragma once
#include "Employee.h"
class Tech : public Employee
{
public:
	Tech();
	~Tech();

	virtual void Init();
	virtual void UpLevel(int level) ;
	virtual void GetPay() ;
private:
	int m_work_hours;
	double m_perhoursmoney;
};

Tech.cpp

#include "stdafx.h"
#include "Tech.h"


Tech::Tech()
{
	cout << "Tech()" << endl;
	m_perhoursmoney = 100;
}


Tech::~Tech()
{
	cout << "~Tech()" << endl;
}


void Tech::Init()
{
	cout << "请输入员工姓名" << endl;
	cin >> m_name;
}


void Tech::UpLevel(int level)
{
	if (level >= 0)
	{
		m_level = level;
		if (m_level == 1)
			m_perhoursmoney = 100;
		if (m_level == 2)
			m_perhoursmoney = 200;
		if (m_level == 3)
			m_perhoursmoney = 300;
		if (m_level >= 4)
			m_perhoursmoney = 400;
	}
		
}




void Tech::GetPay()
{
	cout << "请输入" << m_name << " 工作时长: " << endl;
	cin >> m_work_hours;
	m_salary = m_work_hours * m_perhoursmoney;

}



Manager

Manager.h

#pragma once
#include "Employee.h"
class Manager : virtual public Employee
{
public:
	Manager();
	virtual ~Manager();

	virtual void UpLevel(int level) ;
	virtual void GetPay() ;
	virtual void Init();
protected:
	int m_fix_salary;
};


Manager.cpp

#include "stdafx.h"
#include "Manager.h"


Manager::Manager()
{
	cout << "Manager()" << endl;

}

Manager::~Manager()
{
	cout << "~Manager()" << endl;
}


void Manager::Init()
{
	cout << "请输入经理姓名: " << endl;
	cin >> m_name;
	m_fix_salary = 8000;
}



void Manager::UpLevel(int level)
{
	if (level >= 0)
	{
		m_level = level;
		if (m_level == 1)
		{
			m_salary = 8000;
		}
		if (m_level == 2)
		{
			m_salary = 10000;
		}
		if (m_level == 3)
		{
			m_salary = 12000;
		}
	}
}



void Manager::GetPay()
{
	m_salary = m_fix_salary;
}


SaleMan

SaleMan.h

#pragma once
#include "Employee.h"
class SaleMan : virtual public Employee
{
public:
	SaleMan();
	virtual ~SaleMan();

	virtual void Init();
	virtual void UpLevel(int level) ;
	virtual void GetPay() ;


protected:
	int m_curent_amout;
	double  m_rate;
};


SaleMan.cpp

#include "stdafx.h"
#include "SaleMan.h"


SaleMan::SaleMan()
{
	cout << "SaleMan()" << endl;
	m_rate = 0.04;
}


SaleMan::~SaleMan()
{
	cout << "~SaleMan() " << endl;
}

void SaleMan::Init()
{
	cout << "请输入销售人员的姓名: " << endl;
	cin >> m_name;
}

 void SaleMan::UpLevel(int level)
{
	 if (level >= 0)
	 {
		 m_level = level;
		 if (m_level == 1)
			 m_rate = 0.04;
		 if (m_level == 2)
			 m_rate = 0.05;
		 if (m_level == 3)
			 m_rate = 0.06;
	 }
}


 void SaleMan::GetPay()
{
	 cout << "请输入销售人员的销售额:" << endl;
	 cin >> m_curent_amout;
	 m_salary = m_curent_amout * m_rate;
}


SalesManager

SalesManager.h

#pragma once
#include "Manager.h"
#include "SaleMan.h"
class SalesManager :public Manager,SaleMan
{
public:
	SalesManager();
	~SalesManager();

	virtual void Init();
	virtual void UpLevel(int level);
	virtual void GetPay();
};

SalesManager.cpp

#include "stdafx.h"
#include "SalesManager.h"


SalesManager::SalesManager()
{
	cout << "SalesManager() " << endl;

	m_fix_salary = 5000;
	m_rate = 0.05;
}


SalesManager::~SalesManager()
{
	cout << "~SalesManager()" << endl;
}


void SalesManager::Init()
{
	cout << "请输入销售经理姓名 " << endl;
	cin >> m_name;
}


void SalesManager::UpLevel(int level)
{
	if (level >= 0)
	{
		m_level = level;
		if (m_level == 1)
			m_rate = 0.04;
		if (m_level == 2)
			m_rate = 0.05;
		if (m_level == 3)
			m_rate = 0.06;
	}
}


void SalesManager::GetPay()
{
	cout << "请输入销售经理的销售额 :" << endl;
	cin >> m_curent_amout;
	m_salary = m_curent_amout * m_rate + m_fix_salary;
}



测试代码

User.cpp

#include "stdafx.h"
#include "Employee.h"
#include "Tech.h"
#include "Manager.h"
#include "SaleMan.h"
#include "SalesManager.h"

int main()
{
#if 0
	Employee* p1 = new Tech;
	p1->Init();
	p1->UpLevel(3);
	p1->GetPay();
	p1->Display();

	delete p1;

	Employee* p2 = new Manager;
	p2->Init();
	p2->UpLevel(3);
	p2->GetPay();
	p2->Display();
	delete p2;


	Employee* p3 = new SaleMan;
	p3->Init();
	p3->UpLevel(2);
	p3->GetPay();
	p3->Display();
	delete p3;


	Employee* p4 = new SalesManager;
	p4->Init();
	p4->UpLevel(2);
	p4->GetPay();
	p4->Display();
	delete p4;
#endif

	Employee* p[] = { new Tech, new SaleMan, new Manager, new SalesManager };
	for (int i = 0; i < sizeof(p)/sizeof(p[0]);  i++)
	{
		p[i]->Init();
		p[i]->UpLevel(2);
		p[i]->GetPay();
		p[i]->Display();
	}

	for (int i = 0; i < sizeof(p) / sizeof(p[0]); i++)
	{
		delete p[i];
	}

    return 0;
}


总结:

  1. 遇到多态中的继承现象,将析构函数写成虚析构函数,保证每次析构都能调用子类和父类的析构
  2. 当继承出现菱形的时候,子类继承父类的时候得是虚继承,保证父类的变量在孙子中只有一份
  3. 构造函数中不要写业务,只能写变量的初始化,必须初始化时候写的业务如交互输入信息等,应该另外提供Init方法

相关文章: