User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 361,894 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,239 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 5051 | Replies: 6
Reply
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Help Help : Pointers to array of class objects .

  #1  
Jun 7th, 2005
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.


class Rectangle
{

public :

int side1 , side2;
Rectangle(int,int);
Rectangle();
void get_area();

private :
protected :

};

Rectangle::Rectangle(int x, int y)
{

side1=x;
side2=y;

}


void Rectangle::get_area()
{
  cout<<"Area :"<< side1*side2<<endl;
}


Rectangle *ptr_arr;
 ptr_arr=new Rectangle[2] ;                 // pointer to an array of objects to the class

 // 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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2005
Location: Kolkata
Posts: 32
Reputation: amt_muk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Re: Help : Pointers to array of class objects .

  #2  
Jun 7th, 2005
Hi Varun,
Just try this:

Rectangle *ptr_arr[2];
ptr_arr[0] = new Rectangle(10,20);
ptr_arr[1] = new Rectangle(15,5);
Reply With Quote  
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Re: Help : Pointers to array of class objects .

  #3  
Jun 8th, 2005
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 ???
Reply With Quote  
Join Date: May 2005
Location: Kolkata
Posts: 32
Reputation: amt_muk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Re: Help : Pointers to array of class objects .

  #4  
Jun 8th, 2005
Rectangle * ptr_arr;
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
ptr_arr[0] = new Rectangle(10,20);
ptr_arr[1] = new Rectangle(15,5);
Reply With Quote  
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Re: Help : Pointers to array of class objects .

  #5  
Jun 8th, 2005
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
Reply With Quote  
Join Date: May 2005
Location: Kolkata
Posts: 32
Reputation: amt_muk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Re: Help : Pointers to array of class objects .

  #6  
Jun 8th, 2005
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
Reply With Quote  
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Re: Help : Pointers to array of class objects .

  #7  
Jun 8th, 2005
Thannxxxx amt_muk ..... :-)
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:20 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC