- #include<iostream.h>
- #include<string.h>
- class employee
- {
- protected:
- int no;
- char *name;
- float salary;
- static int totalno;
- public:
- employee()
- {
- char temname[20];
- cout<<"执行employee构造函数!"<<endl;
- no=totalno++;
- cout<<"职工姓名:";
- cin>>temname;
- name=new char[strlen(temname)+1];
- strcpy(name,temname);
- salary=0;
- }
- ~employee()
- {
- cout<<"执行employee析构函数!"<<endl;
- delete[] name;
- }
- void pay(){}
- void display(){}
- };
- class technician:public employee
- {
- private:
- float hourlyrate;
- int workhours;
- public:
- technician()
- {cout<<"执行technician构造函数!"<<endl;hourlyrate=100;}
- void pay()
- {
- cout<<name<<"本月工作时数:";
- cin>>workhours;
- salary=hourlyrate*workhours;
- }
- void display()
- {
- cout<<"兼职技术人员"<<name<<"(编号为"<<no
- <<")"<<"本月工资:"<<salary<<endl;
- }
- };
- int employee::totalno=100; //静态数据成员totalno在类外进行初始化
- class saleman:virtual public employee
- {
- protected:
- float commrate;
- float sales;
- public:
- saleman()
- {cout<<"执行saleman构造函数!"<<endl;commrate=0.04f;}
- void pay()
- {
- cout<<name<<"本月销售额:";
- cin>>sales;
- salary=sales*commrate;
- }
- void display()
- {
- cout<<"销售员"<<name<<"(编号为"<<no<<")"
- <<")"<<"本月工资:"<<salary<<endl;
- }
- };
- class manager:virtual public employee
- {
- protected:
- float monthlypay;
- public:
- manager()
- {
- cout<<"执行manager构造函数!"<<endl;
- monthlypay=8000;
- }
- void pay(){salary=monthlypay;}
- void display()
- {
- cout<<"经理"<<name<<"(编号为"<<no<<")"
- <<")"<<"本月工资:"<<salary<<endl;
- }
- };
- class salesmanager:public manager,public saleman
- {
- public:
- salesmanager()
- {
- cout<<"执行salesmanager构造函数!"<<endl;
- monthlypay=5000;
- commrate=0.005f;
- }
- void pay()
- {
- cout<<name<<"所管部门月销售量:";
- cin>>sales;
- salary=monthlypay+commrate*sales;
- }
- void display()
- {
- cout<<"销售经理"<<name<<"(编号为"<<no<<")"
- <<")"<<"本月工资:"<<salary<<endl;
- }
- };
- void main()
- {
- manager m1;
- technician t1;
- saleman s1;
- cout<<endl<<"请注意以下建立类salesmanager的对象的过程!"<<endl;
- salesmanager sm1;
- m1.pay();
- m1.display();
- t1.pay();
- t1.display();
- s1.pay();
- s1.display();
- sm1.pay();
- sm1.display();
- }