1

I've developed a compiling bank system of different accounts. My base class is Account, and derived classes are Checking, Savings, MoneyMarket. The latter three inherit private member variable 'balance' from Account. All four accounts need to maintain and modify their own 'balance'.

However, I'm confused about the relation between Account's balance and derived class's 'balance'.

As you can see in Checking's getBalance(), it is forced to use Account's getBalance() due to the private variable, and the code only works when it displays Account::balance. This seems very strange, that it should call Account's balance to display it's own.

Please note that that all of Account's public methods are virtual to allow override.

Why does this work the way it is? Shouldn't the derived classes call their own copy of 'balance'?

Note: this code works and correctly displays the exact modified balance for each object. Below is Checking.cpp:

#include <iostream>
#include <iomanip>
#include "Checking.h"
using namespace std;

Checking::Checking() {setBalance(500); }

Checking::~Checking() {setBalance(0);}

void Checking::Withdrawal(double p_withdrawal){
    setBalance( getBalance(0) - p_withdrawal);
    cout << setprecision(2) << fixed;
    cout<<"\nWithdrawal from Checking leaves balance: "<<getBalance(0);
}

double Checking::getBalance(bool print){
    if (print==1)
        cout<<"\nBalance of Checking:"<< Account::getBalance(0);
    return Account::getBalance(1);
}

And for Account.h:

#ifndef ACCOUNT_H
#define ACCOUNT_H
using namespace std;

class Account{
public:
    Account();
    ~Account();

    virtual double getBalance(bool);
    virtual void setBalance(double);

    virtual void Deposit(double);
    virtual void Withdrawal(double);

    virtual void Transfer(Account&, Account&, double); 

private:
    double balance;

};

#endif 
Student
  • 11
  • 2
  • "All four accounts need to maintain and modify their own 'balance'." Why? What's wrong with the inherited one? I've never heard of a bank account with more than one balance. – molbdnilo Aug 21 '14 at 14:00
  • Hi. In the same way that a Checking and Savings account have their own balance, so do these. You are correct, each one has just one balance. My question is about which copy of 'balance' Checking's getBalance is calling. – Student Aug 21 '14 at 14:04
  • @user2786217 `getBalance` calls `Account`'s `getBalance` which accesses the `balance` of `Account` instance which is part of the `Checking` instance on which you call the function. – eerorika Aug 21 '14 at 14:09
  • There should be only one `balance`, the one in `Account`. Each subclass will automatically use that variable through the getter and setter inherited from the base class (and it *is* that account's own variable, not shared with anything else). Don't add a `balance` member to the subclasses. – molbdnilo Aug 21 '14 at 14:11
  • @user2079303 Thanks - can you explain that a bit more, or provide a link for info? – Student Aug 21 '14 at 14:15
  • @Student, here's a list of books, check out the beginner section http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Aug 21 '14 at 14:23

1 Answers1

1

private means non accessible from outside the class scope it is defined in, not even in children classes, if you want to directly access balance within your children classes make it protected not private.

Also note that inheritance (at least publicinheritance) means that your child class IS A parent class from the outside world and is useable as a parent class. Thus there is no "copy" of balance in your child class, there is one and only one balance in each instance. The balance field belongs to the Account class, whether this class is extended by inheritance or not does not change that.

The logic is : Checking IS AN Account (from an outside the class point of view), Account has a balance thus Checking has a balance. There is only one balance which is privately controlled by the Account part of the Checking.

Drax
  • 12,682
  • 7
  • 45
  • 85
  • But then when I modify balance in each subclass, am I modifying that subclass's copy of balance? – Student Aug 21 '14 at 14:06
  • Thanks Drax. What is the significance of Checking's getBalance() calling Account::getBalance()? Why can it simply not modify 'balance' instead? When I try to use balance variable in Checking's methods, I get 'private member' error. – Student Aug 21 '14 at 14:11
  • @Student as said in the first sentence of this answer, you can't access private members of your parent class in your child class, make it protected if you want that. – Drax Aug 21 '14 at 14:13
  • Thanks for clarifying. I know understand that in inheritance, derived classes can't access an inherited private variable - I changed Account's balance to 'protected' to reflect this. However, can you please explain why the code was working the way it is above? – Student Aug 21 '14 at 14:27
  • @Student `getBalance` and `setBalance` are public in the parent class, so you can call them from your child class, that's what for example `Account::getBlance(1)` does :) – Drax Aug 21 '14 at 14:29
  • I see. And we have to use that in Checking's getter/setter because, obviously, you can't call Checking::getBalance() in the getter. Interesting that calling Account::getBalance() in Checking's getter gives Checking's balance. Cool. Now I know the other way - make the private variable protected instead. – Student Aug 21 '14 at 14:39
  • @Student Just to be sure, `Checking's balance` is `Account's balance`, only `Checking` cannot access it directly :) – Drax Aug 21 '14 at 14:43
  • Wait, you just confused me with that. Checking inherited balance from Account, but can modify it's own copy of balance w/o altering the original inherited balance, correct? And if we modified balance in Checking, it would not affect balance in Account, right? – Student Aug 21 '14 at 14:48
  • @Student man did you read the part of the answer where i say that "there is NO copy of `balance` ", there is only one `balance` which is the `Account's balance` and is inherited (this does not mean copy at all) by `Checking`. There is ONLY ONE `balance` (and i thought i said that too much time in this post :D) – Drax Aug 21 '14 at 14:55
  • I understand it better now. Do you have any informative links that explain this part of inheritance well? – Student Aug 22 '14 at 13:45
  • @Student Actually this is the very basic principle of inheritance ^^ Try this : http://www.cplusplus.com/doc/tutorial/inheritance/ – Drax Aug 22 '14 at 14:04