S
SB
Ok, very simple problem. I'm trying to update a value by calling a function
using pass by reference, but it does not update the value. In short, the
value I'm trying to update is balance, which is a private member of the
class Account. I have a public function called getBalance(). I have another
public function called deposit, which I pass the balance (by calling
getBalance() using pass by reference) and a second value for the amount of
deposit. Inside the deposit() function if I print the value of the balance
plus the amount to be deposited, it prints the right value, i.e. current
balance plus deposit amount. However, it's not updating the balance member
of the Account class because when I call the getBalance(), it still shows
the original value before the deposit. I know I'm missing something very
simple here, but I just can't see it. Also, I'm getting the following
warning regarding the function calls using pass by reference, but I don't
understand it..
report4_q3.cpp: In function `int main()':
report4_q3.cpp:20: warning: initialization of non-const reference `double &'
from rvalue `double'
report4_q3a.cpp:21: warning: in passing argument 1 of
`Account::deposit(double &, double)'
report4_q3.cpp:27: warning: initialization of non-const reference `double &'
from rvalue `double'
report4_q3a.cpp:29: warning: in passing argument 1 of
`Account::withdrawal(double &, double)'
Here's the .h file....(report4_q3.h)
using namespace std;
class Account
{
private:
double balance;
public:
Account(); // default constructor
double getBalance();
void setBalance(double);
void deposit(double&, double);
void withdrawal(double&, double);
~Account(); // destructor
};
Here's the implementation file...(report4_q3a.cpp)
#include <iostream>
#include <string>
#include "report4_q3.h"
using namespace std;
// default constructor
Account::Account()
{
// give a starting balance of $500
balance = 500.00;
}
double Account::getBalance()
{
return balance;
}
void Account::deposit(double& bal, double deposit)
{
cout<<endl<<"making a deposit!"<<endl;
bal += deposit;
cout<<"after transaction balance is: $"<<bal<<endl; //this of course shows
the correct balance
}
void Account::withdrawal(double& bal, double withdrawal)
{
cout<<"making a withdrawal!"<<endl;
bal -= withdrawal;
cout<<"Your new balance is $"<<getBalance()<<endl;
}
// destructor
Account::~Account()
{
// do nothing
}
Here's the file with main...(report4_q3.cpp)
#include "report4_q3a.cpp"
using namespace std;
int main()
{
Account a;
int inputVal = 0;
double amount = 0.0;
cout<<"Your balance is: $"<<a.getBalance()<<endl;
cout<<endl<<"Press 1 to make a deposit, Press 2 to make a withdrawal, 3 for
account balance and 4 to quit."<<endl;
cin>>inputVal;
switch(inputVal)
{
case 1: {
cout<<"Enter the amount to deposit: ";
cin>>amount;
a.deposit(a.getBalance(), amount);
cout<<"Your new balance is $"<<a.getBalance()<<endl; // right here it
shows the original balance which is incorrect
}
break;
case 2: {
cout<<"Enter the amount to withdrawal: ";
cin>>amount;
a.withdrawal(a.getBalance(), amount);
}
break;
case 3: cout<<"Your balance is: $"<<a.getBalance()<<endl;
break;
case 4: return 0;
break;
default: cout<<"You have entered an invalid value - try again."<<endl;
}
return 0;
}
Any help is GREATLY appreciated!
Thanks!
SB
using pass by reference, but it does not update the value. In short, the
value I'm trying to update is balance, which is a private member of the
class Account. I have a public function called getBalance(). I have another
public function called deposit, which I pass the balance (by calling
getBalance() using pass by reference) and a second value for the amount of
deposit. Inside the deposit() function if I print the value of the balance
plus the amount to be deposited, it prints the right value, i.e. current
balance plus deposit amount. However, it's not updating the balance member
of the Account class because when I call the getBalance(), it still shows
the original value before the deposit. I know I'm missing something very
simple here, but I just can't see it. Also, I'm getting the following
warning regarding the function calls using pass by reference, but I don't
understand it..
report4_q3.cpp: In function `int main()':
report4_q3.cpp:20: warning: initialization of non-const reference `double &'
from rvalue `double'
report4_q3a.cpp:21: warning: in passing argument 1 of
`Account::deposit(double &, double)'
report4_q3.cpp:27: warning: initialization of non-const reference `double &'
from rvalue `double'
report4_q3a.cpp:29: warning: in passing argument 1 of
`Account::withdrawal(double &, double)'
Here's the .h file....(report4_q3.h)
using namespace std;
class Account
{
private:
double balance;
public:
Account(); // default constructor
double getBalance();
void setBalance(double);
void deposit(double&, double);
void withdrawal(double&, double);
~Account(); // destructor
};
Here's the implementation file...(report4_q3a.cpp)
#include <iostream>
#include <string>
#include "report4_q3.h"
using namespace std;
// default constructor
Account::Account()
{
// give a starting balance of $500
balance = 500.00;
}
double Account::getBalance()
{
return balance;
}
void Account::deposit(double& bal, double deposit)
{
cout<<endl<<"making a deposit!"<<endl;
bal += deposit;
cout<<"after transaction balance is: $"<<bal<<endl; //this of course shows
the correct balance
}
void Account::withdrawal(double& bal, double withdrawal)
{
cout<<"making a withdrawal!"<<endl;
bal -= withdrawal;
cout<<"Your new balance is $"<<getBalance()<<endl;
}
// destructor
Account::~Account()
{
// do nothing
}
Here's the file with main...(report4_q3.cpp)
#include "report4_q3a.cpp"
using namespace std;
int main()
{
Account a;
int inputVal = 0;
double amount = 0.0;
cout<<"Your balance is: $"<<a.getBalance()<<endl;
cout<<endl<<"Press 1 to make a deposit, Press 2 to make a withdrawal, 3 for
account balance and 4 to quit."<<endl;
cin>>inputVal;
switch(inputVal)
{
case 1: {
cout<<"Enter the amount to deposit: ";
cin>>amount;
a.deposit(a.getBalance(), amount);
cout<<"Your new balance is $"<<a.getBalance()<<endl; // right here it
shows the original balance which is incorrect
}
break;
case 2: {
cout<<"Enter the amount to withdrawal: ";
cin>>amount;
a.withdrawal(a.getBalance(), amount);
}
break;
case 3: cout<<"Your balance is: $"<<a.getBalance()<<endl;
break;
case 4: return 0;
break;
default: cout<<"You have entered an invalid value - try again."<<endl;
}
return 0;
}
Any help is GREATLY appreciated!
Thanks!
SB