How do i go about doing calculation function?

Say, i have a display() that display the name and particulars of a employee.
I have a base class Employee() and 2 derive class TempEmployee and ContractEmployee classes. How do i go about defining a
Calculation function that have this formula:

Yearly gross pay = (12 x monthlyPay) + (OvertimeRate * noOfOvertimeHours)

Lets say for my base class i have relevant fields like:

1
2
3
float monthlyPay;
float Overtimerate;
float noOfOvertimeHours;


My method will be:
float calculatePay();

What do i need to pass in for this?
As in the way to implement the fields in..
Looks like you don't really know how functions work. Take a look at this page:
http://www.cplusplus.com/doc/tutorial/functions/

Ultimately, a function consist of three 'decisions':
-What input does it need? ("Parameters")
-What calculations does it need to do? ("Function body")
-What output does it provide? ("Return value")

Once you have that, you're good to go!
Sorry dbl post.. was using mobile to post this. the network was slow back then. I'll try and give the outcome
A doublepost with over half an hour in between and different description of the same problem. Your network must be pretty terrible.
It showed error, and i accidentally override the whole text when i select all and tried to copy. end up can't undo.

Anyway, if lets say I have my declaration of:
1
2
3
float monthlyPay;
float overtimeHr;
float overtimeRate;


And in the constructor I have:
1
2
3
4
5
6
Staff::Staff(float mPay, float otHour, float otRate)
{
   monthlyPay = mPay;
   overtimeHr = otHour;
   overtimeRate = otRate;
}


Do I pass in the mPay or monthlyPay?

Formula = (12 months * monthly pay) + (number of overtime hours * overtime payrate)

Is this correct?

1
2
3
4
5
6
float calculatePay(float mPay, float otHour, float otRate)
{
   float grossSalary;
   grossSalary = (12 * mPay) + (otHour * otRate);
   return grossSalary;
}


Yep, that's perfect.

If you want a little more elegance (but less readability), you can simply skip the temporary object:

1
2
3
4
float calculatePay(float mPay, float otHour, float otRate)
{
     return (12 * mPay) + (otHour * otRate);
}

Either way is fine though!
Last edited on
Ahh great. Thks for the tip

Another issue is that I have a display()

1
2
3
4
5
Staff::display()
{
cout<< "Staff's name        : "<< name <<endl;
cout<< "Staff's monthly pay : $"<< monthlyPay <<endl;
}


How do I put this function inside such that my static objects are able to print out the proper gross pay?
Do you want Staf::display() to also print the gross pay?

You can output (or use in any other way) the return value of a function just like you would a variable of that type. cout << calculatePay(mPay, otHour, otRate) << endl; will output the value of calculatePay.
Ahh. Awesome it worked but instead of mPay i use monthlyPay
1
2
3
4
5
6
7
Staff::Staff(float mPay, float otHour, float otRate)
{
   name = n;
   monthlyPay = mPay;
   overtimeHr = otHour;
   overtimeRate = otRate;
}


I tried that because in the display(), i use name instead of the "n" like above.
1
2
3
4
5
Staff::display()
{
cout<< "Staff's name        : "<< name <<endl;
cout<< "Staff's monthly pay : $"<< monthlyPay <<endl;
}


Weird. Should be using the n ...mPay..otHour instead.. don't know why it worked..
No, it's correct. 'monthlyPay' is a member of Staff objects, where 'mPay' is just a temporary variable during the function call.

The 'name = n' doesn't make much sense though, as you don't pass a variable 'n' to your Constructor. Where is it coming from?
Okay I got you bud. thanks for help!
One last question - How do I calculate all the monthly pay of all the staff?
As in the total amount to be paid to every single staff.
Last edited on
is it possible to do a addition of every staff gross salary in the array?
Topic archived. No new replies allowed.