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;