Question: 5-10. a. Modify the rectangle class from 5-8b (shown at the bottom) so rectangles can be added or subtracted by overloading the + and -
5-10. a. Modify the rectangle class from 5-8b (shown at the bottom) so rectangles can be added or subtracted by overloading the + and - operators to mean adding or subtracting the lengths and widths.
b. Modify the rectangle class again so rectangles can be compared by overloading some of the comparison operators (=, !=, <, >, <=, >=) to compare the area (or perimeter). Overload the operators used below.
Use the following ex5-10.cpp and create the corresponding rectangle5-10.h header file. Overload the operators used below. Add documentation. Submit the rectangle5-10.h /*
Exercise 5-10 This program will create a rectangle object */ #include #include"rectangle5-10.h" // modified rectangle5-8b.h with operators overloaded using namespace std; void main() { rectangle510 r1(7, 5), r2(5, 3); // construct two rectangles float sum, dif; // use the overload operator + to add length and width from both rectangles ; sum = r1 + r2; dif = r1 - r2; if (r1 > r2) // use the overload operator > to compare areas cout << "r1 has a greater area "; else if (r1 < r2) cout << "r1 has a less area "; else if (r1 == r2) cout << "r1 and r2 have the same area "; if (r1 != r2) cout << "Not equal "; cout << "Sum of two rectangles is " << sum << endl; cout << "Difference of two rectangles is " << dif << endl; cout << "Rectangle 1 "; r1.print(); cout << "Rectangle 2 "; r2.print(); }
//Rectangle class to be modified
#ifndef rectangle58b_h // if not defined (by multiple includes), define it #define rectangle58b_h #include using namespace std; class rectangle58b { private: float length; float width; float area; float perimeter; public: rectangle58b() // no argument constructor { length = 1; width = 1; area = 1; perimeter = 4; } rectangle58b(float l, float w) // overloaded constructor { length = l; width = w; area = length * width; perimeter = 2 * (length + width); } void print() { cout << "Length is " << length << endl; cout << "Width is " << width << endl; cout << "Area is " << area << endl; cout << "Perimeter is " << perimeter << endl; } }; #endif