I'd like to return all this information from my toString like how it does on line 6. I have a strong feeling I've done something wrong and/or could do this a better way. Can anyone point me in the right direction?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
string Employee::toString() //double check this function, it's weird how i've done it
{
ostringstream ostr;
ostr << "ID: " << IDnumber << " " << name << " " << payRate
<< endl;
return ostr.str();
//loop through all payslips for that employee and use the payslip toString function
for(int i = 0; i <= payCount - 1; i++){
pSlip[i].toString();
}
//calcs and displays pay and tax totals for payslips
cout << "YTD pay: " << YTDpay << " YTD tax: " << YTDtax << endl;
}
I'd like to return all this information from my toString like how it does on line 6
I am not sure what you are trying achieve. It looks like you want to calc YTD stuff.
Why are you converting the info to a string? Why not cycle through the pSlip array and calc the totals?
The pSlip[i].toString(); doesn't seem to make sense. The array doesn't have a function toString() You need to make an object from the class, then call whichever function you need.
I think you might need a class called CPayslip, which holds that info. The CEmployee class could have an array (or <vector>) of CPayslip objects. The CEmployee class would also have a function that cycles through the array and calcs the totals.
I want to print employee information along with the employees payslips then the variables YTDpay and YTDtax. I don't want to do any calculations I've done them all in other functions. I have created a toString function in a seperate class for pSlips so it's fine I believe it's using polymorphism.
I'm only a beginner so I'm still probably making some obvious mistakes somewhere. But yeah I want this toString function to return the Employee info (which it does on line 6) along with the paySlips for that employee (which it gets them when looping through the array but it doesn't return them. Ideally I want to add them to the variable ostr so I can return it and same goes with the line that returns YTDpay and YTDtax. So yeah I want to do something like adding all the info to the ostr variable and then return it.
I hope I'm making sense, thanks for your contribution.
Why not append the payslip info to the the ostr variable, then append the YTD stuff in the same way, then return the whole thing at the end of the function.
If you have very similar toString functions in 2 classes, this implies having a virtual function in a base class.