btw, passing the cointotal directly in like that is not a great design. Since it's just getting a copy of the coins int, then you'd have to send any updated coins value as a return value, which makes your client code really messy and also error prone if you forget to use the return value, and you end up doubling up e.g.
player.coinTotal = gameShop.shopFunction(player.coinTotal) which is just basically writing everything twice, so it's not good. And of course you can only update one thing this way (coinTotal), which would make it difficult if e.g. there were multiple things you could buy in the shop.
The best solution is to pass a reference to the player object in. Passing the cointotal from the player actuall takes 3 times as long. If you plass &player, it just passes a copy of memory address of the player in as the parameter. if you pass player.coinTotal, it takes the player memory address, calculates the coinTotal memory address from within that, then looks up that memory address to get the int, then passes a copy of that. So much slower!
I set up an example 'interface' for the player class for getting and setting the cointotal. Then the shop would handle all the shopping and adjustments within itself, and you also cater for having different characters go shopping without needing extra code.
class coins
{
int coinTotal=0; // private:
public: // this is the class interface
void setCoins(int amount) { coinTotal = amount; }
int getCoins() { return coinTotal; }
void addCoins(int amount) { coinTotal += amount; }
};
class shop
{
public:
void shopFunction (coins &player) //probably shouldn't have a return value. but most likely it would be a bool true/false value of whether you bought anything
{
//insert function stuff here
}
};
int main
{
coins player;
player.setCoins(foo);
shop gameShop;
gameShop.shopFunction(player);
return 0;
}