basic classes question

Discussion in 'C++' started by pauldepstein@att.net, Dec 9, 2005.

  1. Guest

    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
    , Dec 9, 2005
    #1
    1. Advertising

  2. 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 (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.
    Neelesh Bodas, Dec 9, 2005
    #2
    1. Advertising

  3. Guest

    Neelesh Bodas wrote:
    > 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 (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
    , Dec 19, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jinho
    Replies:
    2
    Views:
    375
    Joseph Millar
    Aug 6, 2003
  2. Michael Gray

    basic classes manipulation

    Michael Gray, Aug 21, 2004, in forum: C++
    Replies:
    4
    Views:
    370
    Howard
    Aug 24, 2004
  3. Engineer
    Replies:
    6
    Views:
    614
    Jeremy Bowers
    May 1, 2005
  4. Replies:
    0
    Views:
    428
  5. August0866
    Replies:
    1
    Views:
    113
    Sebastian Hungerecker
    Mar 17, 2008
Loading...

Share This Page