basic classes question

P

pauldepstein

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 (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 (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
 
N

Neelesh Bodas

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 (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 (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 = new bank_account(i); // initailze with i.
}

Also, prefer std::vector over arrays.

Hope this helps.
 
P

pauldepstein

Neelesh said:
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 (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 (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 = 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
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top