Creating instances of objects

N

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.
 
J

Jeff Schwab

nog said:
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 );
 
N

nog

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. :)
 
J

Jeff Schwab

nog said:
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.
 
D

David Harmon

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';
}
 
N

nog

--------------------------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. :)
 
D

Dylan Nicholson

nog said:
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
 
N

nog

------------------------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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top