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

basic classes question

P: n/a
Suppose I have a class of objects which take an integer parameter.

I can easily create an object with the required parameter as follows:

name_of_class variable_name(integer_value);

For clarity, suppose I have a bank-account class and each object falls
into one of 5 distinct categories.

Then I might have code like:

bank_account object_1(5);

bank account object_2(3);
But if I have lots of objects of the class, I don't know what the
correct syntax is for doing such assignments (creating the objects) in
a loop.

Would the following work? (By using a giant leap of imagination, let
my_integer_valued_function denote an integer_valued function.)

object_array = new bank_account[n];

for (i=0; i<n; i++)

bank_account object_array[i] (my_integer_valued function of i);
Or perhaps it should be

object_array = new bank_account[n];

for (i=0; i<n; i++)

object_array[i] (my_integer_valued function of i);

Thanks for all your help and advice. I have received a lot of help
from this group.

Paul Epstein

Dec 9 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

pa**********@att.net wrote:
Suppose I have a class of objects which take an integer parameter.

I can easily create an object with the required parameter as follows:

name_of_class variable_name(integer_value);

For clarity, suppose I have a bank-account class and each object falls
into one of 5 distinct categories.

Then I might have code like:

bank_account object_1(5);

bank account object_2(3);
But if I have lots of objects of the class, I don't know what the
correct syntax is for doing such assignments (creating the objects) in
a loop.

Would the following work? (By using a giant leap of imagination, let
my_integer_valued_function denote an integer_valued function.)

object_array = new bank_account[n];

for (i=0; i<n; i++)

bank_account object_array[i] (my_integer_valued function of i);

No

Or perhaps it should be

object_array = new bank_account[n];

for (i=0; i<n; i++)

object_array[i] (my_integer_valued function of i);


No.
When you use operator new[], all objects get constructed using the
no-arg constructor.
If you want to construct all objects with a different initial value,
use array of pointers, and allocate in a loop.

const int n = 100;
bank_account* object_array[n];

for(int i=0; i < n; i++)
{
object_array[i] = new bank_account(i); // initailze with i.
}

Also, prefer std::vector over arrays.

Hope this helps.

Dec 9 '05 #2

P: n/a

Neelesh Bodas wrote:
pa**********@att.net wrote:
Suppose I have a class of objects which take an integer parameter.

I can easily create an object with the required parameter as follows:

name_of_class variable_name(integer_value);

For clarity, suppose I have a bank-account class and each object falls
into one of 5 distinct categories.

Then I might have code like:

bank_account object_1(5);

bank account object_2(3);
But if I have lots of objects of the class, I don't know what the
correct syntax is for doing such assignments (creating the objects) in
a loop.

Would the following work? (By using a giant leap of imagination, let
my_integer_valued_function denote an integer_valued function.)

object_array = new bank_account[n];

for (i=0; i<n; i++)

bank_account object_array[i] (my_integer_valued function of i);


No

Or perhaps it should be

object_array = new bank_account[n];

for (i=0; i<n; i++)

object_array[i] (my_integer_valued function of i);


No.
When you use operator new[], all objects get constructed using the
no-arg constructor.
If you want to construct all objects with a different initial value,
use array of pointers, and allocate in a loop.

const int n = 100;
bank_account* object_array[n];

for(int i=0; i < n; i++)
{
object_array[i] = new bank_account(i); // initailze with i.
}

Also, prefer std::vector over arrays.

Thanks.

I find const int n = 100 to be a bit inflexible.

I can't get this to work in the case of variable n. Since arrays need
to have constant integer bounds, it seems I need a pointer to a
pointer.

I somehow can't get this to work.

Thanks for your continued help.

Paul Epstein

Dec 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.