//å
°å·ç§é¥¼ï¼ä¸è§£é
//employee.h
class employee
{
protected:
char name[20]; //å§å
int individualEmpNo; //个人ç¼å·
int grade; //级å«
float accumPay; //æèªæ»é¢
static int employeeNo; //æ¬å
¬å¸èåç¼å·ç®åæ大å¼
public:
employee(); //æé å½æ°
~employee(); //ææå½æ°
virtual void pay()=0; //计ç®æèªå½æ°ï¼çº¯èå½æ°ï¼
virtual void promote(int increment=0); //å级å½æ°ï¼èå½æ°ï¼
void SetName(char *); //设置å§åå½æ°
char * GetName(); //æåå§åå½æ°
int GetindividualEmpNo(); //æåç¼å·å½æ°
int Getgrade(); //æå级å«å½æ°
float GetaccumPay(); //æåæèªå½æ°
};
class technician:public employee //å
¼èææ¯äººåç±»
{
private:
float hourlyRate; //æ¯å°æ¶é
Ž
int workHours; //å½æå·¥ä½æ¶æ°
public:
technician(); //æé å½æ°
void SetworkHours(int wh); //设置工ä½æ¶æ°å½æ°
void pay(); //计ç®æèªå½æ°
void promote(int); //å级å½æ°
};
class salesman:virtual public employee //å
¼èæ¨éåç±»
{
protected:
float CommRate; //æéå®é¢æåé
¬éçç¾åæ¯
float sales; //å½æéå®é¢
public:
salesman(); //æé å½æ°
void Setsales(float sl); //设置éå®é¢å½æ°
void pay(); //计ç®æèªå½æ°
void promote(int); //å级å½æ°
};
class manager:virtual public employee //ç»çç±»
{
protected:
float monthlyPay; //åºå®æèªæ°
public:
manager(); //æé å½æ°
void pay(); //计ç®æèªå½æ°
void promote(int); //å级å½æ°
};
class salesmanager:public manager,public salesman //éå®ç»çç±»
{
public:
salesmanager(); //æé å½æ°
void pay(); //计ç®æèªå½æ°
void promote(int); //å级å½æ°
};
//employee.cpp
#include<iostream>
#include<cstring>
#include"employee.h"
using namespace std;
int employee::employeeNo=1000; //åå·¥ç¼å·åºæ°ä¸º1000
employee::employee()
{ individualEmpNo=employeeNo++; //æ°è¾å
¥çåå·¥ç¼å·ä¸ºç®åæ大ç¼å·å 1
grade=1; //级å«åå¼ä¸º1
accumPay=0.0;} //æèªæ»é¢åå¼ä¸º0
employee::~employee()
{}
void employee::promote(int increment)
{ grade+=increment; } //å级ï¼æåç级æ°ç±incrementæå®
void employee::SetName(char* names)
{ strcpy(name,names); } //设置å§å
char* employee::GetName()
{ return name;} //æåæåå§å
int employee::GetindividualEmpNo()
{ return individualEmpNo;} //æåæåç¼å·
int employee::Getgrade()
{ return grade;} //æåæå级å«
float employee::GetaccumPay()
{ return accumPay;} //æåæèª
technician::technician()
{ hourlyRate=100;} //æ¯å°æ¶é
¬é100å
void technician::SetworkHours(int wh)
{ workHours=wh;} //设置工ä½æ¶é´
void technician::pay()
{ accumPay=hourlyRate*workHours;} //计ç®æèªï¼æå°æ¶è®¡é
¬
void technician::promote(int)
{ employee::promote(2); } //è°ç¨åºç±»å级å½æ°ï¼å2级
salesman::salesman()
{ CommRate=0.04;} //éå®æææ¯ä¾4%
void salesman::Setsales(float sl)
{ sales=sl;} //设置éå®é¢
void salesman::pay()
{ accumPay=sales*CommRate;} //æèª=éå®ææ
void salesman::promote(int)
{ employee::promote(0); } //è°ç¨åºç±»å级å½æ°ï¼å0级
manager::manager()
{ monthlyPay=8000;} //åºå®æèª8000å
void manager::pay()
{ accumPay=monthlyPay;} //æèªæ»é¢å³åºå®æèªæ°
void manager::promote(int )
{ employee::promote(3);} //è°ç¨åºç±»å级å½æ°ï¼å3级
salesmanager::salesmanager()
{ monthlyPay=5000;
CommRate=0.005;}
void salesmanager::pay()
{ accumPay=monthlyPay+CommRate*sales; } //æèª=åºå®æèª+éå®ææ
void salesmanager::promote(int)
{ employee::promote(2);} //è°ç¨åºç±»å级å½æ°ï¼å2级
//main.cpp
#include<iostream>
#include"employee.h"
#include<vector>
using namespace std;
int main()
{
manager m1;
technician t1;
salesmanager sm1;
salesman s1;
char namestr[20]; //è¾å
¥éåå§åæ¶é¦å
临æ¶åæ¾å¨namesträ¸
vector <employee *> vchar; //声æç¨äºä¿åæå对象çåé容å¨
vchar.push_back(&m1);
vchar.push_back(&t1);
vchar.push_back(&sm1);
vchar.push_back(&s1);
int i;
for(i=0;i<4;i++)
{
cout<<"请è¾ä¸ä¸ä¸ªéåçå§å:";
cin>>namestr;
vchar[i]->SetName(namestr); //设置æ¯ä¸ªæåå§å
vchar[i]->promote(); //å级
}
cout<<"请è¾å
¥å
¼èææ¯äººå"<<t1.GetName()<<"æ¬æçå·¥ä½æ¶æ°:";
int ww;
cin>>ww;
t1.SetworkHours(ww); //设置工ä½æ¶é´
cout<<"请è¾å
¥éå®ç»ç"<<sm1.GetName()<<"æ管è¾é¨é¨æ¬æçéå®æ»é¢:";
float sl;
cin>>sl;
sm1.Setsales(sl); //设置éå®æ»é¢
cout<<"请è¾å
¥æ¨éå"<<s1.GetName()<<"æ¬æçéå®é¢:";
cin>>sl;
s1.Setsales(sl); //设置éå®é¢
for(i=0;i<4;i++)
{
vchar[i]->pay(); //计ç®æèª
cout<<vchar[i]->GetName()<<"ç¼å·"<<vchar[i]->GetindividualEmpNo()<<"级å«ä¸º"
<<vchar[i]->Getgrade()<<"级ï¼æ¬æå·¥èµ"<<vchar[i]->GetaccumPay()<<endl;
}
}
温馨提示:内容为网友见解,仅供参考