0

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I think this would be useful: https://stackoverflow.com/q/388242/2079303 – eerorika Jul 17 '20 at 02:15
  • 1
    Simply have `Account` expose a public method to get the account's name, then loop through the vector until you find the `Account` with the desired name, and then you can `deposit()` into that account. – Remy Lebeau Jul 17 '20 at 03:39

1 Answers1

0

Sounds like you need to create a couple of classes, you need to create a class for savings, trust and checkings account.. In your customer class, you would need three vectors one for your savings, one for trust, and one checkings account. You would then push them to your vector.

Yunfei Chen
  • 630
  • 1
  • 8
  • 20
  • To: Yunfei Chen Thanks but no, I have created saving, account, trust, checking, I_Printable cpp and header files. IDK if I should post my program in the question because, its about 300 lines of code. I was wondering if there is a way to use my base pointer, which is account, and put it vector but how would keep track of each account? In order to deposit and withdraw etc. – Ajay Tiwari Jul 17 '20 at 18:39
  • Although I don't like it you can have the account being a parent class and have the other three being child classes and get inheritance. – Yunfei Chen Jul 17 '20 at 19:10
  • What do you not like about it? I learned new concept, polymorphism. I wanted to apply that concept by creating a program to better understand it. Am I messing up something that I don't know? – Ajay Tiwari Jul 17 '20 at 21:34
  • Well it is less effecient then using a vector, also, it is determined during runtime, we want to make as much of our variables known during compile time as possible..... For the sake of effeciency.... – Yunfei Chen Jul 17 '20 at 22:07