您的位置:首页 > 其它

某公司有老板Boss、雇员Employee、小时工HourlyWorker和营销人员CommWorker,设计一个程序计算他们的薪金

2013-11-17 01:44 1441 查看
老板实行年薪制,如一年15万;雇员按月计酬,方法是基本工资+奖金;小时工按工作时间计算报酬,方法是工作小时*每小时单价;营销人员按月计酬,方法是基本工资+销售利润*5%。

每类人员都有姓名、职工编号、年龄、性别、工资等数据。设计计算格雷人员报酬的程序,用虚函数getPay()计算格雷人员的应得报酬。用虚函数print()打印输出各位工作人员的基本数据。

提示:将各类人员都共有的属性和行为抽象在类Person中,包括姓名、职工编号、年龄、性别等,以及函数getPay()和print()。getPay()设计为纯虚函数。将print()设计成一般虚函数,其余类从Person类派生,各类再定义getPay()的实现方法,并重载函数print()输出具体数据。

头文件:

头文件名:hanshu.h

代码:

#pragma once
#include"iostream"
#include"string"
#include"iomanip"
using namespace std;

class Person{
private:
char name[8];
int number;
int age;
char sex;
static int len;
public:
Person(char *a,int b,int c,char d):number(b),age(c),sex(d){
len=strlen(a);
strcpy(name,a); }
virtual double getPay()=0;
virtual void print(){
cout << setiosflags(ios::left);
cout << "姓名:" << setw(8) << "职工编号: " << setw(8) << "年龄:";
cout << setw(8) << "性别" << setw(8) << "工资" << endl ;
getname();
cout << setw(8) << getnumber() << setw(8) << getage();
cout << setw(8) << getsex() << setw(8) << getPay() << endl ;
}
void  getname(){
for(int i=0;i<len;i++)
cout << name[i];
}
int getnumber(){ return number; }
int getage(){ return age; }
char getsex(){ return sex; }
};

int Person::len=0;

class Boss:public Person{
private:
int workertime;
double wage;
public:
Boss(char *a,int b,int c,char d,int f,double g):workertime(f),wage(g),Person(a,b,c,d){};
double getPay();
void print(){
cout << setiosflags(ios::left);
cout << "姓名:  " << setw(8) << "职工编号:  " << setw(8) << "年龄:";
cout << setw(8) << "性别" << setw(8) << "工资" << endl ;
getname();
cout << "\t";
cout << setw(12) << getnumber() << setw(8) << getage();
cout << setw(8) << getsex() << getPay() << "万" << endl ;
}
};

double Boss::getPay(){
return (workertime*wage);
}

class Employee:public Person{
private:
double wage,bonus;
public:
Employee(char *a,int b,int c,char d,double f,double g):bonus(f),wage(g),Person(a,b,c,d){};double getPay();
void print(){
cout << setiosflags(ios::left);
cout << "姓名:  " << setw(8) << "职工编号:  " << setw(8) << "年龄:";
cout << setw(8) << "性别" << setw(8) << "工资" << endl ;
getname();
cout << "\t";
cout << setw(12) << getnumber() << setw(8) << getage();
cout << setw(8) << getsex() << setw(8) << getPay() << endl ;
}

};

double Employee::getPay(){
return (wage+bonus);
}

class HourlyWorker:public Person{
private:
int time;
double wage;
public:
HourlyWorker(char *a,int b,int c,char d,int f,double g):time(f),wage(g),Person(a,b,c,d){};
double getPay();
void print(){
cout << setiosflags(ios::left);
cout << "姓名:  " << setw(8) << "职工编号:  " << setw(8) << "年龄:";
cout << setw(8) << "性别" << setw(8) << "工资" << endl ;
getname();
cout << "\t";
cout << setw(12) << getnumber() << setw(8) << getage();
cout << setw(8) << getsex() << setw(8) << getPay() << endl ;
}
};

double HourlyWorker::getPay(){
return (time*wage);
}

class CommWorker:public Person{
private:
double wage;
double profit;
public:
CommWorker(char *a,int b,int c,char d,double f,double g):wage(f),profit(g),Person(a,b,c,d){};
double getPay();
void print(){
cout << setiosflags(ios::left);
cout << "姓名:  " << setw(8) << "职工编号:  " << setw(8) << "年龄:";
cout << setw(8) << "性别" << setw(8) << "工资" << endl ;
getname();
cout << "\t";
cout << setw(12) << getnumber() << setw(8) << getage();
cout << setw(8) << getsex() << setw(8) << getPay() << endl ;
}
};

double CommWorker::getPay(){
return (wage+profit*0.05);
}


主函数:

#include"iostream"
#include"hanshu.h"
using namespace std;

void main()
{
char a[]="liu";
char d[]="pp";
char g[]="L";
char f[]="p";
Boss b(a,1,20,'M',2,15);
b.print();
Employee e(d,2,20,'G',2000,500);
e.print();
HourlyWorker h(g,3,20,'M',20,80);
h.print();
CommWorker c(f,4,20,'M',2000,8000);
c.print();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: