//---------------------------------------------------------------- // // point-with-private.cpp // // Implements a point in two dimensional space. Based on the example // from Pohl, C++ by Dissection, pages 166-16. // // written by: Simon Parsons // modified : 20th February 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 { private: double x, y; // Data members public: void print() const; // Public functions to manipulate void set( double u, double v ); // the private data members. double getX(); double getY(); }; // Print the values void point::print() const { cout << "(" << x << "," << y << ")\n"; } // Set the values. void point::set( double u, double v ) { x = u; y = v; } // Accessor functions. We need these to get hold of x and y since they // are private. double point::getX(){ return x; } double point::getY(){ return y; } // End of point //---------------------------------------------------------------- // // Main program // This creates two instances of point, and then prints out their contents. int main() { point a, b; a.set(1.2, 3.4); cout << "point a = "; a.print(); b.set(4.5,5.6); cout << "point b = "; cout << b.getX() << " " << b.getY() << endl; } // end of main()