Question
Four doubles are read from input, where the first two doubles are the length and width of rectangle1 and the second two doubles are the
Four doubles are read from input, where the first two doubles are the length and width of rectangle1 and the second two doubles are the length and width of rectangle2. Define two functions to overload the - operator. The first function overloads the - operator to subtract a rectangle and a double representing the length of the rectangle. The second function overloads the - operator to subtract two rectangles.
Ex: If the input is 19.0 12.0 5.0 3.5, then the output is:
Length: 19 units, width: 12 units Length: 5 units Difference: Length: 14 units, width: 12 units Length: 19 units, width: 12 units Length: 5 units, width: 3.5 units Difference: Length: 14 units, width: 8.5 units
Note: The difference of a rectangle and a double representing the length of the rectangle is:
the difference of the length of the rectangle and the double
the width of the rectangle is unchanged
Note: The difference of two rectangles is:
the difference of the lengths of the rectangles
the difference of the widths of the rectangles
Code:
#include
class Rectangle { public: Rectangle(double length = 0.0, double width = 0.0); void Print() const; Rectangle operator-(double rhs); Rectangle operator-(Rectangle rhs); private: double numLength; double numWidth; };
Rectangle::Rectangle(double length, double width) { numLength = length; numWidth = width; }
// No need to accommodate for overflow or negative values
/* Your code goes here */ // DO NOT CHANGE ANY OTHER CODE (JUST ADD HERE)
void Rectangle::Print() const { cout << "Length: " << numLength << " units, width: " << numWidth << " units"; }
int main() { double length1; double width1; double length2; double width2; cin >> length1; cin >> width1; cin >> length2; cin >> width2; Rectangle rectangle1(length1, width1); Rectangle rectangle2(length2, width2); Rectangle difference1 = rectangle1 - length2; Rectangle difference2 = rectangle1 - rectangle2; rectangle1.Print(); cout << endl; cout << "Length: " << length2 << " units" << endl; cout << "Difference: "; difference1.Print(); cout << endl; cout << endl; rectangle1.Print(); cout << endl; rectangle2.Print(); cout << endl; cout << "Difference: "; difference2.Print(); cout << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started