array of pointers to objects : object pointer « Pointer « C++ Tutorial






#include <iostream>  
  using namespace std;  

  class person{  
     protected:  
        char name[40]; 
     public:  
        void setName(){  
           cout << "Enter name: ";  
           cin >> name;  
        }  
        void printName()
        {  
           cout << "\n   Name is: " << name;  
        }  
    };  

  int main()  
    {  
     person* persPtr[100];
     int n = 0;           
     char choice;  
    
     do{  
        persPtr[n] = new person;         
        persPtr[n]->setName();           
        n++;                             
        cout << "Enter another (y/n)? "; 
        cin >> choice;                   
     }while( choice=='y' );                
    
     for(int j=0; j<n; j++){
        cout << "\nPerson number " << j+1;  
        persPtr[j]->printName();  
     }  
     cout << endl;  
     return 0;  
    }








11.7.object pointer
11.7.1.Pointers to Objects
11.7.2.uses a pointer to access all three elements of array ob after being assigned ob's starting address
11.7.3.assign the address of a public member of an object to a pointer and then access that member by using the pointer
11.7.4.dereferencing the pointer returned by new
11.7.5.array of pointers to objects