假设某销售公司有一般员工,销售员工和销售经理。月工资计算办法:
一般员工月薪=基本工资
销售员工月薪=基本工资+销售额*提成率
销售经理月薪=基本工资+职务工资+销售额提成率
编写程序,定义一个表示基本员工的基类Employee,他包含三个表示员工基本信息的成员:
编号number,姓名name,和基本工资basicSalary。
由Employee类派生销售员工Salasman类,Salasman类包含两个新数据成员:销售额sales和静态数据成员提成比例commrate。
再由Salasman类派生表示销售经理的Salamanager类。Salasmanager类包含新书局成员:岗位工资jobSalary。
为这些类定义初始化数据的构造函数,以及输入数据Input 计算工资Pay 和输出工资条Print的成员函数。
该公司员工基本工资2000元,销售经理的岗位工资3000元,提成率=5/1000。在main函数中,输入若干不同的员工信息测试你的类结构。
代码如下:
仅供个人学习使用。
/*员工工资管理*/
#include<iostream>
#include<string>
using namespace std;
class Employee
{
public:
Employee(string number1=" ",string name1=" ",double bsalary=2000):number(number1),name(name1),basicSalary(bsalary){}
void Input();
void Print();
protected:
string number;
string name;
double basicSalary;
};
void Employee::Input()
{
cout<<"编号:";
cin>>this->number;
cout<<"姓名:";
cin>>this->name;
}
void Employee::Print()
{
cout<<"编号:"<<this->number<<"\t普通员工:"<<this->name<<"\t本月工资:"<<this->basicSalary<<endl;
}
class Salesman:public Employee
{
public:
Salesman(string number1=" ",string name1=" ",int sales1=0,double bsalary=2000):Employee(number1,name1,bsalary)
{
sales=sales1;
}
void Input()
{
Employee::Input();
cout<<"销售额:";
cin>>this->sales;
}
double Pay()
{
double salary;
salary=this->basicSalary+(commrate*this->sales);
return salary;
}
void Print()
{
cout<<"编号:"<<this->number<<"\t销售员:"<<this->name<<"\t本月工资:"<<this->Salesman::Pay()<<endl;
}
protected:
int sales;
static double commrate;
};
double Salesman::commrate=5.0/1000.0; //静态数据成员类内声明,类外定义
class Salesmanager:public Salesman
{
public:
Salesmanager(string number1=" ",string name1=" ",int sales1=0,double bsalary=2000,double jsalary=1000):Salesman(number1,name1,sales1,bsalary)
{
jobSalary=jsalary;
}
void Input()
{
Employee::Input();
cout<<"部门总销售额:";
cin>>this->sales;
}
double Pay()
{
double salary;
salary=this->jobSalary+this->basicSalary+(commrate*this->sales);
return salary;
}
void Print()
{
cout<<"编号:"<<this->number<<"\t销售经理:"<<this->name<<"\t本月工资:"<<this->Salesmanager::Pay()<<endl;
}
protected:
double jobSalary;
};
int main()
{
cout<<"普通员工测试:"<<endl;
Employee emp1;
emp1.Input();
emp1.Print();
cout<<endl;
cout<<"销售员工测试:"<<endl;
Salesman emp2;
emp2.Input();
emp2.Print();
cout<<endl;
cout<<"销售经理测试:"<<endl;
Salesmanager emp3;
emp3.Input();
emp3.Print();
return 0;
}