Problem G: 设计工人类Worker

 

Problem Description

设计工人类Worker,它具有姓名name,年龄age,工作部门Dept,工资salary等数据成员。其中,salary为Salary类型的数据,下面的程序拟完成Worker类的设计并用静态成员统计工人的人数,请把程序补充完整。

提示:这里成员函数setXX()用于设置各分项数据,成员函数getXX()用于获取各分项数据,XX代表数据成员,如age对应的成员函数则为setAge()和getAge()。

#include <iostream>

#include <string>

using namespace std;

class Salary{

private:

         double Wage,Subsidy,Rent,WaterFee,ElecFee;//基本工资Wage,岗位工资Subsidy,房租Rent,水费WaterFee,电费ElecFee

public:

         Salary(double i1,double i2=0,double i3=0,double i4=0,double i5=0){//初始化工资数据的各分项

                   Wage=i1;

                   Subsidy=i2;

                   Rent=i3;

                   WaterFee=i4;

                   ElecFee=i5;

         }

         Salary(){//初始化工资的各分项数据为0

                   Wage=Subsidy=Rent=WaterFee=ElecFee=0;

         }

         void setWage(double f){Wage=f;}

         double getWage(){return Wage;}

 

         void setSubsidy(double f){Subsidy=f;}

         double getSubsidy(){return Subsidy;}

 

         void setRent(double f){Rent=f;}

         double getRent(){return Rent;}

 

         void setWaterFee(double f){WaterFee=f;}

         double getWaterFee(){return WaterFee;}

 

         void setElecFee(double f){ElecFee=f;}

         double getElecFee(){return ElecFee;}

 

         double RealSalary(){//计算实发工资,实发工资=Wage+Subsidy-Rent-WaterFee-ElecFee

                   return Wage+Subsidy-Rent-WaterFee-ElecFee;}

};

//你的代码将被嵌在这里

int main(){

         Worker w1("John",30,"design");

         Worker w2;

         cout<<"the total num is: "<<w1.getNum()<<endl;

         w2.setName("Linda");

         cout<<"in w2 the name is: "<<w2.getName()<<endl;

         return 0;

}

Sample Output

the total num is: 2
in w2 the name is: Linda
class Worker{
    static int num;
    string name,Dept;
    int age;
    Salary *salary = new Salary();
public:
    Worker():age(0),name(""),Dept(""){
        Worker::num++;
    }
    Worker(string n,int i,string dept):age(i),name(n),Dept(dept){
        Worker::num++;
    }
    ~Worker(){
        Worker::num--;
    }
    void setAge(int i) {
        age = i;
    }
    int getAge() {
        return age;
    }
    void setName(string n) {
        name = n;
    }
    string getName() {
        return name;
    }
    void setDept(string dept) {
        Dept = dept;
    }
    string getDept() {
        return Dept;
    }
    void setSalary(double wage) {
        salary->setWage(wage);
    }
    Salary * getSalary() {
        return salary;
    }
    static int getNum(){
        return Worker::num;
    }
};
int Worker::num = 0;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值