【发布时间】:2021-08-05 08:29:36
【问题描述】:
我有一个问题-
编写一个 c++ 程序来计算 4 名员工的总薪水(净薪水+DA+TDS),其中 + 运算符重载以获得所有 4 名员工获得的总薪水。还可以通过运算符重载得到员工的平均工资。显示所有四名员工的所有详细信息。还显示谁的薪水最高。员工工资必须由用户在运行时输入。
#include<iostream>
#include<string>
using namespace std;
class Employee
{
private:
int net_salary, DA, TDA, Gross_salary;
public:
void SetData(int net, int da, int tda, int gross)
{
net_salary=net;
DA=da;
TDA=tda;
Gross_salary=gross;
}
void GetData()
{
cout << "Enter net salary: "<<net_salary;
DA = (15*net_salary)/100; //Declaring the value of DA as 15% and calculating the amount
//on basis of net salary of the employee
TDA = (10*net_salary)/100; //Declaring the value of TDA as 10% and calculating the amount
// of TDA on basis of net salary
Gross_salary = net_salary+DA+TDA;
}
void DisplayData()
{
cout << "Total Gross Salary = "<<Gross_salary;
}
Employee operator +(Employee e)
{
Employee temp;
temp.Gross_salary=Gross_salary+e.Gross_salary;
return temp;
}
};
int main()
{
Employee e1,e2,e3,e4,e5;
e1.GetData();
e2.GetData();
e3.GetData();
e4.GetData();
e5=e1+e2+e3+e4;
e5.DisplayData();
return 0;
}
【问题讨论】:
-
你没有在任何地方调用 n
SetData(),所以当operator+尝试使用它们时,你的数据成员没有分配给它们的值。