Friday 17 April 2015

Array of Pointers to Objects


An array of pointers to objects is often used to handle a group of objects, which need not necessarily reside contiguously in memory, as in the case of static array of objects. The syntax for defining an array of pointers to objects is the same as any of the fundamental types.

Sample Program

#include <iostream.h>
class ap
{
int data;
static int count;
public:
ap(void)
{
data = ++count;
}
void show(void)
{
cout<<”Data “<<endl;
}
};
void main()
{
ap *obj[10];
cout<<”Enter the number of objects to create”<<endl;
int no;
cin>>no;
for(int i=0;i<no;i++)
obj[i]->setcount();
for(int i=0;i<no;i++)
obj[i]->show();
}

No comments:

Post a Comment