Question
I'm sorry...hopefully this is the information that you need. The part that I'm having trouble with is in bold. This chapter uses the class rectangleType
I'm sorry...hopefully this is the information that you need. The part that I'm having trouble with is in bold.
This chapter uses the class rectangleType to illustate how to overload the operators +, *, ==, !=, >>, and <<. In this exercise, first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts a to c.
a. Overload the pre- and post-increment and decrement operators to increment and decrement, respectively, the length and width of a rectangle by one unit. (Note that after decrementing the length and width, they must be postive.)
b. Overload the binary operator - to subtract the dimensions of one rectangle from the corresponding dimensions of another rectangle. If the resulting dimensions are not positive, output an appropriate message and do not perform the operation.
c. The operators == and != are overloaded by considering the lengths and widths of rectangles. Redefine the functions to overload the relational operator by considering the areas of rectangles as follows: Two rectangles are the same, if they have the same area; otherwise, the rectangles are not the same. Similary, rectangle yard1 is greater than rectangle yard2 if the area of yard1 is greater than the area of yard2. Overload the remaining relational operators using similar definitions.
d. Write the definitions of the functions to overload the operators defined in parts a to c.
e. Write a test program that tests various operations on the class rectangleType.
I have a question for the solution Chapter 13, Problem 1PE. Part (b) asks to output a message and NOT perform the subtraction calculation if the resulting numbers are not positive. This is not implemented within the code, and I'm having major problems doing so. Can someone add this to the Chapter 13 Problem 1PE solution along with an explanation? I've provided my code below (assuming that is the details needed). In case statement 2, if the number is negative after the calculation, it shouldn't output anything. As the code stands now, it will still subtract. Any way to resolve this?
#include
#include
#include
using namespace std;
class rectangleType {
friend ostream& operator << (ostream&, const rectangleType&);
friend istream& operator >> (istream&, rectangleType&);
public:
void setDimension(double l, double w);
double getWidth() const;
double getLength() const;
double perimeter() const;
double area() const;
rectangleType operator++();
rectangleType operator--();
rectangleType operator++(int);
rectangleType operator--(int);
rectangleType operator + (const rectangleType&) const;
rectangleType operator - (const rectangleType&) const;
rectangleType operator * (const rectangleType&) const;
bool operator == (const rectangleType&) const;
bool operator != (const rectangleType&) const;
bool operator > (const rectangleType&) const;
bool operator < (const rectangleType&) const;
bool operator >= (const rectangleType&) const;
bool operator <= (const rectangleType&) const;
//Implementing constructors
rectangleType();
rectangleType(double l, double w);
protected:
double length;
double width;
};
//Overloading stream insertion operator
ostream& operator << (ostream& osObject, const rectangleType& rectangle) {
osObject << "Length = " << rectangle.length << ", width = " << rectangle.width;
return osObject;
}
istream& operator >> (istream& isObject, rectangleType& rectangle) {
isObject >> rectangle.length >> rectangle.width;
return isObject;
}
int main() {
char stop = 'Y';
cout << "Welcome to the Overloading Operators Program" << endl << endl;
rectangleType frstYard(10, 20);
rectangleType secYard;
int selection;
cout << "A given rectangle has the following values:" << endl;
cout << frstYard << endl << endl;
cout << "Please enter the length and width of the rectangle: " << endl;
cout << "(Be sure to enter a space between the two values)" << endl;
cin >> secYard;
cout << endl;
cout << "The rectangle values that you've entered (in yards) is: " << secYard << endl << endl;
while (toupper(stop) == 'Y') {
//Menu screen for the user
cout << "** MENU SELECTION**" << endl << endl;
cout << "1.) Add dimensions" << endl;
cout << "2.) Subtract dimensions" << endl;
cout << "3.) Multiply dimensions" << endl;
cout << "4.) Determine if dimensions are equal" << endl;
cout << "5.) Determine if dimensions are not equal" << endl;
cout << "6.) Determine if given rectangle is <" << endl;
cout << "7.) Determine if given rectangle is >" << endl;
cout << "8.) Determine if given rectangle is <=" << endl;
cout << "9.) Determine if given rectangle is >=" << endl;
cout << "10.) Pre-increment" << endl;
cout << "11.) Post increment" << endl;
cout << "12.) Predecrement" << endl;
cout << "13.) Post decrement" << endl;
cout << "14.) Exit" << endl;
cout << endl;
cout << "Enter select one of the options from the menu: ";
cin >> selection;
if (selection > 91)
selection -= 32;
switch (selection) {
case 1: cout << " The sum of the dimensions of the rectangles is: " << frstYard + secYard;
cout << endl;
cout << endl;
break;
case 2: cout << " The difference of the dimensions rectangles is: " << frstYard - secYard;
cout << endl;
cout << endl;
break;
case 3: cout << " The product of the dimensions of the rectangles is: " << frstYard * secYard;
cout << endl;
cout << endl;
break;
case 4: cout << " Are the rectangles equal?: ";
if (frstYard == secYard)
cout << " Yes";
else
cout << " No";
cout << endl;
cout << endl;
break;
case 5: cout << " Are the rectangles not equal?: ";
if (frstYard != secYard)
cout << " The rectangles are NOT equal.";
else
cout << " the rectangles ARE equal";
cout << endl;
cout << endl;
break;
case 6: cout << " Is the given rectangle smaller than your rectangle?: ";
if (frstYard < secYard)
cout << " Yes, it is smaller than my rectangle.";
if (frstYard == secYard)
cout << " N/A...the rectangles are equal.";
if (frstYard > secYard)
cout << " No, it is larger than my rectangle.";
cout << endl;
cout << endl;
break;
case 7: cout << " Is the given rectangle larger than your rectangle?: ";
if (frstYard > secYard)
cout << " Yes it is larger than my rectangle.";
if (frstYard == secYard)
cout << " N/A...the rectangles are equal.";
if (frstYard < secYard)
cout << " No, it is smaller than my rectangle.";
cout << endl;
cout << endl;
break;
case 8: cout << " Is the given rectangle less than or equal to your rectangle?: ";
if (frstYard <= secYard)
cout << " Yes";
else
cout << " No";
cout << endl;
cout << endl;
break;
case 9: cout << " Is the given rectangle greater than or equal to your rectangle?: ";
if (frstYard >= secYard)
cout << " Yes";
else
cout << " No";
cout << endl;
cout << endl;
break;
case 10:
cout << " My rectangle, pre-incremented, is " << ++secYard << endl;
cout << endl;
break;
case 11:
cout << " My rectangle, post incremented, is " << secYard++ << endl;
cout << endl;
break;
case 12:
cout << " My rectangle, pre-decremented, is " << --secYard << endl;
cout << endl;
break;
case 13:
cout << " My rectangle, post decremented, is " << secYard-- << endl;
cout << endl;
break;
}
cout << "Would you like to make another menu seletion (Y/N)?: ";
cin >> stop;
}
cout << " Thank you for using the Overloading Operators Program!" << endl;
cout << endl;
system("pause");
return 0;
}
//Preincrementing function definition
rectangleType rectangleType::operator++() {
++length;
++width;
return *this;
delete this;
}
//Post incrementing function definition
rectangleType rectangleType::operator++(int n) {
rectangleType temp(*this);
length++;
width++;
return *this;
delete this;
}
//Predecrementing function definition
rectangleType rectangleType::operator--() {
--length;
--width;
return *this;
delete this;
}
//Post decrementing function definition
rectangleType rectangleType::operator--(int n) {
rectangleType temp(*this);
length--;
width--;
return *this;
delete this;
}
//Operator + function definition
rectangleType rectangleType::operator + (const rectangleType& rectangle) const {
return rectangleType(length + rectangle.getLength(), width + rectangle.getWidth());
}
//Operator - function definition
rectangleType rectangleType::operator - (const rectangleType& rectangle) const {
return rectangleType(length - rectangle.getLength(), width - rectangle.getWidth());
}
//Operator * function definition
rectangleType rectangleType::operator * (const rectangleType& rectangle) const {
return rectangleType(length * rectangle.getLength(), width * rectangle.getWidth());
}
//Operator == function definition
bool rectangleType::operator == (const rectangleType& rectangle) const {
if ((length == rectangle.getLength()) && (width == rectangle.getWidth()))
return true;
else
return false;
}
//Operator <= function definition
bool rectangleType::operator <= (const rectangleType& rectangle) const {
if ((length <= rectangle.getLength()) && (width <= rectangle.getWidth()))
return true;
else
return false;
}
//Operator >= function definition
bool rectangleType::operator >= (const rectangleType& rectangle) const {
if ((length >= rectangle.getLength()) && (width >= rectangle.getWidth()))
return true;
else
return false;
}
//Operator != function definition
bool rectangleType::operator != (const rectangleType& rectangle) const {
if ((length != rectangle.getLength()) && (width != rectangle.getWidth()))
return true;
else
return false;
}
//Operator < function definition
bool rectangleType::operator < (const rectangleType& rectangle) const {
if ((length < rectangle.getLength()) && (width < rectangle.getWidth()))
return true;
else
return false;
}
//Operator > function definition
bool rectangleType::operator > (const rectangleType& rectangle) const {
if ((length > rectangle.getLength()) && (width > rectangle.getWidth()))
return true;
else
return false;
}
double rectangleType::getLength() const {
return length;
}
double rectangleType::getWidth() const {
return width;
}
double rectangleType::area() const {
return (length * width);
}
double rectangleType::perimeter() const {
return (2 * (length + width));
}
void rectangleType::setDimension(double l, double w) {
length = l;
width = w;
}
//Implementing default constructor
rectangleType::rectangleType() {
setDimension(0, 0);
}
//Implementing constructor with parameters
rectangleType::rectangleType(double l, double w) {
setDimension(l, w);
}
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