title: Have I misapplied inheritance?
body: <p>I am trying to set up a program that can generate balance sheets based on summing a number of transactions, and present the results in a format like this:</p>
  
  <p><img src="https://i.stack.imgur.com/M3tbE.jpg" alt="balance sheet">
  The important attributes here are that the top-level account (e.g. Assets) is decomposed into a tree of sub-accounts, and only the lowest-level accounts ("leaves") track their own balances (the balances of higher-level accounts are just the sums of the balances of their sub-accounts).</p>
  
  <p>My go-to approach is to use inheritance:</p>
  
  <pre><code>class Account{
     string name;
     virtual int getBalance() =0; //generic base class has no implementation
     virtual void addToBalance(int amount) =0;
  };
  class ParentAccount : public Account{
     vector&lt;Account*&gt; children;
     virtual int getBalance() {
        int result = 0;
        for (int i = 0; i &lt; children.size(); i++)
            result += children[i]-&gt;getBalance();
        return result;
     }
     virtual void addToBalance(int amount) {
        cout &lt;&lt; "Error: Cannot modify balance of a parent account" &lt;&lt; endl;
        exit(1);
     }
  };
  class ChildAccount : public Account{
     int balance;
     virtual int getBalance() { return balance; }
     virtual void addToBalance(int amount) {balance += amount;}
  };
  </code></pre>
  
  <p>The idea is that which accounts are present is not known at compile time, so the tree account must be generated dynamically.  Inheritance is helpful here because it makes it easy to generate an arbitrarily deep tree structure (ParentAccounts can have children which are ParentAccounts), and because it makes it easy to implement functions like <code>getBalance()</code> using recursion.  </p>
  
  <p>Things get a bit awkward when I attempt to incorporate the features which are unique to the derived classes, such as modifying the balance (which should only be possible for <code>ChildAccount</code> objects, as <code>ParentAccount</code> balances are just defined by the balances of their children).  My plan is that a function like <code>processTransaction(string accountName, int amount)</code> would search through the tree structure looking for an account with the correct name, then call <code>addToBalance(amount)</code> on that account (*note below).  Since the tree structure above would only allow me to find an <code>Account*</code>, it would either be necessary to implement <code>addToBalance(amount)</code> for all classes, as I did above, or to <code>dynamic_cast</code> the <code>Account*</code> to a <code>ChildAccount*</code> before calling <code>addToBalance()</code>.  The first option seems slightly more elegant, but the fact that it requires me to define <code>ParentAccount::addToBalance()</code> (albeit as an error) seems kind of weird to me.</p>
  
  <p>My question is: Is there a name for this awkwardness, and a standard approach for resolving it, or am I just totally misapplying inheritance?  </p>
  
  <p><strong>*Note:</strong> I recognize there is probably a more efficient way of organizing the accounts for searching, but my primary objective is to create a program which is intuitive to interpret and debug.  Based on my current level of understanding, this comes at the price of computational efficiency (in this case at least).</p>