Solved

Confused with classes and passing variables

Posted on 2006-10-29
11
183 Views
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.

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 )
{
}

0
Comment
Question by:andyw27
11 Comments
 
LVL 48

Expert Comment

by:AlexFM
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;
}
0
 

Author Comment

by:andyw27
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 ?

Thanks.
0
 
LVL 48

Accepted Solution

by:
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:

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
 
LVL 4

Assisted Solution

by:NorCal2612
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! :)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 11

Assisted Solution

by:DeepuAbrahamK
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,
DeepuAbrahamK
0
 
LVL 9

Assisted Solution

by:jhshukla
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)
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
 
LVL 11

Assisted Solution

by:dbkruger
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.

Dov
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 18001204
I think my answer is OK.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now