473,804 Members | 3,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

object members in vector

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
3 1849
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2139
by: Jessica | last post by:
Hi, I have a question regarding the object-oriented issue. I understand that a class is a type. I have an array class. Now say that I want to implement an algorithm A that uses the array class. Does it make sense to make a class for the algorithm such as class MyAlg { private: Array A1;
21
2532
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default constructors class MyClass { Foo foo; // foo and bar require default constructors Bar bar; public:
9
2088
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
4
2396
by: ahrimen | last post by:
Hi, First I'll state my over all goal = a text based game with several rooms that have several exits as my first real program that I've done without the help of a book. I can make a normal vector object out of a custom class, I can even use a single object of a class that has vector data members, but If my vector object of a custom class has a vector data member my program crashs not when I instaitant it, but if I try calling any of the...
23
1977
by: sidney | last post by:
Hi all, I am trying to make a vector containing objects the have a reference member. However, as soon as I try to push_back an element into this vector, g++ balks at the fact that it needs to instantiate an operator= on the class containing the reference members (see below for what I try to do). #include <vector>
14
1886
by: Glen Dayton | last post by:
While looking at some old code I ran across a snippet that increments a pointer to access different members of a structure: .... struct Badness { std::string m_begin; std::string m_param1; std::string m_param2; std::string m_end;
4
3717
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
12
6257
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is: gcc version 4.0.1 (Apple Inc. build 5465). thanks for the help. summary of compile error: --------------------------------------- cpp.C:4: error: non-static const member 'const unsigned int A::u',
12
2727
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10564
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.