//---------------------------------------------------------------- // // point5.cpp // // written by: Simon Parsons // modified : 3rd October 2007 // //---------------------------------------------------------------- #include using namespace std; //---------------------------------------------------------------- // // Class definition // Class point represents a location in a set of cartesian coordinates. // // It has two data members, an x and a y value, and three constructors. // The constructors initialise the data members. class point { public: point() : x(0), y(0) { } // Default constructor point( double u ) : x(u), y(0) { } // Convert double to point point( double u, double v ) // Initialise both x and y if we are : x(u), y(v) { } // given values for them. void print() const; void set( double u, double v ); private: double x, y; }; // Member functions of point. Print the values and set the values. void point::print() const { cout << "(" << x << "," << y << ")\n"; } // end of print() void point::set( double u, double v ) { x = u; y = v; } // end of set() //---------------------------------------------------------------- // // The main program // This creates three instances of point, creating them in different ways, and // then printing out their contents. int main() { point a; point b(2.2); point c(2.3, 4.5); a.set( 1.2, 3.4 ); cout << "point a = "; a.print(); cout << "point b = "; b.print(); cout << "point c = "; c.print(); } // end of main()