// More client code that uses a point class defined in point.h. This program // overrides the < operator so that it can sort a vector of points by their // distance from the origin. #include #include #include #include "point.h" using namespace std; // print the contents of the vector to cout void print(const vector & v) { for (const point & p : v) { cout << p << " (" << p.distance_from_origin() << ")" << endl; } cout << endl; } // overloaded < operator comparing distance from origin of the two points bool operator<(const point & lhs, const point & rhs) { return lhs.distance_from_origin() < rhs.distance_from_origin(); } int main() { point p1(12, 14); point p2(7, 18); point p3(13, 5); cout << "p1 distance from origin = " << p1.distance_from_origin() << endl; cout << "p2 distance from origin = " << p2.distance_from_origin() << endl; cout << "p3 distance from origin = " << p3.distance_from_origin() << endl; cout << endl; cout << "p1 = " << p1 << endl; cout << "p2 = " << p2 << endl; cout << "p3 = " << p3 << endl; cout << endl; vector v; v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(point(6, 6)); cout << "before sort:" << endl; print(v); cout << "after sort:" << endl; sort(v.begin(), v.end()); print(v); return 0; }