# inheritance in perl

sub newaccount
{
   my $balance = $_[0];

   my $inquiry = sub { $balance };
   my $deposit = sub { $balance = $balance + $_[0]; };

   my $chargefee = sub { $balance -= 3; };
   my $withdraw = sub 
		  { $balance = $balance - $_[0]; &$chargefee(); };
  
   # return interface function:
   sub
   {
     my $method = $_[0]; # requested method
     if ($method eq withdraw) { return $withdraw; }
     if ($method eq deposit)  { return $deposit; }
     if ($method eq inquiry)  { return &$inquiry(); }
       else { die "error"; }
   }
}

# want savings account with an interestrate field.

sub newsavingsaccount
{
    my ($balance, $interest) = @_;
    my $super = newaccount($balance); # create superclass instance

    # trouble: want to define "addinterest"

    my $addinterest { $balance += $balance * $interest; }

    # the problem here is that the balance I'm changing is not
    # the same balance as in the $super object!

    # How do we get around this problem.  We can use a hash table
    # instead of an interface function, or we can add another method
    # int the superclass that allows one to change the balance.  In
    # either case, however, we'll in effect be making balance PUBLIC!
}

