Pointers to Objects : Pointer Object « Pointer « C++






Pointers to Objects

Pointers to Objects

#include <iostream>
using namespace std;
class myclass {
  int i;
public:
  myclass(int j) { 
     i = j; 
  }
  int getInt() { 
     return i; 
  }
};
int main()
{
  myclass ob(88), *objectPointer;
  objectPointer = &ob;             // get address of ob
  cout << objectPointer->getInt();  // use -> to call getInt()
  return 0;
}


           
       








Related examples in the same category

1.Incrementing and decrementing an object pointer. Incrementing and decrementing an object pointer.
2.A base pointer to access derived objectsA base pointer to access derived objects
3.When a pointer is incremented, it points to the next element of its type