Thread: array of pointers to objects

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    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()
    Last edited by xion; 02-07-2005 at 03:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Creating an array of pointers to int[]
    By OkashiiKen in forum C Programming
    Replies: 3
    Last Post: 09-29-2006, 06:48 PM
  5. Replies: 4
    Last Post: 10-16-2003, 11:26 AM