//ic210 sec 4002 //13 nov 2009 //structs and operator overload #include using namespace std; //define new type point struct point { double x, y; }; //don't forget the ; //void midpoint(double x1, double y1, double x2, double y2, double& , double&); point midpoint(point, point); point readpoint(istream&); void writepoint(ostream&, point); //compute sum point operator+(point, point); point operator/(point, int); int main() { //read point data (x, y) from console point p1, p2; p1.x = 6; p1.y = 5; //access the "data members" of point cout << "enter coordinates (x,y) for p1"; p1 = readpoint(cin); cout << "enter coordinates (x,y) for p2"; p2 = readpoint(cin); //add the two points together point p3; //p3 = sum(p1,p2); p3 = p1 + p2; //write the result cout << "p1 + p2 is "; writepoint(cout,p3); return 0; } point midpoint(point p1, point p2) { point middle; //syntactic sugar middle = (p1 + p2)/2; //middle.x = (p1.x + p2.x) /2; //middle.y = (p1.y + p2.y) /2; return middle; } //read point from stream; input format is (x,y) point readpoint(istream& IN) { point p; char junk; IN >> junk >> p.x >> junk >> p.y >> junk; return p; } //write point to stream using format (x,y) void writepoint(ostream& OUT, point p) { OUT << '(' << p.x << ',' << p.y << ')'; } point operator+(point p1, point p2) { point p3; p3.x = p1.x + p2.x; p3.y = p1.y + p2.y; return p3; } point operator/(point p, int i) { point result; result.x = p.x/i; result.y = p.y/i; return result; }