473,382 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1617
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********************@comcast.com>,
je******@comcast.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********************@comcast.com>,
je******@comcast.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
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


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********************@comcast.com>,
je******@comcast.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.individual.D E>...
In article <q4********************@comcast.com>,
je******@comcast.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>>(istream& 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(istream& stream, vector<Datum>& table)
{
table.insert(table.end(), istream_iterator<Datum>(stream),
istream_iterator<Datum>());

}

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

#include <algorithm>

void read_table(istream& stream, vector<Datum>& table)
{
copy(istream_iterator<Datum>(stream), istream_iterator<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.0402171350.3e017259
@posting.google.com>, wi******@hotmail.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
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...
4
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...
4
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...
12
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...
9
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
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"...
26
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...
2
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...
3
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.