Confused with passing values through classes ?

A

andyw

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

Gianni Mariani

andyw said:
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 )
// get in the habbit of using initializers
: Balance( BalanceCheck( 10 ) )
// BalanceCheck should possibly throw an error exception
{
setAccountBalance( Balance );
}

// function to set the account balance
void Account::setAccountBalance( int Balance )

refactor setAccountBalance into a static method that
returns the new Balance and name it BalanceCheck.
{
if (Balance > 0)
accountBalance = Balance;

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

cout << "You cannot have a negative balance - reset to 0" <<
endl;

I wouldn't do this. I would throw an exception here so that the caller
trying to set the balance gets told that the transaction failed. You
need to somehow account for the credit or balance going into never never
land. Otherwise I would love to be your system admin where I can set up
a whole bunch of accounts where the bogus credit discrepancies get
credited to my personal vacation and entertainment account.
} //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 ?

I don't really understand your question - perhaps this will answer it
for you.
// credit the account
int Account::credit ( int amount )
{
Balance = BalanceCheck( amount + Balance );
return Balance;
 
G

Greg

andyw said:
I'm working through an exercise.

#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.

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

A "credit" is a certain amount of money being added to an account -
while a debit is a certain amount of money leaving the account. So the
credit routine should increase the account balance by the amount of the
credit (or nearly so: a certain percentage of the deposit needs to be
diverted to my personal bank account, the number of which I will be
sending you in a private e-mail.)

Greg
 
D

Daniel T.

andyw said:
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 ?

What variable inside an Account object holds the overall balance? Add
the 10 to that.
Also I'm not sure to what function I should be returning
the value to ?

Your the one designing the function, what do you *want* credit to
return? Does it really need to return anything? Is it a setter or a
getter? (i.e., is it supposed to change the state of an Account, or
return state information about an Account?)
 
J

Jim Langston

andyw said:
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 )
{
Balance += amount;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top