I am trying to write a member function for a class I have. The class is called Coins, and acts as a coin purse that keeps track of how many quarters, nickes, dimes, pennies.
for example, the constructor is Coins(int q, int d, int n, int p)
where each int represents the respective coin.
I am trying to write a member function
void deposit_coins(Coins & c)
where you can deposit coins from one Coin object to another so:
Illustrating as a class method as described in OP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
std::vector<Coins> Coins::deposit_coins(Coins& c)
{
quarters += c.quarters;//transferring coins
nickels += c.nickels;
dimes += c.dimes;
pennies += c.pennies;
c.quarters = 0;//setting Coins c coins to 0
c.nickels = 0;
c.dimes = 0;
c.pennies = 0;
std::vector<Coins> vecC {*this, c};
return vecC;//return by copy since local object
}
In case you don't care about the state of c after the transfer and only want to return *this object then you can omit the steps that set Coins c coins to 0 and return just *this