Description
The purpose of this challenge is to define basic classes with friend functions and operator overloading
Requirements
- Define a class called Point as below
class Point { private: double x, y; public: };
- Write an overloaded constructor for the Point class to set the values for the x and y data members.
- Overload the less-than operator (<) to move the x-value n units to the left.
// Sample usage Point p(5,5); p = p < 2; // After the above line of code, the x value has moved to the left 2 units (x is now 3)
- Overload the greater-than operator (>) to move the x-value n units to the right.
// Sample usage Point p(10,5); p = p > 3; // After the above line of code, the x value has moved to the right 3 units (x is now 13)
- Understand that the overloads in Steps 3 and 4 aren’t recommended approaches as we are repurposing and redefining the behaviors of the less-than and greater-than operators, which should behave more or less as comparison operators. Nevertheless, we will implement them in this example to show and understand proof of concept.
- Overload the asterisk operator (the * used for multiplying) so that two Point objects “multiplied” together returns the area bounded by the two points.
// Sample usage Point p1(2,2), p2(5,5); double area = p1 * p2; cout << area << endl; // should show 9
- Overload the insertion operator (the << used by cout). This will allow you to simply cout any Point objects. When a Point object is displayed, it will look like (x,y).
- In main(), write driver code that will instantiate some Point objects to test your overloads.
Sample main()
// class definitions here int main() { Point p1(5,5), p2(10,10); p1 = p1 < 1; // p1 is now (4,5) p2 = p2 > 3; // p2 is now (13,10) cout << p1 << endl; // shows (4,5) cout << p2 << endl; // shows (13,10); double area = p1 * p2; cout << "Area bounded by p1 and p2 is " << area << endl; // 45 return 0; }
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL0072
Print Requirements