//---------------------------------------------------------------- // // point5.cpp // // written by: Simon Parsons // modified : 3rd October 2007 // // Illustrating the use of assert. // //---------------------------------------------------------------- #include #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 ) { try{ if(u < 0){ throw u; } else x = u; } catch(double u){ cout << "That value of x is no good" << endl; cout << "I'm setting x to zero" << endl; x = 0; } } // 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; double p; double q; cout << "Enter an x-coordinate "; cin >> p; cout << "Enter a y-coordinate "; cin >> q; a.set( p, q ); cout << "point a = "; a.print(); } // end of main()