0

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?

9
  • 1
    Do some research about up-casting and down-casting. Commented Jul 23, 2018 at 6:44
  • 2
    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. Commented Jul 23, 2018 at 6:46
  • 1
    dynamic_cast<SavingAccount*>() is your friend Commented Jul 23, 2018 at 6:53
  • @ 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(); Commented Jul 23, 2018 at 6:57
  • 1
    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. Commented Jul 23, 2018 at 9:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.