Hello,
I'm writing a little program to help myself get used to classes in c++.
It's something like an accounting simulation.
I think that I have a problem with understanding how to call member functions when objects are being passed as arguments. Following is my code involving the three important member functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
void Account::transfer(double amount, Account payee)
{ payee.deposit(withdraw(amount));
}
double Account::withdraw(double amount)
{ if (amount > acct_bal)
{ std::cerr << "Cannot withdraw an amount greater than " << acct_bal << "." << std::endl;
return 0;
}
if (amount > 0)
{ acct_bal -= amount;
return amount;
}
if (amount < 0)
{ std::cerr << "Illegal or negative amount. Aborting. "<< std::endl;
return 0;
}
}
void Account::deposit(double amount)
{ if (amount > 0)
acct_bal += amount;
else
std::cerr << "Illegal deposit amount. Aborting.";
}
|
So, transfer receives a arguments for the amount, then payee (which is another Account type object.) I suspect that the call payee.withdraw(etc.) is somehow incorrect. A similar call directly to the object works, ie:
|
an_account.deposit(other_account.withdraw($$$))
|
The problem with the member function is that the withdrawal effect takes place, but the deposit doesn't register with the "payee" object somehow. There are no errors, either cerrs I've coded or runtime errors. I've messed around with the keyword "this" but haven't had any luck yet. Should I be passing a pointer instead of an object?
If more extensive code is desired, or I haven't been clear, feel free to ask questions.