943,973 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 19721
  • C++ RSS
Jun 7th, 2005
0

Help : Pointers to array of class objects .

Expand Post »
Hi All


I am relatively new to the C++ world . Facing some problem , while initializing an array of objects .



Suppose i have a class Rectangle . I want to have a pointer to an array of objects to the class . How do i invoke the Class Constructor , for a paricular object in the arraylist.


C++ Syntax (Toggle Plain Text)
  1. class Rectangle
  2. {
  3.  
  4. public :
  5.  
  6. int side1 , side2;
  7. Rectangle(int,int);
  8. Rectangle();
  9. void get_area();
  10.  
  11. private :
  12. protected :
  13.  
  14. };
  15.  
  16. Rectangle::Rectangle(int x, int y)
  17. {
  18.  
  19. side1=x;
  20. side2=y;
  21.  
  22. }
  23.  
  24.  
  25. void Rectangle::get_area()
  26. {
  27. cout<<"Area :"<< side1*side2<<endl;
  28. }
  29.  
  30.  
  31. Rectangle *ptr_arr;
  32. ptr_arr=new Rectangle[2] ; // pointer to an array of objects to the class
  33.  
  34. // Now how do i initialize the dimensions for the ptr_arr[1] object , using the constructors ???
<< moderator edit: added [code][/code] tags >>


Pplz Help


Best Regards

Varun
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpta_varun is offline Offline
24 posts
since May 2005
Jun 7th, 2005
0

Re: Help : Pointers to array of class objects .

Hi Varun,
Just try this:

C++ Syntax (Toggle Plain Text)
  1. Rectangle *ptr_arr[2];
  2. ptr_arr[0] = new Rectangle(10,20);
  3. ptr_arr[1] = new Rectangle(15,5);
Reputation Points: 14
Solved Threads: 3
Light Poster
amt_muk is offline Offline
48 posts
since May 2005
Jun 8th, 2005
0

Re: Help : Pointers to array of class objects .

Thnx a lot Sir !!!



That definately worked ......But just wanna know thw diff b/n

Approach 1
---------------

Rectangle * ptr_arr;
ptr_arr=new Rectangle[2];


Approach 2
---------------

Rectangle * ptr_arr[2];
ptr_arr[0]=new Rectangle;
ptr_arr[1]=new Rectangle;


Are the two approaches same ???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpta_varun is offline Offline
24 posts
since May 2005
Jun 8th, 2005
0

Re: Help : Pointers to array of class objects .

C++ Syntax (Toggle Plain Text)
  1. Rectangle * ptr_arr;
  2. ptr_arr=new Rectangle[2];
The above code creates an array of 2 Rectangles by calling the constructor:
Rectangle::Rectangle();
But if u want to call the constructor:
Rectangle::Rectangle(int x, int y)
u should use
C++ Syntax (Toggle Plain Text)
  1. ptr_arr[0] = new Rectangle(10,20);
  2. ptr_arr[1] = new Rectangle(15,5);
Reputation Points: 14
Solved Threads: 3
Light Poster
amt_muk is offline Offline
48 posts
since May 2005
Jun 8th, 2005
0

Re: Help : Pointers to array of class objects .

Actually what i meant was , that is there any diff b//n the memory allocation / meathod scheme of the two ..leave alone the first query of constructors


The first approach shows an array of objects being pointed to ..by the ptr_arr pointer .
Does the second one , signify a pointer array or an object array initialization ....

Wat's the difference . (just suppose that , no constructor is involved0
I hope i am clear .....



Best Regards
Varun
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpta_varun is offline Offline
24 posts
since May 2005
Jun 8th, 2005
0

Re: Help : Pointers to array of class objects .

Look Varun, the main difference between these 2 is that the 1st one allocates the memory at the time of running (dynamic memory allocation). But the 2nd one does the same at both compile and run time. First it creates memory at the time of compilation (ie, static memory allocation) for the line :
Rectangle * ptr_arr[2];
and after that it again creates the memory at run time for :
ptr_arr[0]=new Rectangle;
ptr_arr[1]=new Rectangle;
So I think if there is no specific reason, the 2nd approach should be avoided.

Amit
Reputation Points: 14
Solved Threads: 3
Light Poster
amt_muk is offline Offline
48 posts
since May 2005
Jun 9th, 2005
0

Re: Help : Pointers to array of class objects .

Thannxxxx amt_muk ..... :-)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpta_varun is offline Offline
24 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: WriteLine Specifyng what line to write in a textfile
Next Thread in C++ Forum Timeline: Printing a Queue to file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC