Please don't laugh is you think my question is odd. But I'm having troubles with creating a USERinput of a private class object.
The program gets the input, but when I output the object the value is the same as it ia assigned by the default constructor. The user input is just isn't saved.
Everybody who can can help will give me a big hand in this problem.
Firstly, you should use code tags for posting source: [code]//code goes here [/code]
A method already has all the ammunition it needs to change an object instance. It does not need the instance to be passed in as an argument (although it can feasibly be done with using reference parameters, it does not make sense to do it in this context). You should change these signatures:
Then, within the methods themselves you can simply refer to name and salary as they refer to the current instance. i.e. when calling worker2.inputs();, the variables name and salary within Employee::inputs() refer to the name and salary of worker2. No need to say employeeInstance.name or employeeInstance.salary within the methods.
I alluded to passing by reference, so I will explain. The reason the worker instances are not changing after being passed to inputs is because they are being passed by value (a copy is given to the method and the copy is changed, but not the original instance itself). If you want an object instance to change after being passed into a method, you need to use pass by reference (though you shouldn't use it to solve your current problem):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class Employee {
public:
void stealFrom(Employee& other, double amount);
...
};
void Employee::stealFrom(Employee& other, double amount) //pass by reference. Notice the &
{
salary += amount; //current instance gets more money
//"other" employee gets less money.
//Pass by reference so "other" refers to the instance passed in and not simply a copy
other.salary -= amount;
}