//---------------------------------------------------------------- // // point6.cpp // // written by: Simon Parsons // modified : 17th 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 ) { assert(u > 0); // Here is where we use assert assert(v > 0); // to check for errors. 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; 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()