I am fairly new to C++, about 2 months. I created a small program, using polymorphism, that creates different types of accounts.
Saving
, Checking
, Trust
accounts derive from Account
(base class). My Account
base class is purely virtual.
Originally, I would create accounts like this: (name, balance, interest rate)
Account *sav = new Saving("Joe", 1000, 5);
Account *che = new Checking("Bob", 4000);
Account *tru = new Trust("Aaron", 6000, 4);
This is me statically allocating. I want to create a user-driven program where the user can create as many accounts as they want. How can I do that? How do I create pointers to that object and keep track of them so I can perform functions such as deposit
, withdraw
, modify
, print
, and so on?
I think I have to use the base class pointer and store it in a vector
, but I don't know how to do that.
Here is what I have tried to do:
vector<Account *> accounts;
accounts.push_back( new Savings(name, bal, interest_rate));
I can do this for other accounts as well.
where I can take those values from users with no problem. I stumble on how to access each individual account, though.
For example, if the user creates 5 accounts: 2 savings, 2 trusts, 1 checking. Now the user wants to deposit 1500
to the trust account with the name "John"
. How would I access that?