I have this code to represent bank:
class Bank {
friend class InvestmentMethod;
std::vector<BaseBankAccount*> accounts;
public:
//...
BaseBankAccount is an abstract class for all accounts in a bank:
class BaseBankAccount {
public:
BaseBankAccount() {}
virtual int getInterest() const = 0;
virtual int getInvestedSum() const = 0;
virtual void increaseDepositSum(int additionalSum) = 0;
virtual void close(std::string& reason) = 0;
virtual ~BaseBankAccount() {}
};
The problem is, when I manipulate with pointers to derived class objects through pointers to base class objects, the set of methods I can call is restricted by BaseBankAccount public interface - no matter what type the REAL object is.
For example, not every account has an option to increase sum invested already - so, I didn`t include this method in a base class:
class BankAccount: public BaseBankAccount {
protected:
BaseDeposit* deposit;
double sumInvested;
public:
BankAccount(int sum, int term, int inter): sumInvested(sum), depositTerm(term), interest(inter) {}
int getInterest() const { return interest; }
int getInvestedSum() const { return sumInvested; }
void prolong(int increaseTerm) {
depositTerm += increaseTerm;
}
void increaseInvestment(double addition) {
sumInvested += addition;
}
virtual ~BankAccount() {}
};
then, I want to call it:
Bank bank1(...);
bank1.accounts[i]->increaseInvestment(1000.0);
So, what can I do to get access to the interface of derived class objects in this case? As far as I know, downcasting to concrete type each time I need to call specific functionality is not good.
Create one more abstract class derived from this to expand the interface?
Create parallel hierarchy for each specific type I need to implement (looks a bit heavy thing to do)?