#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string name;
public:
Person(string _name, int _money) : name(_name), money(_money) {}
int money;
int get_money() {return money; }
};
class Company
{
private:
string name;
public:
Company(string _name) : name(_name) {}
void pay_salary(Person p, int amount);
};
void Company::pay_salary(Person p, int amount)
{
p.money += amount;
}
int main()
{
Person bob("Bob", 0);
Company c("C");
cout << bob.get_money() << endl; // prints 0
c.pay_salary(bob,100);
cout << bob.get_money() << endl; // prints 0
return 0;
}
In this code (simplified to illustrate my problem) I want to access the member variable money, inside Company to increase the money of a Person. However, p.money += amount appears to only work locally, and does not change the value of money for the specific person in main.
What are some methods of making this work? Would it be possible to make money a a protected member variable?