您的位置:首页 > 编程语言 > C语言/C++

c和c++的一些训练题(14)(公司工资管理系统)(多态)

2015-03-26 17:42 393 查看
问题的提出:编写一个小公司的工资管理系统。该公司主要有4类人员:经理、兼职技术人员、销售员和销售经理。要求存储并显示每类人员的编号(从100起编号)、姓名和月薪,同时给出在创建每一类对象时构造函数的执行顺序(月薪计算方法为:经理固定月薪8000元,兼职技术人员100元/小时,销售员为当月销售额的4%,销售经理保底工资5000元另加其所管部门销售额的5%),要求用虚函数实现。

代码:

// virtual_.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

using namespace std;
const int pt=100;//兼职人员时薪
class Employee
{
protected:
char *name;
int num;
int salary;
static int totalnum;
public:
//virtual void pay()=0;
virtual void display()=0;
Employee(){cout<<"***************************************"<<endl;}
virtual ~Employee(){}

};
int Employee::totalnum=100;
class Manager:virtual public Employee
{
public:
Manager()
{
char *n=new char[256];
int no;
int w=8000;
cout<<"请输入经理的姓名:";
cin>>n;
name = n;
salary=w;
no=totalnum;
num=no;
totalnum++;
//cout<<"经理的工资为:"<<salary<<endl;
}
//void pay()
//{
//
//}
void display()
{
cout<<"经理的姓名为:"<<name<<endl;
cout<<"经理的工号为:"<<num<<endl;
cout<<"经理的工资为:"<<salary<<endl;
cout<<"***************************************"<<endl;
}
protected:
int wage;
};

class Parttime:virtual public Employee
{
public:
Parttime()
{
char *n=new char[256];
int no;
int t;
cout<<"请输入兼职人员的姓名:";
cin>>n;
cout<<"请输入兼职人员工作的小时数:";
cin>>t;
name = n;
time=t;
salary=time*pt;
no=totalnum;
num=no;
totalnum++;
//cout<<"经理的工资为:"<<salary<<endl;
}
//void pay()
//{
//
//}
void display()
{
cout<<"兼职人员的姓名为:"<<name<<endl;
cout<<"兼职人员的工号为:"<<num<<endl;
cout<<"兼职人员的工资为:"<<salary<<endl;
cout<<"***************************************"<<endl;
}
protected:
int time;
};

class Salesperson:virtual public Employee
{
public:
Salesperson()
{
char *n=new char[256];
int no;
int t;
cout<<"请输入销售员的姓名:";
cin>>n;
cout<<"请输入销售员的销售额:";
cin>>t;
name = n;
sales=t;
salary=sales*(0.04);
no=totalnum;
num=no;
totalnum++;
//cout<<"经理的工资为:"<<salary<<endl;
}
//void pay()
//{
//
//}
void display()
{
cout<<"销售员的姓名为:"<<name<<endl;
cout<<"销售员的工号为:"<<num<<endl;
cout<<"销售员的工资为:"<<salary<<endl;
cout<<"***************************************"<<endl;
}
protected:
int sales;
};

class SalesManager:virtual public Employee
{
public:
SalesManager()
{
char *n=new char[256];
int no;
int t;
int w=5000;
cout<<"请输入销售经理的姓名:";
cin>>n;
cout<<"请输入销售经理所在部门的销售额:";
cin>>t;
name = n;
sales=t;
wage=w;
salary=sales*(0.05)+w;
no=totalnum;
num=no;
totalnum++;
//cout<<"经理的工资为:"<<salary<<endl;
}
//void pay()
//{
//
//}
void display()
{
cout<<"销售经理的姓名为:"<<name<<endl;
cout<<"销售经理的工号为:"<<num<<endl;
cout<<"销售经理的工资为:"<<salary<<endl;
cout<<"***************************************"<<endl;
}
protected:
int wage;
int sales;
};

int _tmain(int argc, _TCHAR* argv[])
{
Manager a;
Parttime b;
Salesperson c;
SalesManager d;
Employee *em[4]={&a,&b,&c,&d};
for(int i=0; i<4; i++)
{
em[i]->display();
}
system("pause");
return 0;
}


结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: