-2

I am very confuse about vector of object in c++ could any one give me any tip to understand how to take objects as vector . my question is very very simple but i do not know why i am confuse.

Q.define a bank class, containing a vector of bank accounts, with methods to add a new account and to print all the accounts.

Add a method to deposit a specified amount into the account of a named customer, and another to similarly withdraw. Use your print method to test them.

please help me at start because i do not want lose the concept at the start.

Arshad
  • 43
  • 1
  • 2
  • 6
  • Provide what you have reached, by code. – herohuyongtao Jan 24 '14 at 14:31
  • Hint: `vector` has template support. – apnorton Jan 24 '14 at 14:31
  • `class bank_account {}; class bank { std::vector accounts; };` – user123 Jan 24 '14 at 14:31
  • 1
    If this is too much for you, go to the book list http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list and get one of the Beginner books. – pmr Jan 24 '14 at 14:32
  • 2
    `std::vector accounts;` is what you want. Your introductory book should describe how `vector` works; otherwise, there are online references like [this](http://en.cppreference.com/w/cpp/container/vector) and [this](http://www.cplusplus.com/reference/vector/vector/), but you're not going to get far without a [good book](http://stackoverflow.com/questions/388242). – Mike Seymour Jan 24 '14 at 14:32
  • If you want to be able to easily get the account of a named customer, this sounds more like the job for a std::map – Brian Schlenker Jan 24 '14 at 14:33
  • @BrianSchlenker And even more for a database – Sebastian Hoffmann Jan 24 '14 at 14:34
  • @Paranaix And even more like homework for the part of the course that introduces `vector`, where you would fail if you didn't follow the specification. – molbdnilo Jan 24 '14 at 14:46
  • @molbdnilo Hey I havent started with this :D I know the purpose of the assignment. Although if somebody already proposes a better implementation we should atleast provide the most common implementation – Sebastian Hoffmann Jan 24 '14 at 14:50

2 Answers2

1

Vector is a list of objects. To create a vector of bank accounts, you can go with :

vector<BankAccount> accountList;

The type between the <> (BankAccount) is to indicate the vector what type of object it must store.

To add an object to the vector, you have :

accountList.push_back(myBankAccount);

Where myBankAccount is an instanciation of the BankAccount class.

The documentation for the vector class is here : http://www.cplusplus.com/reference/vector/vector/

You just have to create a Bank object which owns a vector object, the latter storing the BankAccount objects.

Vulpo
  • 873
  • 1
  • 9
  • 25
  • 1
    1. I don't think `add` is valid syntax for vectors 2. You may want to define copy constructor if you care about deep/shallow copy. Vector to pointer to the objects may be better from a performance point of view. – Bill Jan 24 '14 at 14:46
  • @Bill Whether a vector of pointers is performance wise better depends much on how it is used and if the underlying object can be moved. With such a task which is just an exercise the performance aspect is completely meanigless (and even not answereable for us) and I would thus rate your suggestion as premature optimization (or deoptimization). One should usually always go with RAII if he doesnt have an important reason for pointers. – Sebastian Hoffmann Jan 24 '14 at 14:54
  • 1
    Right, I edited to correct the add problem. Of course storing pointers are better than storing objects, I wanted to keep it very simple but indeed, maybe I should not teach bad practices. – Vulpo Jan 24 '14 at 14:56
  • @Paranaix Yes, I agree to some extent. But, I wanted to point out the problem of deep and shallow copy when you assign objects in vectors. – Bill Jan 24 '14 at 14:56
0

I think this should be enough for you to "start":

struct Account
{
    std::string name;
};

struct Bank
{

    void Add(const Account& acc)
    {
        // insert into vector here, you better find out how to do this by your own
    }

private:
    std::vector<Account> m_accounts;

};
Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77