473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Confused with passing values through classes ?

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

Oct 29 '06 #1
4 1017
andyw wrote:
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;
}

Oct 29 '06 #2

andyw wrote:
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

Oct 29 '06 #3
"andyw" <an******@hotmail.comwrote:
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?)
// credit the account
int Account::credit ( int amount )
{
}
--
To send me email, put "sheltie" in the subject.
Oct 29 '06 #4

"andyw" <an******@hotmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
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;
}
Oct 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Beach Potato | last post by:
I guess I've been out of C++ for a while, since right now I don't seem to get a simple solution for overriding inherited constrictors. What worked in Borland C++ & Pascal (and Java, if I remember...
11
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to...
10
by: Stan | last post by:
There are two ways to pass structured data to a web service: xml === <Order OrderId="123" OrderAmount="234" /> or class =====
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
3
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder...
7
by: AMP | last post by:
Hello, I have this in form1: namespace Pass { public partial class Form1 : Form { public Form2 form2; public Form1() {
10
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
3
by: Buckaroo Banzai | last post by:
Hi, I just started learning JAVA and I'm really NOT sure how to manipulate values in different classes. for example. I have a class 'class1' which updates some variables for its own use. then I...
10
by: mcl | last post by:
Why can I not the change the value of a variable in another class, when I have passed it via a parameter list. I am sure I am being stupid, but I thought passed objects were Read/ Write eg...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.