You can pass it directly. The problem with the code above is that you're passing values x and y in but you're not actually doing anything with them in the body of the function (you're returning the multiplication of the member values salary and tax). You'd need to use the values x and y in the function body to obtain a different result.
To me, it seems like this code might need a little restructuring. I'm not quite sure what the 'solve' instance should be doing. To me, it makes more sense to have a list of employees, the ability to set their salary and tax and something to work out their tax.
I'll give a brief example of what I mean. However, I'll use a struct to keep it implicitly public for simplicity:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
struct Employee
{
string m_forename;
string m_surname;
double m_salary;
double m_tax;
double CalculateDeduction()
{
return m_salary * m_tax;
}
};
// In main...
const int NUM_EMPLOYEES = 5;
Employee employees[NUM_EMPLOYEES]; // A list of five employees, for example
for( int i = 0; i < NUM_EMPLOYEES; i++ )
{
// They're all named "John Smith" ;-)
employees[i].m_forename = "John";
employees[i].m_surname = "Smith";
// Assign some different values for salary and tax
employees[i].m_salary = ( i + 1 ) * 10000;
employees[i].m_salary = ( i + 1 ) * 0.05;
cout << "The tax deduction for employee " << i << " is: " << employees[i].ComputeDeduction() << endl;
}
|
If you wanted to store the deduction for each employee, you could add a deduction amount member variable and instead of returning the value in ComputeDeduction, you could just assign it to that variable.
What I've written is more or less the same as yours, a few syntactically issues aside. It makes more sense to treat Employee as an object, rather than have a type of object called salary computation, which is more of a function.