题目描述
设计一个计算薪水的类Payroll,数据成员包括:单位小时工资、周工作小时、每周应付工资,其中每周应付工资=单位小时工资* 周工作小时。要求:定义构造函数、析构函数、拷贝构造函数,常成员函数output()用来输出对象的数据成员值。主函数中定义两个对象:第一个对象的单位小时工资、周工作小时由键盘输入,第二个对象定义为常对象,他的单位小时工资为第一个对象的1.5倍,周工作小时相同,输出每个对象的信息。
输入描述
一行:第一个对象的单位小时工资、周工作小时
输出描述
两个对象的小时工资、周工作时数、周应付工资
函数调用的相关信息
输入样例
25 38
输出样例
Constructor is called!
小时工资:25 周工作时数:38 周应付工资:950
Copy constructor is called!
小时工资:37.5 周工作时数:38 周应付工资:1425
Destructor is called!
#include <iostream>
using namespace std;
class Payroll
{
public:
Payroll(double a, int b)
{
cout << "Constructor is called!" << endl;
SalaryPurHour = a;
WorkTime = b;
}
double GetTotalSalary()
{
TotalSalary = WorkTime * SalaryPurHour;
return TotalSalary;
}
Payroll(const Payroll &a);
void output()
{
cout << "小时工资:" << SalaryPurHour << " 周工作时数:" << WorkTime << " 周应付工资:" << GetTotalSalary() << endl;
}
~Payroll()
{
cout << "Destructor is called!" << endl;
}
private:
int WorkTime;
double SalaryPurHour;
double TotalSalary;
};
Payroll::Payroll(const Payroll &x)
{
cout << "Copy constructor is called!" << endl;
SalaryPurHour = 1.5*x.SalaryPurHour;
WorkTime = x.WorkTime;
TotalSalary = SalaryPurHour * WorkTime;
}
int main()
{
int WorkTime;
double SalaryPurHour;
cin >> SalaryPurHour;
cin >> WorkTime;
Payroll A(SalaryPurHour, WorkTime);
A.output();
Payroll B(A);
B.output();
return 0;
}
拷贝构造函数
Payroll::Payroll(const Payroll &x)
{
cout << "Copy constructor is called!" << endl;
SalaryPurHour = 1.5*x.SalaryPurHour;
WorkTime = x.WorkTime;
TotalSalary = SalaryPurHour * WorkTime;
}