Comp 2673
Spring 2002
Homework 4 solutions

  1. The leak is in the "read" function. In the line
    maze = new char[num_rows*num_cols];     // allocate space for the maze
    
    the pointer "maze" is made to point to a new dynamically allocated character array, but if there already was a maze (for example, if this is the second time we're calling the "read" function), then we should release that old memory first. To fix it, right before the memory allocation, insert the line
    if (maze) delete [] maze;
    
  2. class BankAccount {
    public:
        BankAccount();
        BankAccount(int, int, float=0);
        bool deposit(float amt);  // returns true if deposit was made
        bool withdraw(float amt);  // returns true if withdrawal was made
        float get_balance() const;
        int get_account_num() const;
    private:
        int account_num;
        int account_holder;
        float balance;
    };
    class CheckingAccount: public BankAccount {
    public:
        CheckingAccount();
        CheckingAccount(int, int, float=0, float=0, float=0);
        bool charge_fee();  // return false if couldn't charge fee
        float get_min_balance() const;
        float get_service_fee() const;   
        void set_min_balance(float new_balance);
        void set_service_fee(float new_service_fee);
    private:
        float minimum_balance;
        float service_fee;
    };
    class SavingsAccount: public BankAccount {
    public:
        SavingsAccount();
        SavingsAccount(int, int, float=0, float=0);
        float get_interest_rate() const;
        void set_interest_rate(float new_interest_rate);
        void add_interest();  // compute interest and add it to balance
    private:
        float interest_rate;
    };
    
  3. Results will vary.