array of pointers to objects
on declaring an array of pointers to objects does the program allocate memory for 100 persons, or only allocates them upon
Code:
persPtr[n] = new person;
? having a hard time understanding the operator new.
Code:
///////////////////////////////////////
class person //class of persons
{
protected:
char name[40]; //persons name
public:
void setName() //set the name
{
cout << "Enter name: ";
cin >> name;
}
void printName() //get the name
{
cout << "\n Name is: " << name;
}
};
///////////////////////////////////////
int main()
{
person* persPtr[100]; //array of pointers to persons
int n = 0; //number of persons in array
char choice;
do
{
persPtr[n] = new person; //make new object
persPtr[n]->setName(); //set persons name
n++; //count new person
cout << "Enter another (y/n)? "; //enter another?
cin >> choice;
} while( choice == 'y' ); //quit on 'n'
for(int i = 0; i < n; i++)
{
cout << "\nPerson number " << i+1;
persPtr[i]->printName();
}
cout << endl;
return 0;
} //end main()