basic classes question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pauldepstein@att.net

    #1

    basic classes question

    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(i nteger_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_valu ed_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_val ued 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_val ued function of i);

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

    Paul Epstein

  • Neelesh Bodas

    #2
    Re: basic classes question


    pauldepstein@at t.net wrote:[color=blue]
    > 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(i nteger_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_valu ed_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_val ued function of i);
    >[/color]

    No
    [color=blue]
    >
    > Or perhaps it should be
    >
    > object_array = new bank_account[n];
    >
    > for (i=0; i<n; i++)
    >
    > object_array[i] (my_integer_val ued function of i);[/color]

    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.

    Comment

    • pauldepstein@att.net

      #3
      Re: basic classes question


      Neelesh Bodas wrote:[color=blue]
      > pauldepstein@at t.net wrote:[color=green]
      > > 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(i nteger_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_valu ed_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_val ued function of i);
      > >[/color]
      >
      > No
      >[color=green]
      > >
      > > Or perhaps it should be
      > >
      > > object_array = new bank_account[n];
      > >
      > > for (i=0; i<n; i++)
      > >
      > > object_array[i] (my_integer_val ued function of i);[/color]
      >
      > 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.
      >[/color]
      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

      Comment

      Working...