Confused with classes and passing variables

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 )
{
      setAccountBalance( Balance );
}

// function to set the account balance
void Account::setAccountBalance( int Balance )
{
      if (Balance > 0)
            accountBalance = Balance;

      if (Balance <= 0)
      {
            accountBalance = 0;

            cout << "You cannot have a negative balance - reset to 0" << endl;
      } //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 ?

// credit the account
int Account::credit ( int amount )
{
}

andyw27Asked:
Who is Participating?
 
AlexFMConnect With a Mentor Commented:
It returns new value of accountBalance. For example, if balance was 100 and amount is 10, new balance is 110, and this value is returned. This allows to know new balance by calling credit function. Consider that credit has void return type. Client calls this function and wants to know new balance:

balance1.credit( 10 );
int newBalance = balance1.getAccountBalance();   // newBalance = 110

If credit function returns new balance, client code can be:

int newBalance = balance1.credit( 10 );        // newBalance = 110

If client doesn't want to know new balance, it can ignore return value:

balance1.credit( 10 );
0
 
AlexFMCommented:
It is your decision: for example, function can return new balance:

int Account::credit ( int amount )
{
    accountBalance += amount;           // or -= (?) What is credit exactly - excuse my ignorance in anything except programming :)
    return accountBalance;
}
0
 
andyw27Author Commented:

Credit is a member function thats adds to the balance.

In you example where is the value returnbalance actually going. Thats the bit I getting confused with.  Is it being returned for use by a particular function or by returning it is overwriting all instances of int accountBalance ?

Thanks.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
NorCal2612Connect With a Mentor Commented:
void Account::credit ( int amount )
{
    accountBalance += amount;           // or -= (?) What is credit exactly - excuse my ignorance in anything except programming :)
}

//then you could have something called debit to use -= if you wanted. credit just means you add something to your account and debit is the opposite.

void Account::debit(int amount)
{
  accountBalance -= amount;
}

Or... was it specified that you needed to return an integer from the credit function?

If so, you could just return a 0 or a 1 depending on whether the credit was successful. For example, if the program passed a negative number into credit, you could return 1 to indicate to the calling function that the credit was unsuccessful.

Hope that helps a bit! :)
0
 
Deepu AbrahamConnect With a Mentor R & D Engineering ManagerCommented:
Hi there,

If your accountBalance is a member variable then you don't have to return any thing may be you can return true or false as NorCal2612 said.

void Account::credit ( int amount )
{
  accountBalance += amount;
}

So this accountBalance with new value and will be updated and will be available to that instance of object.

Best Regards,
DeepuAbrahamK
0
 
jhshuklaConnect With a Mentor Commented:
>> overwriting all instances of int accountBalance
There is one instance of accountBalance per Account object you create. that is not technically correct statement but you need not worry about that.
Let's say you create two accounts
Account acc1(20), acc2(50);

and credit 5 dollars (or euros or whichever currency you are using)
acc1.credit(5);

this will not affect the accountBalance in acc2.

acc1.current();
acc2.current();
will output "25" and "50" respectively.

There are also static member variables in a class. These variables are shared by all instances of the class and modifying them in one instance will affect its value in all instances because it is the same thing that is shared between all instances. For example you could have an int NumAccounts representing total number of accounts in use. Increment it in constructor and decrement it in destructor.

I suppose this helps your concern with "all instances of accountBalance".

There are also static member functions but they present a lot different issues and I recommend that you avoid them for the time being.

===
regarding what to return from credit() member function, C++ gives you a lot of options on how to achieve the same thing. depending on how your class would be used (and how you want it to be used), you can decide what to return. In this case I would go with void, given the limited info that we have. There are a lot of cases where you would want to overwrite original value with a new one and after that there is no means to recover the original value. In such a situation you would want to return the original value.
0
 
dbkrugerConnect With a Mentor Commented:
I realize this is just an exercise, but storing an integer amount of money is probably not the best choice. You could store an integer number of pennies, or store as a double or long double.

Other things you might want to consider include

You check to make sure that set isn't allowed to make the balance negative, but you don't check for a negative credit or debit, which is probably more relevant. Consider: I withdraw -1000000 and the ATM adds $1000000 to my account.

Dov
0
 
AlexFMCommented:
I think my answer is OK.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.