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 );
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Issue accessing member variable of atomic class 16 115
Eclipse IDE - Cannot copy/paste from console output 8 185
Beginner to Unreal Engine 4 5 79
c++, dynamic object by json 1 42
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

786 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