Accessing C++ private class members...

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
I forget how to do it. I have a class that is called Savings and it has a private member called balance. The constructor sets it to 0 but then I want to change it down the road to 3000 and play around with it in other functions. I'll out the balance then and then start it all over again with a new person and do the same process but it will have a different balance. This has me scratching my head.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
1. read up on "friend"
2. use "public"
3. Add "accessor functions" to the class, e.g.
void SetBalance(double newbal) ;
double GetBalance( ) ;
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
To access a private member, you have to create public functions in that class, like:

public int CLASSNAME:transfertoaccount(UINT amount, UINT timestamp)
{
moneyinaccount += amount;
// some other calls or whatever
return 1;
}

and

public int CLASSNAME:getbalance()
{
return mouneyinaccout;
}

You cannot access a private member directly, although MS C++ allows you to define friend functions.

But it's better not to use that. Private is private and has to be private. A function can do some checking on the values before applying them to the private variable.

 

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
Lost here. Look at my files, I uploaded them for you to see where I'm lost.
.cpp http://www.angelfire.com/goth/id_nut/P5Code.cpp
.h http://www.angelfire.com/goth/id_nut/P5Header.h

See, I'm totally lost on all this. I hope you get what I'm trying to do. This only has to work for two savers and I declare the variables, not a user. I want one balance to be 2000 the other to be 3000. I want it to run the program with an interest rate of .3% and display the amounts for both balances then run again but change the interest rate to .4% and show the balances.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: idNut
Lost here. Look at my files, I uploaded them for you to see where I'm lost.
.cpp http://www.angelfire.com/goth/id_nut/P5Code.cpp
.h http://www.angelfire.com/goth/id_nut/P5Header.h

See, I'm totally lost on all this. I hope you get what I'm trying to do. This only has to work for two savers and I declare the variables, not a user. I want one balance to be 2000 the other to be 3000. I want it to run the program with an interest rate of .3% and display the amounts for both balances then run again but change the interest rate to .4% and show the balances.


First, when you find yourself initializing variables right after you instantiate a class, maybe you should look at overloading the constructor:

// Constructor Overload
SavingsAccount(double AnnualInterestRate,
double InitialBalance = 2000,
int iMember = 0)
{
dAnnualInterestRate = AnnualInterestRate;
dSavingsBalance = dSavingsBalance;
this->iMember = iMember;
}

Next, you can't change the interest rate after you have called fCalcMonthlyInterest() because it modifies the balance. So if you want to allow that, maybe you should have another variable that stores the balance that you started with? Then you can add a method that allows you to change the interest rate while also reverting back to the balance that was saved before calling fCalcMonthlyInterest().
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: idNut
Lost here. Look at my files, I uploaded them for you to see where I'm lost.
.cpp http://www.angelfire.com/goth/id_nut/P5Code.cpp
.h http://www.angelfire.com/goth/id_nut/P5Header.h

See, I'm totally lost on all this. I hope you get what I'm trying to do. This only has to work for two savers and I declare the variables, not a user. I want one balance to be 2000 the other to be 3000. I want it to run the program with an interest rate of .3% and display the amounts for both balances then run again but change the interest rate to .4% and show the balances.

It would be easier if you posted you code to the forum so we could comment directly. But you didn't so I won't.

In any case.
The first problem I see is in your constructor for the SavingsAccount::SavingsAccount() class
First you set dSavingsBalance to 0 This seams reasonable.
Then, 2 lines later, you set it to 2000?! So, now all SavingsAccount objects start with a balance of 2000?

What you ought to do is have the default constructor (ie. the constructor that takes no arguments) set the initial balance to 0.
Then, add another constructor that takes the initial balance as an argument. Like this (I added an initial interest rate also):

SavingsAccount::SavingsAccount(double initial_balance, double initial_rate)
{
dAnnualInterestRate = initial_rate;
dSavingsBalance = initial_balance;
}

Now, you main function would look something like this:
int main()
{
SavingsAccount Saver1(2000, 0.3), Saver2(3000, 0.3);
....
}

You should probably add member access functions as well:

void SavingsAccount::set_balance(double new_balance)
{
dSavingsBalance = new_balance;
return;
}

double SavingsAccount::get_balance(void)
{
return dSavingsBalance;
}

In general, try to avoid public data members. Make them all private, and use access functions to work with them.
The advantage of this is that you can control how these variables get updated. For example, in your program, I could do this:

Saver1.dAnnualInterestRate = -0.3;

Which is clearly nonsense. I could catch this though:

void SavingsAccount::set_rate(double new_rate)
{
if(new_rate < 0)
{
cout << "WARNING: Attempting to set interest rate to a negative value!\n";
take_some_action();
}

dAnnualInterestRate = new_rate;
return;
}

If this is the only way that dAnnualInterestRate can be set (even the constructor uses it!) Then you are protected from dAnnualInterestRate ever being set to a negative value.

Here is another trick that may be beyond the scope of your current assignment (but maybe good for brownie points?)
If all your savings account objects will always all have the same interest rate, make dAnnualInterestRatea static member function.
This makes it shared between all instances of the class. You do this:

SavingsAccount:: DAnnualInterestRate = 0.5;

And now the interest rate for saver1 and saver 2 is now 0.5

And finally, are you sure you want the fCalcMonthlyInterest() function to be changing your interest rate?
 

Spydermag68

Platinum Member
Apr 5, 2002
2,616
99
91
Just a note on style.

Only use public, protected and private declarations only once in a header file.

Keep simular member functions togeter, SetBalance() and GetBalance().

If you are going to overload operators like = and + then overload +=.

If you create a member function that is only called by other member functions then place it in the private section.

Constructors should always the following when posible: Initialize variables, verify data, translate to internal representation, and allocate resources.