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:
If you have any questions about this, let me know.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; }
David