vendredi 29 octobre 2021

How to use a child class method in an array of superclass type C++

I have a superclass bankAccount along with its multiple child classes. As my professors example shows, I created a dynamic array, bankAccount *accountList[10], then allow the user to select what type of account they want to access, which is then stored in accountList[i] =(whatever account type they chose). My problem arises when the user selects an option which isn't the superclass (bankAccount) and I need to do the corresponding methods, such as postInterest() as I get the error 'class bankAccount' has no member named 'postInterest' I've found 2 work arounds, the first being placing the methods (of the child classes) within the constructors, and while this methods gets me a correct output, if I try accessing these methods again in my main outside of the accountList[i] =savingsAccount(name, actN, bal, check) I get the same error. The second work around was using a static_cast, which has worked without fail so far. Is this the only way I can do this or am I missing something (On week 4 of C++ OOP and professor has skipped lectures for 2 weeks so concepts aren't too clear to me right now)

main.cpp

#include <iostream>
#include <string>
using namespace std;
#include "bankAccount.h"

#include "savingsAccount.h"


int main()
{
    string choice;
    int actN;
    double iR;
    double bal;
    double with;
    double dep;
    string name;
    bankAccount *accountList[10];
    for (int i=0; i<10; i++)
    {
        
        cout<<"Would you like to access Bank or Savings: ";
        cin >> choice;
        
        if (choice == "B"){
            cout << "Please enter Name: ";
            cin>>name;
            cout << "Please enter Account #: ";
            cin>>actN;
            
            cout<< "Please enter Balance: ";
            cin>> bal;
            
            cout << "How much would you like to withdrawl: ";
            cin >> with;

            cout << "How much would you like to Deposit: ";
            cin >> dep;
        
            accountList[i] = new bankAccount(name, actN, bal);
            accountList[i] -> setWithdrawl(with);
            accountList[i] -> setDeposit(dep);
            accountList[i] -> show();
    }
        if (choice == "S"){
            double intRate;
            cout << "Please enter Name: ";
            cin >> name;
            cout << "Please enter Account #: ";
            cin>>actN;
            
            cout<< "Please enter Balance: ";
            cin>> bal;
            
            cout << "How much would you like to withdrawl: ";
            cin >> with;
        
            cout << "How much would you like to Deposit: ";
            cin >> dep;
            
            cout << "Please enter Interest rate %: ";
            cin>> intRate;
            accountList[i] = new savingsAccount(name, actN, bal, intRate);
            accountList[i] -> setWithdrawl(with);
            accountList[i] -> setDeposit(dep);
            static_cast<savingsAccount*>(accountList[i]) -> postInterest();
            accountList[i] ->show();
        }
    }
}

bankAccount.h

#ifndef bankAccount_H
#define bankAccount_H
#include <iostream>
#include <string>

using namespace std;

class bankAccount{
    protected:
        double balance;
        int acctNumber;
        string name;
    public:
    bankAccount();

    
    bankAccount(string n, int aN, double bal);
    
    void setName(string n);
    
    void setAcctNum(int actNum);

    void setBal(double bal);
        
    double getBal();
    
    void setWithdrawl(double w);
    void setDeposit(double d);
    
    virtual void show();
};
#endif

bankAccount.cpp

#include "bankAccount.h"

bankAccount::bankAccount()
{
    acctNumber=0;
    balance = 0;
    name = "N/A";
}
    
    bankAccount::bankAccount(string n, int aN, double bal)
{
    setName(n);
    setAcctNum(aN);
    setBal(bal);
        
}
    
void bankAccount::setName(string n)
{
    name = n;
}

void bankAccount::setAcctNum(int actNum)
{
    acctNumber = actNum;
}

void bankAccount::setBal(double bal)
{
    balance = bal;
}

double bankAccount::getBal() 
{
    return balance;
}

void bankAccount::setWithdrawl(double w)
{
    balance = balance - w;
}

void bankAccount::setDeposit(double d)
{
    balance = balance + d;
}

void bankAccount::show()
{
    cout <<"Holder name: "<<name<<endl<< "Account number: "<< acctNumber<<endl <<"Balance: "<<balance<<endl<<endl;
}

savingsAccount.h

#ifndef savingsAccount_H
#define savingsAccount_H
#include <string>
#include "bankAccount.h"
  
using namespace std;

class savingsAccount :public bankAccount
{
    protected:
        double interestRate;
    public:
        savingsAccount();

        savingsAccount(string n,int aN, double bal, double iR);
        void setIR(double iR);

        void postInterest();
        virtual void show();
        
};
#endif

savingsAccount.cpp

#include "savingsAccount.h"

savingsAccount::savingsAccount(){
    interestRate = 0;
}
savingsAccount::savingsAccount(string n,int aN, double bal, double iR) : bankAccount (n,aN, bal)
{
    setIR(iR);
    //postInterest(); I know this is commented out, just to give a reference to what I was talking about in my post
}

void savingsAccount:: setIR(double iR)
{
    interestRate = iR/100;
}

void savingsAccount:: postInterest()
{
    balance *=(1+interestRate/12);
}

void savingsAccount::show()
{
    cout<<"Account Number: "<<acctNumber<<endl<<"Interest Rate: "<<interestRate<<endl<<"Final Balance: "<< balance<<endl<<endl;
}

P.S. this is my first stack overflow post! So any tips on making my post easier to answer would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire