473,545 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating instances of objects

nog

What's the best approach to creating many objects of a
class?
I have in mind using something analogous to a table to hold
the data - which is in a form similar to (char name, char
address, date joinDate) - and being able to declare each
row, one after another by looping.
With about 30 instances to create, I feel there must be a
better way than making an individual declaration for each
one.
TIA.
Jul 22 '05 #1
7 1635
nog wrote:
What's the best approach to creating many objects of a
class?
I have in mind using something analogous to a table to hold
the data - which is in a form similar to (char name, char
address, date joinDate) - and being able to declare each
row, one after another by looping.
With about 30 instances to create, I feel there must be a
better way than making an individual declaration for each
one.
TIA.


std::vector< Datum >( 30 );

Jul 22 '05 #2
nog
In article <q4************ ********@comcas t.com>,
je******@comcas t.net says...
nog wrote:
What's the best approach to creating many objects of a
class?
I have in mind using something analogous to a table to hold
the data - which is in a form similar to (char name, char
address, date joinDate) - and being able to declare each
row, one after another by looping.
With about 30 instances to create, I feel there must be a
better way than making an individual declaration for each
one.
TIA.


std::vector< Datum >( 30 );


Thanks for responding Jeff, although with my very limited
repertoire, this doesn't translate into a solution for me.
None of the vector examples I've looked at deal with
accommodating data of differing types in a row x column
fashion so, unfortunately it doesn't get me too far.

I've got several lines of data:

name1, address1, date1
name2, address2, date2,
.... etc.

and I want to use these to instantiate objects via the
constructor. All of this is quite new to me so seeing your
suggestion hasn't instantly lit any fires. :-)
Jul 22 '05 #3
nog wrote:
In article <q4************ ********@comcas t.com>,
je******@comcas t.net says...
nog wrote:
What's the best approach to creating many objects of a
class?
I have in mind using something analogous to a table to hold
the data - which is in a form similar to (char name, char
address, date joinDate) - and being able to declare each
row, one after another by looping.
With about 30 instances to create, I feel there must be a
better way than making an individual declaration for each
one.
TIA.


std::vector < Datum >( 30 );

Thanks for responding Jeff, although with my very limited
repertoire, this doesn't translate into a solution for me.
None of the vector examples I've looked at deal with
accommodating data of differing types in a row x column
fashion so, unfortunately it doesn't get me too far.

I've got several lines of data:

name1, address1, date1
name2, address2, date2,
... etc.

and I want to use these to instantiate objects via the
constructor. All of this is quite new to me so seeing your
suggestion hasn't instantly lit any fires. :-)

Sorry!

Often, related data of different types can be aggregated in a struct:

struct Person
{
Name name;
Address address;
Date date;
};

You can collect a bunch of struct's into a table using built-in C++ arrays:

Person people[ num_people ];

Or, you can use one of the standard library containers, like vector:

std::vector< Person > people( num_people );

Then, you can access any datum in the table like this:

std::cout << people[ 10 ].name << '\n';

To learn more about using tables (built-in arrays or std::vectors),
check out any C++ intro written in the past few years. There are plenty
of free tutorials on the web; Bruce Eckel's "Thinking in C++" is
supposed to be among the best.
Jul 22 '05 #4
On Tue, 17 Feb 2004 15:45:26 -0000 in comp.lang.c++, nog
<so************ @gmx.net> was alleged to have written:
None of the vector examples I've looked at deal with
accommodatin g data of differing types in a row x column
fashion so, unfortunately it doesn't get me too far.

I've got several lines of data:

name1, address1, date1


Create a class (or struct) with name, address, and date members.
Use that class (or struct) in your vector.

This should be covered in any C++ textbook, so I would hope examples
would not be too hard to find. Here's a small bit (warning:not tested.)

struct datum {
std::string name, address, date;
};
std::vector< Datum > table;

std::string nam, addr, dat;
while (std::cin >> nam >> addr >> dat) {
table.push_back (datum(nam, addr, dat));
};

for (int row=0; row < table.size(); ++row) {
std::cout << table[row].name << '\t'
<< table[row].address << '\t'
<< table[row].date << '\n';
}

Jul 22 '05 #5
nog
In article <cL************ ********@comcas t.com>,
je******@comcas t.net says...
--------------------------8<
Sorry!

Often, related data of different types can be aggregated in a struct:

struct Person
{
Name name;
Address address;
Date date;
};

You can collect a bunch of struct's into a table using built-in C++ arrays:

Person people[ num_people ];

Or, you can use one of the standard library containers, like vector:

std::vector< Person > people( num_people );

Then, you can access any datum in the table like this:

std::cout << people[ 10 ].name << '\n';

To learn more about using tables (built-in arrays or std::vectors),
check out any C++ intro written in the past few years. There are plenty
of free tutorials on the web; Bruce Eckel's "Thinking in C++" is
supposed to be among the best.


Thanks Jeff, I'll work on that and see how I get on. :-)
Jul 22 '05 #6
nog <so************ @gmx.net> wrote in message news:<MP******* *************** **@News.individ ual.DE>...
In article <q4************ ********@comcas t.com>,
je******@comcas t.net says...
nog wrote:
What's the best approach to creating many objects of a
class?
I have in mind using something analogous to a table to hold
the data - which is in a form similar to (char name, char
address, date joinDate) - and being able to declare each
row, one after another by looping.
With about 30 instances to create, I feel there must be a
better way than making an individual declaration for each
one.
TIA.


std::vector< Datum >( 30 );


Thanks for responding Jeff, although with my very limited
repertoire, this doesn't translate into a solution for me.
None of the vector examples I've looked at deal with
accommodating data of differing types in a row x column
fashion so, unfortunately it doesn't get me too far.

I've got several lines of data:

name1, address1, date1
name2, address2, date2,
... etc.

and I want to use these to instantiate objects via the
constructor. All of this is quite new to me so seeing your
suggestion hasn't instantly lit any fires. :-)


using namespace std; // just for clarity

struct Datum
{
string name1;
string address1;
string date1;
};

Then

vector<Datum> table;

Will create a variable-sized array of Datum objects. If you actually
want to initialize them at compile time with fixed values, that can be
done neatly by adding a constructor:

using namespace std;

struct Datum
{
Datum() { } // need this for vector<>
Datum(const string& name, const string& addr, const string& date) :
name1(name), address1(addr), date1(date) { }
string name1;
string address1;
string date1;
};

And do something like:

table.push_back (Datum("Mr Joe Bloggs", "4 City Rd", "5/10/1946"));
table.push_back (Datum("Mrs Jane Brown", "2/34 High St",
"7/3/1958"));

etc. etc.

But more likely you'd want to read these from a file, or from user
input, and how you do this depends largely on what file format you
want to use. However if you can write a function like this:

istream& operator>>(istr eam& stream, Datum& datum)
{
// read in the members - very trivial possibility is:
stream >> datum.name1 >> datum.address1 >> datum.date1;
return stream;
}

Then you can read in the whole vector like this:

(#include <iterator>)

void read_table(istr eam& stream, vector<Datum>& table)
{
table.insert(ta ble.end(), istream_iterato r<Datum>(stream ),
istream_iterato r<Datum>());

}

Unfortunately some older compilers won't support that last call to
insert - you could also try:

#include <algorithm>

void read_table(istr eam& stream, vector<Datum>& table)
{
copy(istream_it erator<Datum>(s tream), istream_iterato r<Datum>(),
inserter(table, table.end()));
}

Note that std::vector<> isn't very efficient for adding large amounts
of data to, unless you can roughly guess its likely maximum size
beforehand (and use reserve()). Other alternatives are std::list<>
and std::deque<>.

Hope this doesn't scare you off too much, but learning how to use the
standard containers, streams and algorithms to do basic everyday tasks
is vital to understanding the power of C++.

Dylan
Jul 22 '05 #7
nog
In article <7d428a77.04021 71350.3e017259
@posting.google .com>, wi******@hotmai l.com says...
------------------------8<
Hope this doesn't scare you off too much, but learning how to use the
standard containers, streams and algorithms to do basic everyday tasks
is vital to understanding the power of C++.


As I'm finding out. Thanks for that.
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
2539
by: Sean R. Lynch | last post by:
I've been playing around with Zope's RestrictedPython, and I think I'm on the way to making the modifications necessary to create a capabilities-based restricted execution system. The idea is to strip out any part of RestrictedPython that's not necessary for doing capabilities and do all security using just capabilities. The basic idea...
4
561
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum number of window handle, because if I reduce to about 2 instances of each window, it can run. But not 20 instances of each window. Does anyone know...
4
1298
by: Benjamin Lukner | last post by:
Hi! I had a problem for several days and now found out what it is caused by: When calling Sub New of a class under XP, on a specific line (getting IP address) the procedure was left immediately without raising an error. Break Points in code lines after the "x = new y" were ignored. Stepping through the code opens a message box on that...
12
3141
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without restarting the application. What I did was to create an AppDomain that loaded the plugins and everything was great, until I tried to pass...
9
2824
by: Daz | last post by:
Hello people! (This post is best viewed using a monospace font). I need to create a class, which holds 4 elements: std::string ItemName int Calories int Weight int Density
11
1361
by: JohnJSal | last post by:
It seems like what I want to do is something that programmers deal with everyday, but I just can't think of a way to do it. Basically, I am writing a DB front-end and I want a new "Researcher" object to be created whenever the user presses the "New Record" button. He can open as many new records at a time as he wants and fill in the...
26
5341
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... ...
2
3305
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until recently -- or maybe I should just speak for myself.) The fact that JSON is more elegant goes without saying, yet I can't seem to find a way to use JSON...
3
1466
by: Peter Morris | last post by:
Hi all Let's say I am creating a model to represent classes and properties. In addition to this I need instances of classes and values for those properties. KEY: (AssociationEndName)
0
7479
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...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5987
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...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
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...
0
3468
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...
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.