0
votes

Consider the following code for

class BankAccount
{
protected:
    int accNo;
    int balance;
    std::string custName;
    std::string custAddress;

public:
    BankAccount(int aNo, int bal,  std::string name, std::string address);//:accNo(aNo), balance(bal), custName(name), custAddress(address);
    BankAccount(const BankAccount&);
    BankAccount();
    ~BankAccount();
    BankAccount& operator=(const BankAccount&);

    int getAccNumber() const {return accNo;};
    virtual int getBalance() const {return balance;};
    std::string getAccountHolderName()const {return custName;};
    std::string getAccountHolderAddress()const {return custAddress;};
    virtual std::string getAccountType()const{return "UNKNOWN";};
};


class CurrentAccount:public BankAccount
{
private:
    int dailyTrancLimit;
public:
    CurrentAccount(int aNo, int bal,  std::string name, std::string address);
    int getTransactionLimit()const {return dailyTrancLimit;};
    void setTranscationLimit(int transLimit){dailyTrancLimit = transLimit;};
    std::string getAccountType()const{return "CURRENT";};

};

class SavingAccount:public BankAccount
{
private:
    int intrestRate;
    int accumuatedIntrest;
public:
    SavingAccount(int aNo, int bal,  std::string name, std::string address);
    int getBalance()const {return balance+accumuatedIntrest;};
    void setIntrestEarned(int intrest){accumuatedIntrest=intrest;};
    std::string getAccountType()const{return "SAVINGS";};

};

I want to call setIntrestEarned() in SavingAccount class with base class pointer. I don't want to add setIntrestEarned() as virtual in base class BankAccount because it has no meaning in the other type of accounts like derived one CurrentAccount.

And if we keep adding various function in different derived class as virtual in base class then it will end up like a superset of functions of derived class.

What would be the best way to design these type of class hierarchy?

1
Do some research about up-casting and down-casting.Some programmer dude
If you're in a situation where you know your objects of Base class are definitely instances of Derived class (which you have to know if you want to reliably call a method of the derived class on them), then you can cast them to the derived class to call on that.Zinki
@ Zinki Consider the following : void printAccDetails(BankAccount* const acc) { cout<<"\nBalance of \""<<acc->getAccountHolderName()<<"\" is: "<<acc->getBalance()<<" Acc Type: "<<acc->getAccountType(); //cout<<"\nTransaction Limit "<<acc->getTransactionLimit(); //Error here } Now I want to call this function with diff type (Current, Savings and bankAcc) of pointers. So in this function I will have to determine the type of acc and the print it details. How do I determine here ??? considering no function getAccountType();Rizwan
There is no automatic casting. You can use static_cast<T*>(p) if and only if you know for sure that the object pointed to by p is of type T. dynamic_cast<T*> will return a nullptr if the object pointed to is not of class T.Adrian W

1 Answers

1
votes

If it has no meaning in your base class, then you do not need to inherit from it.

Inheritance is only useful in the form where: B is a subset of A. B can have exclusive functions that A does not have.

Hence, if your savingsacc class requires certain information that A contains, then inherit it, and create exclusive functions for B that A does not need as C may also be a subset of A.