Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5-10. a. Modify the rectangle class from 5-8b (shown all the way below) so recta

ID: 3771412 • Letter: 5

Question

5-10.   a.   Modify the rectangle class from 5-8b (shown all the way below) so rectangles can be added or subtracted by overloading the + and - operators to mean adding the length and width of both rectangles or subtracting the lengths and widths of the second rectangle from the first rectangle.
   b.   Modify the rectangle class again so rectangles can be compared by overloading the comparison operators (==, !=, <, >) to compare the area of the two rectangles.

Use the following ex5-10.cpp and create the corresponding rectangle5-10.h header file. Submit the rectangle5-10.h with both parts a and b.

/* Bill Wohlleber
Exercise 5-10
This program will create a rectangle object
*/
#include<iostream>
#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();
}

/* Bill Wohlleber
Exercise 5-8b super
This class will define a rectangle
*/
#ifndef rectangle58b_h
#define rectangle58b_h
#include<iostream>
using namespace std;

class rectangle58b
{
protected:
   float length;
   float width;
   float area;
   float perimeter;

public:
   rectangle58b()
   {
       length = 1;
       width = 1;
       area = 1;
       perimeter = 4;
   }

   rectangle58b(float l, float w)
   {
       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

Explanation / Answer

In my opinion the sum and dif variable should be of rectangle type otherwise question doesnt make sense. Operator+ should add length and width of two rectangles and then what? I think its is asked to create a new rectangle. Hence data type of sum and dif should not be float. I have made some necessay changes in main code. Please say if this is acceptable


#ifndef rectangle510_h
#define rectangle510_h
#include<iostream>
using namespace std;
class rectangle510
{
protected:
float length;
float width;
float area;
float perimeter;
public:
rectangle510()
{
length = 1;
width = 1;
area = 1;
perimeter = 4;
}
rectangle510(float l, float w)
{
length = l;
width = w;
area = length * width;
perimeter = 2 * (length + width);
}
rectangle510 operator+(rectangle510 obj)
{
return rectangle510(length+obj.length,width+obj.width);
}
rectangle510 operator-(rectangle510 obj)
{
return rectangle510(length-obj.length,width-obj.width);
}
bool operator==(rectangle510 obj)
{
return (area==obj.area);
}
bool operator!=(rectangle510 obj)
{
return (area!=obj.area);
}

bool operator<(rectangle510 obj)
{
return (area<obj.area);
}
bool operator>(rectangle510 obj)
{
return (area>obj.area);
}

void print()
{
cout << "Length is " << length << endl;
cout << "Width is " << width << endl;
cout << "Area is " << area << endl;
cout << "Perimeter is " << perimeter << endl;
}
};
#endif


#include<iostream>
#include"rectangle5-10.h" // modified rectangle5-8b.h with operators overloaded
using namespace std;

int main()
{
rectangle510 r1(7, 5), r2(5, 3); // construct two rectangles
rectangle510 sum, dif; // use the overload operator + to add length and width from both rectangles ;
sum = r1 + r2;
dif = r1 - r2;
  
sum.print();
dif.print();

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 << "Rectangle 1 ";
r1.print();
cout << "Rectangle 2 ";
r2.print();
  
return 1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote