andyw wrote:
Quote:
I'm working through an exercise.
>
I got it working so that the constructor / get and set function all
work. Now I'm trying to create a new function that will change the
value, here is what I have at the moment:
#include <iostream>
using std::cout;
using std::endl;
>
#include "account.h"
>
// constructor initializes accountBalance with int supplied as argument
Account::Account( int Balance )
// get in the habbit of using initializers
: Balance( BalanceCheck( 10 ) )
// BalanceCheck should possibly throw an error exception
Quote:
{
setAccountBalance( Balance );
}
>
// function to set the account balance
void Account::setAccountBalance( int Balance )
refactor setAccountBalance into a static method that
returns the new Balance and name it BalanceCheck.
Quote:
{
if (Balance 0)
accountBalance = Balance;
>
if (Balance <= 0)
{
accountBalance = 0;
>
cout << "You cannot have a negative balance - reset to 0" <<
endl;
I wouldn't do this. I would throw an exception here so that the caller
trying to set the balance gets told that the transaction failed. You
need to somehow account for the credit or balance going into never never
land. Otherwise I would love to be your system admin where I can set up
a whole bunch of accounts where the bogus credit discrepancies get
credited to my personal vacation and entertainment account.
Quote:
} //end if statement
}
>
// function to get the course name
int Account::getAccountBalance()
{
return accountBalance;
}
>
>
>
// display the current balance
void Account::current()
{
cout << "The current balance is: " << getAccountBalance() << endl;
}
>
------------------------------------------------------------------------------------------
The function I'm having trouble with is the credit(). Can someone talk
me through what should be happening here.
>
I have the following code in main.cpp file.
>
balance1.credit( 10 );
>
This I believe should call the credit function of object1 and pass it
an integer value of 10. The function below then needs to take this
value of 10 add it to the overall balance. So do I just add amount to
int Balance ? Also I'm not sure to what function I should be returning
the value to ?
I don't really understand your question - perhaps this will answer it
for you.
Quote:
>
// credit the account
int Account::credit ( int amount )
{
Balance = BalanceCheck( amount + Balance );
return Balance;