Array of Objects with Constructors

This is a discussion on Array of Objects with Constructors within the C++ Programming forums, part of the General Programming Boards category; Hey, today I was doing some programming and I have come to a little confusion. An array of Objects. I ...

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Array of Objects with Constructors

    Hey, today I was doing some programming and I have come to a little confusion.
    An array of Objects.
    I get objects, and classes and even constructors. But I can't imagine an array of constructors with classes.
    e.g.
    Code:
    class Human
    {
     private: 
          float height, weight ; 
    
      public:
            Human(float,float) ;
            void setMeasurements(float,float)  ; 
    
    }
    Human::Human(float height, float weight) 
    {
       setMeasurements(height,weight) ; 
    }
    Human::setMeasureMents(float height,float weight) 
    {
        Human::height = height ; 
       Human::weight = weight ; 
    }
    int main(void) 
    {
            Human Eman(15.0f,20.0f) ; //Wow I get this part easy
    
           //when I create an object of userdefined type Human, a constructor is called
        //with 15 and 20 passed as arguments this I send to setM and so on....
        //but this I don't get  an array of object
    
        Human multipleMe[10] = {(20.0f,10.0f),(18.0f,20.0f)...}  ;
        
        i'm so confused :S , how is it that constructor parameters (don't know how to describe them)  is initialized as the values for multipleMe?  
           Thanks
    
           
            return 0 ; 
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    6,897
    To use the array initializer the proper syntax is
    Code:
        Human multipleMe[10] = { Human(20.0f,10.0f), Human(18.0f,20.0f), };
    That's really all there is to it. You would also have to call the constructor as opposed to simply using a variable name when you want the newly constructed object to exist only as long as it is being used or contained. That is a so called temporary object.
    Quote Originally Posted by phantomotap
    Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Adding objects to an array
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 08:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21