By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

object members in vector

P: n/a
Okay guys,

I got my vector to work but how do I do the following:

1. I create a bank account object.
2. I place that bank account object in the vector
3. I have a loop that repeats steps 1 and 2

My question is.....how do I get the vector to have objects of the same
type, but with different names?

I want account1 in element 0
account2 in element 1
And so on until the user finally decides that they want to break out of
the loop.

Aug 4 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
nick wrote:
I got my vector to work but how do I do the following:

1. I create a bank account object.
2. I place that bank account object in the vector
3. I have a loop that repeats steps 1 and 2

My question is.....how do I get the vector to have objects of the same
type, but with different names?
They already have different names. If your vector is known as 'v', then
the first object is 'v[0]', the second is 'v[1]', and so on.

I want account1 in element 0
account2 in element 1
And so on until the user finally decides that they want to break out
of the loop.


That I don't understand. If you want to give your accounts some names,
then you should declare a member variable 'name' or sometning in each
account object.

V
Aug 4 '05 #2

P: n/a
Okay, I think I see your point. So let me rewrite it and tell me if I'm
on the right track....
I create an object called bankAccount. I activate all the member
variables and functions for it. So let's say that it has a deposit
amount of 1200. That bankAccount object goes into vector position[0]. I
stay in my loop and it creates another bankAccount object with, let's
say, a 300 deposit. That goes into my vector at position[1].

So I then have X amount of bankAccount objects in my vector with
different deposit amounts. So I need to create a loop to add elements
to the vector just like a for loop with arrays?

Aug 4 '05 #3

P: n/a
nick wrote:
Okay, I think I see your point. So let me rewrite it and tell me if I'm
on the right track....
I create an object called bankAccount. I activate all the member
variables and functions for it. So let's say that it has a deposit
amount of 1200. That bankAccount object goes into vector position[0]. I
stay in my loop and it creates another bankAccount object with, let's
say, a 300 deposit. That goes into my vector at position[1].

So I then have X amount
... X number ..
of bankAccount objects in my vector with
different deposit amounts. So I need to create a loop to add elements
to the vector just like a for loop with arrays?


I suppose... A standard container 'vector' is essentially a wrapper
around its C++ array of data, with the ability to add to that array (the
"regular" C++ array doesn't allow that) and remove from it. A 'vector'
behaves similarly to the other standard sequential containers, 'list' and
'deque', with a couple of exceptions (lacking a better word).

You were asking about the names of those objects. I am not sure what you
need those names for, but often in banking every account has a number and
an associated customer name. With that information at hand you can either
look up the account (find it in the container) by the number or by the
name, provided that the lookup functionality exists. You don't have to
assign any other "number" than the position in the vector (and the lookup
functionality for it already exists in the indexing operator, []), but you
could add the functionality to look the accounts up by the customer name
as well...

V
Aug 4 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.