用C++編寫一個簡單的員工工資管理系統~

Y0uX1Nr3N發表於2018-03-13

用基礎的C++語言編寫一個簡單的員工工資管理系統
系統功能簡單 但可以進行更多功能的改進

程式碼較為簡單 很多地方僅是為了方便除錯
仍有許多不足的地方需要改進

程式碼如下
#include<iostream>
#include<string>
using namespace std;
#define MAX 10
//員工工資管理

class Employee
{
public:
	Employee(string name,int year):m_strName(name),m_nYear(year){} //建構函式
	virtual ~Employee(){}
	string GetName()const{return m_strName;}   //獲取姓名
	int GetYear()const{return m_nYear;}       //獲取工齡
	virtual int GetSalary() = 0;              //獲取工資
private:
	string m_strName;
	int m_nYear;
};

class Worker:public Employee
{
public:
	Worker(string name,int year):Employee(name,year){}
	~Worker(){}
	virtual int GetSalary(){return 12000;} //此處未給出具體工資演算法,可進行修改  
};

class Manager:public Employee
{
public:
	Manager(string name,int year):Employee(name,year){}
	~Manager(){}
	virtual int GetSalary(){return 13000;}  //此處未給出具體工資演算法,可進行修改
};

class EmployeeSalarySystem
{
public:
	EmployeeSalarySystem(){m_nCount = 0;}
	~EmployeeSalarySystem()
	{
		while(--m_nCount >= 0)
		{
			delete employee[m_nCount];
			employee[m_nCount] = NULL;
		}
	}
	void InPutEmployee()
	{
		string name;
		int year;    //工齡
		int flag;    //職位標誌位,Worker:0;Manager:1
		cout<<"請輸入員工姓名與工齡"<<endl;
		cin>>name>>year;
		cout<<"請輸入員工職位,Worker:0;Manager:1;"<<endl;
		cin>>flag;
		if(flag == 0)
		{
			employee[m_nCount] = new Worker(name,year);
			++m_nCount;
		}
		if(flag == 1)
		{
			employee[m_nCount] = new Manager(name,year);
			++m_nCount;
		}
	}
	void ShowEmployee()
	{
		for(int i=0;i<m_nCount;i++)
			cout<<"姓名:"<<employee[i]->GetName()<<"  工齡:"<<employee[i]->GetYear()<<"  工資:"<<employee[i]->GetSalary()<<endl;
	}
private:
	Employee *employee[MAX];   //上方對MAX進行了巨集定義
	int m_nCount;
};

void main()//主函式僅為進行除錯,具體還可進行改進
{
	EmployeeSalarySystem ess;
	ess.InPutEmployee();
	ess.InPutEmployee();
	ess.InPutEmployee();
	ess.ShowEmployee();  
}

相關文章