/*File : salarySystem.h
*Auth : sjin
*Date : 2014-04-27
*Mail : 413977243@qq.com
*/
#ifndef __SALARYSYSTEM_H
#define __SALARYSYSTEM_H
#include <string>
#include <iostream>
#define MAX_COUNT 1000
using namespace std;
//员工类
class Emplyee{
public:
//构造函数
//根据员工的名字和入职时间构造员工对象
Emplyee(string strName , int nYears) : m_strName(strName),m_nYears(nYears){};
//获得得员工的工资。
virtual int GetSalary() const {return 0;};
//提供一个公共接口,使外界得到员工的名字
string GetName() const { return m_strName;};
protected:
int m_nYears; //入职时间
string m_strName; //姓名
};
/*经理类
*因为经理是员工中的一种,从员工类派生
*/
class Manager : public Emplyee {
public:
/*使用基类的高走函数,完成对属性的初始化工作*/
Manager(string strName,int nYears):Emplyee(strName,nYears) {};
/* 对基类提供的纯虚函数进行自定义*/
virtual int GetSalary() const { return 5000*m_nYears + 10000;};
};
class Worker: public Emplyee {
public:
/*使用基类的构造函数,完成对属性的初始化工作*/
Worker(string strName,int nYears):Emplyee(strName,nYears) {};
/* 对基类提供的纯虚函数进行自定义*/
virtual int GetSalary() const { return 200*m_nYears + 2000;};
};
class SalaryStstem {
public:
/*构造函数和析构函数*/
SalaryStstem(void);
~SalaryStstem(void);
public:
/*输入员工信息*/
void InputEmplyee(void);
/*现实员工信息*/
void DisplaySalary(void);
/*计算平均工资*/
double GetAverSalary(void);
private:
/*当前员工总数*/
int m_nCount;
/*用于保存所有员工对象*/
Emplyee *m_arrEmplyee[MAX_COUNT];
};
#endif/*File : salarySystem.cpp
*Auth : sjin
*Date : 2014-04-27
*Mail : 413977243@qq.com
*/
#include "salarySystem.h"
/*构造函数初始化*/
SalaryStstem::SalaryStstem(void)
{
m_nCount = 0;// 初始化员工总数为0
}
/*析构函数 清理资源,释放内存*/
SalaryStstem::~SalaryStstem(void)
{
int i = 0;
for(i = 0; i < m_nCount; i++){
Emplyee *pEmplyee = m_arrEmplyee[i];
delete pEmplyee;
m_arrEmplyee[i] = NULL;
}
}
void help()
{
//提示信息
cout << "*************************************************" << endl;
cout << "* 请输入员工信息 *\n" <<
"* 格式: 员工姓名 入职时间 是否为经理级别 *\n" <<
"* 例如: sjin 4 0 *\n" <<
"* 输入end 表示输入结束 *\n" <<
"* 输入help 显示提示信息 *" << endl;
cout << "*************************************************" << endl;
}
/*获取用户输入*/
void SalaryStstem::InputEmplyee(void)
{
string strName = "";
int nYears = 0;
bool bManager = false;
int nIndex = 0;
// 提示信息
help();
/*开始循环接收用户输入的用户数据*/
while(nIndex < MAX_COUNT){
//清空输入流
cin.clear();
//读取用户数据
cin >> strName ;
//cout << strName << nYears << bManager<<endl;
if ("end" == strName){
break;
}
if("help" == strName){
help();
continue;
}
cin >> nYears >> bManager;
Emplyee *pEmplyee = NULL;
if(bManager){
pEmplyee = new Manager(strName,nYears);
} else {
pEmplyee = new Worker(strName,nYears);
}
m_arrEmplyee[nIndex] = pEmplyee;
++nIndex;
}
//保存员工数量总数
m_nCount = nIndex;
}
/*输出员工工资信息*/
void SalaryStstem::DisplaySalary(void)
{
cout << "工资管理系统" << endl;
cout << "当前员工总数:" << m_nCount <<
"\n平均工资" << GetAverSalary() << endl;
cout << "员工具体工资信息如下:" << endl;
for ( int i = 0; i < m_nCount; i++){
cout << "姓名:" << m_arrEmplyee[i]->GetName() << "\t 工资" <<
m_arrEmplyee[i]->GetSalary() << endl;
}
}
/*输入员工平均工资*/
double SalaryStstem::GetAverSalary()
{
int nTotal = 0;
for ( int i = 0; i < m_nCount; i++){
nTotal += m_arrEmplyee[i]->GetSalary();
}
return (double)nTotal/(m_nCount);
}/*File : main.cpp
*Auth : sjin
*Date : 2014-04-27
*Mail : 413977243@qq.com
*/
#include "salarySystem.h"
int main()
{
//创建SalaryStstem 对象
SalaryStstem nSalarySys;
//获得用户输入
nSalarySys.InputEmplyee();
cout << "*********员工的工资信息表**********" << endl;
//显示具体的工资信息
nSalarySys.DisplaySalary();
return 0;
}Object.prototype.toString & typeof
原文地址:http://blog.csdn.net/sunyingyuan/article/details/24623627