Back to notes -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    The class SavingsAccount.Lecture 15 - slide 10 : 13
Program 3
using System;

/// <summary> 
///   A specialization of BankAccount for saving purposes.
/// </summary>
public class SavingsAccount: BankAccount {

   /// <summary> A constructor that constructs a Savings account on the 
   /// basis of an owner and the interestrate. The balanced defaults
   /// to 0.0.</summary>
   /// <param name = "o"> The owners name </param>
   /// <param name = "ir"> The interestrate </param>
   public SavingsAccount(string o, double ir): 
     base(o, 0.0M, ir) {
   }

   /// <summary> A constructor that constructs a SavingsAccount on the 
   /// basis of an owner, balance, and the interestrate</summary>
   /// <param name = "o"> The owners name </param>
   /// <param name = "b"> The initial balance of the account </param>
   /// <param name = "ir"> The interestrate </param>
   public SavingsAccount(string o, decimal b, double ir): 
     base(o, b, ir) {
   }

   /// <summary> Withdraw an amount from the savings account </summary>
   /// <param name = "amount"> The amount withdrawn </param>
   /// <exception cref = "Exception"> In case of insufficient funds
   ///    an exception is thrown </exception>
   public override void Withdraw (decimal amount) {
      if (amount < balance)
          balance -= amount;
      else
          throw new Exception("Cannot withdraw");
   }

   /// <summary>
   ///   Add the annual interest to the savings account.
   ///   This may increase the current balance of the account.
   /// </summary>
   public override void AddInterests() {
      balance = balance + balance * (decimal)interestRate 
                        - 100.0M;
   }    

   /// <summary>
   ///   Return a text string that represents this account
   ///   for output purposes
   /// </summary>
   public override string ToString() {
      return owner + "'s check account holds " +
            + balance + " kroner";
   }
}