return0;
}
3.某单位的职工工资包括基本工资Wage,岗位津贴Subsidy,房租Rent,水费WaterFee,电费ElecFee。
设计实习工资管理的类Salary,该类的形式如下:
classSalary
{
public:
Salary(){初始化工资数据的各分项数据为0}
Salary(……){初始化工资数据的各分项数据}
voidsetXX(doublef){XX=f;}
doublegetXX(){returnXX;}
doubleRealSalary(){……}//计算实发工资
……
Private:
BoubleWage,Subsidy,Rent,WaterFee,ElecFee;
};
其中,成员函数setXX()用于设置工资的各分项数据,成员函数getXX()用于获取工资的各分项数据,XX代表Wage、Subsidy等数据成员,如Wage对应的成员函数则为setWage()和
getWage()。
实发工资=Wage+Subsidy-Rent-WaterFee-ElecFee
编程完善该类的设计,并在主函数中测试该类的各成员函数。
答案:
#include
#include
classsalary
{private:
doublewage;//基本工资
doublesubsidy;//岗位
doublerent;//房租
doublewaterfee;//水费
doubleelecfee;//电费
public:
salary(doubleWage=0,doubleSubsidy=0,doubleRent=0,doubleWaterfee=0,doubleElectfee=0)
{//初始化工资数据的各分项
wage=Wage;subsidy=Subsidy;rent=Rent;waterfee=Waterfee;elecfee=Electfee;}
salary(constsalary&s)//拷贝构造函数
{wage=s.wage;subsidy=s.subsidy;rent=s.rent;waterfee=s.waterfee;elecfee=s.elecfee;}
doublereal_salary();
voiddisplay();
~salary(){}//析构函数
};
doublesalary:
:
real_salary()
{doubleReal_Salary=0;//实际发放工资
Real_Salary=wage+subsidy-rent-waterfee-elecfee;returnReal_Salary;
}
voidsalary:
:
display()
{cout<cout<<"基本工资:
"<<""<"<<""<cout<<"房租:
"<<""<"<<""<cout<<"电费:
"<<""<cout<<"合计:
"<<""<cout<<"\n";
}
voidmain()
{salarySALARY(1000,20,30,40,50);
SALARY.display();
}
4.设计一个时钟类Clock。
数据成员包括hour(小时)、minute(分钟)、second(秒)。
要求用成员函数实现以下功能:
(1)创建具有指定时钟(小时、分钟、秒)的Clock对象,默认时钟为00:
00:
00。
(2)动态地设置时、分、秒。
(3)在屏幕上按“时:
分:
秒”的格式显示时钟。
(4)在主函数中测试该类。
答案:
#include
usingnamespacestd;
classClock
{public:
Clock(inth=0,intm=0,ints=0)
{hour=h;minute=m;second=s;}
voidsetHour(){cin>>hour;}
voidsetMinute(){cin>>minute;}
voidsetSecond()
{cin>>second;}
voidshow()
{cout<"<"<intgetHour(){returnhour;}
intgetMinute(){returnminute;}
intgetSecond(){returnsecond;}
private:
inthour,minute,second;
};
voidmain()
{Clocka;
cout<"<"<a.show();
}
5.设计一个职工类Employee。
数据成员包括字符串型的name(姓名)、street(街道地址)、city(市)、province(省)、postalcode(邮政编码)。
要求用成员函数实现以下功能:
(1)创建职工对象时,可以按不同方式指定职工信息。
(2)动态地设置职工对象信息。
(3)在屏幕上打印职工信息。
(4)在主函数中测试该类。
参考:
#include
#include
usingnamespacestd;
classEmployee{stringname;
stringstreet;
stringcity;
stringprovince;
stringpostalcode;
public:
Employee(stringstring1,stringstring2,stringstring3,stringstring4,stringstring5)
:
name(string1),street(string2),city(string3),province(string4),postalcode(string5)
{};
Employee(stringstring_1,stringstring_2);
Employee();
voidset(stringstringa,stringstringb,stringstringc,stringstringd,stringstringe);
voiddisplay();
};
Employee:
:
Employee(stringstring_1,stringstring_2)
{name=string_1;
street=string_2;
city="福州";
province="宁德";
postalcode="10086";
}
Employee:
:
Employee()
{}
voidEmployee:
:
display()
{cout<cout<<"\n****职工信息显示*****\n";
cout<<"*姓名:
"<<""<cout<<"*街道地址:
"<<""<cout<<"*市:
"<<""<cout<<"*省:
"<<""<