//----------------------------------------------------------------- // // pointers.cpp // // Illustrating basic things about pointers // // written by: Simon Parsons // modified : 5th March 2010 // //----------------------------------------------------------------- #include using namespace std; //---------------------------------------------------------------- // // point // The class point represents a location in a set of cartesian coordinates. // // It has two data members, an x and a y value. class point { public: double x, y; void print(); void set(double, double); }; // Print the values void point::print(){ cout << "(" << x << "," << y << ")\n"; } // Set the values. void point::set( double u, double v ) { x = u; y = v; } // End of point //---------------------------------------------------------------- // // Main program int main() { // Declare an integer an integer array, and a pointer to an integer. int a; int b[5] = {1, 2, 3, 4, 5}; int* aptr; // Make the pointer point to the integer and then use it to manipulate // the value of the integer. aptr = &a; *aptr = 6; cout << "a has value: " << a << endl; (*aptr)++; cout << "a has value: " << a << endl; *aptr = *aptr + 1; cout << "a has value: " << a << endl; // New use a pointer to an object point p; point* pptr; // Make the pointer point to the object pptr = &p; // Access the members of the object (*pptr).set(1.2,3.4); pptr->print(); } // end of main()