Thread: pointers, arrays, and inheretance....C++

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Question pointers, arrays, and inheretance....C++

    attached is a zip of my C++ program called Lab5. (also, there is an complete.exe file - this is the finished program that i have to duplicate.)

    I am seriously stuck on this assignment. the part that i am stuck on is on the first two options given to the user. the user is allowed to press 1 to create a checking account and 2 to create a savings account. the user creates two accounts and then either deposits/withdraws/showbalance/or transfers money of either account.

    the part that i'm stuck on is creating the checking or savings account object with the NEW operator and assigning it to an Array of pointers (which point to objects of the BankAccount class - which both Checking and Savings inherit from.) doing this, i'm allowed to use the functions of the BankAccount class, but not the Checkings or Savings classes. I've tried to use static_cast but it doesn't seem to work like i think it would. any help including simple comments on possibilites would greatly be appriciated. thanks.

    ~Benjamin

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    When working with inheritance and polymorphism, always remember to use the virtual identifier within the class headers and remeber to define your class destructors - even if you do not use them. Also, there is only a need to create a BankAccount pointer as each CAcct or SAcct is a BankAccount. Here are the files I have altered:

    Code:
    //////////////////////////////////////////////////////////
    // BankAccount.h
    //
    //
    //
    
    #ifndef BANKACCOUNT_H
    #define BANKACCOUNT_H
    
    class BankAccount
    {
    public:
        BankAccount( void );
        BankAccount( double );
        virtual ~BankAccount() {};
    
        int withdraw( double );
        int deposit( double );
        int transfer( BankAccount &, double );
        virtual void print( void );
    
    private:
        double balance;
    	
    }; // end BankAccount class
    
    #endif
    
    //////////////////////////////////////////////////////////
    // CAcct.h
    //
    // Checking Account Class Header File
    //
    
    #ifndef CACCT_H
    #define CACCT_H
    
    #include "BankAccount.h"
    
    class CAcct : public BankAccount
    {
    public:
        CAcct( void );
        virtual ~CAcct() {};
    
        virtual void print ( void );
    
    private:
        double fee;
    	
    }; // end CAcct class
    
    #endif
    
    //////////////////////////////////////////////////////////
    // SAcct.h
    //
    // Savings Account Class Header File
    //
    
    #ifndef SACCT_H
    #define SACCT_H
    
    #include "BankAccount.h"
    
    class SAcct : public BankAccount
    {
    public:
        SAcct( void );
        virtual ~SAcct() {};
    
        virtual void print ( void );
    
    private:
        double rate;
    	
    }; // end SAcct class
    
    #endif
    
    //////////////////////////////////////////////////////////
    // Test.cpp
    // Benjamin Comstock
    // C++
    // Lab 5
    // Bank Account Program
    // 11-08-01
    
    
    #include "CAcct.h"
    #include "SAcct.h"
    #include <iostream.h>
    
    // driver
    // entering main
    int main ( void )
    {
        // creating variables for program
        int choice = 0;    //choice var
        int GB = 0;
        BankAccount *aPtr[2];
    
        for (int i =0; i < 2; i++)
            aPtr[i] = NULL;    // make sure your pointers start with NULL values
    
        cout << "Enter (1) for Checking or (2) for Savings: ";
        cin >> choice;
    
        if (choice == 1)
        {
            aPtr[0] = new CAcct();    // assign pointer to a new CAcct object
            aPtr[0]->deposit(23.46);
            aPtr[0]->print();
    		
            GB = 1;
        }
        else if (choice == 2)
        {
            aPtr[1] = new SAcct();  // assign pointer to a new SAcct object
            aPtr[1]->print();
    
            GB = 2;
        }
        cout << "Good Bad reported: " << GB <<endl;
    
        // clean up the memory
        for (i =0; i < 2; i++)
        {
            if ( aPtr[i] != NULL)  // only delete if necessary
            {
                delete aPtr[i];
                aPtr[i] = NULL;
            }
        }
    
        return 0;
    }
    If you have any questions about this, let me know.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Smile

    Thank you very much for helping me out there. very appriciated.

    Ben


    still got some problems on some other parts of the program, but am going to work on them a bit longer before i give in and ask for help.

Popular pages Recent additions subscribe to a feed