#include #include using namespace std; class point { public: //stuff that can be access from everywhere double getX() { return x; } double getY() { return y; } void setX(double x) { this->x = x; } void setY(double y) { this->y = y; } //operations double slope(point other) { return (y - other.y)/(x - other.x); } double intercept(double m) { return y - m*x; } double dist(point other) { double dx, dy; //distance in each dimension dx = x - other.x; dy = y - other.y; return sqrt(dx * dx + dy *dy); } point midpoint(point other) { point mp; mp.setX((x + other.x)/2.0); mp.setY((y + other.y)/2.0); return mp; } private: //stuff that only the class itself can access double x; double y; }; //overloaded insertion operator ostream & operator<<(ostream &os, point &p) { os << "(" << p.getX() << ", " << p.getY() << ")"; return os; } //overloaded extraction operator istream & operator>>(istream &is, point &p) { double x, y; is >> x; is >> y; p.setX(x); p.setY(y); return is; } int main(void) { point p1, p2, mp; double m; //get the points cout << "Enter point 1: "; cin >> p1; cout << "Enter point 2: "; cin >> p2; //test the slope m = p1.slope(p2); cout << "Slope: " << m << endl; //test the intercept cout << "Intercept: " << p1.intercept(m) << endl; //test the distance cout << "Distance: " << p1.dist(p2) << endl; //test the midpoint mp = p1.midpoint(p2); cout << mp << endl; return 0; }