#include <math.h> #include <iostream.h> class Coordinate { public: Coordinate(); // default constructor Coordinate(int xValue, int yValue, int displayValue); Coordinate(int xValue, int yValue); void setX(int xValue); void setY(int yValue); void setDisplay(int dValue); int getX() const; int getY() const; int getDisplay() const; friend float distance(Coordinate p1, Coordinate p2); friend ostream & operator << (ostream & oStream, const Coordinate & aPoint); private: int x; int y; int display; }; istream & operator >> (istream & iStream, Coordinate & aPoint);
#include "Coordinate.h" #include <math.h> #include <iostream.h> float distance(Coordinate p1, Coordinate p2) { int deltaX = p1.x - p2.x; int deltaY = p1.y - p2.y; return sqrt(deltaX*deltaX + deltaY*deltaY); // sqrt in math.h } ostream & operator << (ostream & oStream, const Coordinate & aPoint) { oStream << aPoint.x << " " << aPoint.y << " " << aPoint.display; return oStream; } istream & operator >> (istream & iStream, Coordinate & aPoint) { int x,y,display; iStream >> x >> y >> display; aPoint.setX(x); aPoint.setY(y); aPoint.setDisplay(display); return iStream; }
#include "Coordinate.h" void main() { Coordinate point1; cout << point1.x; }