Confused with classes and passing variables

Posted on 2006-10-29
Last Modified: 2010-04-01
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. 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 )

Question by:andyw27
LVL 48

Expert Comment

ID: 17828369
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;

Author Comment

ID: 17828377

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 ?

LVL 48

Accepted Solution

AlexFM earned 100 total points
ID: 17828387
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: 10 );
int newBalance = balance1.getAccountBalance();   // newBalance = 110

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

int newBalance = 10 );        // newBalance = 110

If client doesn't want to know new balance, it can ignore return value: 10 );
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


Assisted Solution

NorCal2612 earned 100 total points
ID: 17829275
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! :)
LVL 11

Assisted Solution

DeepuAbrahamK earned 100 total points
ID: 17829762
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,

Assisted Solution

jhshukla earned 100 total points
ID: 17831994
>> 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);

this will not affect the accountBalance in acc2.

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.
LVL 11

Assisted Solution

dbkruger earned 100 total points
ID: 17834411
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.

LVL 48

Expert Comment

ID: 18001204
I think my answer is OK.

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question