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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 );
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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