Chapter 13 Lab Assignment Is this program typed and running correctly? The quest
ID: 3583838 • Letter: C
Question
Chapter 13 Lab Assignment
Is this program typed and running correctly?
The question is:
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.
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.)
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.
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.
Write the definitions of the functions to overload the operators defined in parts a to c.
Write a test program that tests various operations on the class rectangleType.
The code i have typed is:
//#include "stdafx.h"
#include <iostream>
#include <cassert>
using namespace std;
#define frz system("pause");
class rectangleType
{
//Overload the stream insertion and extraction operators
friend ostream& operator<<(ostream&, const rectangleType &);
friend istream& operator>>(istream&, rectangleType &);
public:
void setDimension(double l, double w);
double getLength() const;
double getWidth() const;
double area() const;
double perimeter() const;
//Overload the decrement operators
rectangleType operator -- (); //pre-decrement
rectangleType operator -- (int); //post-decrement
//Overload the relational operators
bool operator <= (const rectangleType&) const;
bool operator < (const rectangleType&) const;
bool operator >= (const rectangleType&) const;
bool operator > (const rectangleType&) const;
//Constructors
rectangleType();
rectangleType(double l, double w);
private:
double length;
double width;
};
void rectangleType::setDimension(double l, double w)
{
if (l >= 0)
length = l;
else
length = 0;
if (w >= 0)
width = w;
else
width = 0;
}
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);
}
rectangleType::rectangleType(double l, double w)
{
setDimension(l, w);
}
rectangleType::rectangleType()
{
length = 0;
width = 0;
}
/**********************************************************************
Code the operator overload function definitions for pre and post
decrement and the relational operators prototyped in the class
definition here.
***********************************************************************/
bool rectangleType::operator <= (const rectangleType& rectangle) const
{
return ( rectangle.area() <= rectangle.area());
}
bool rectangleType::operator < (const rectangleType& rectangle) const
{
return ( rectangle.area() < rectangle.area());
}
bool rectangleType::operator >= (const rectangleType& rectangle) const
{
return ( rectangle.area() >= rectangle.area());
}
bool rectangleType::operator > (const rectangleType& rectangle) const
{
return ( rectangle.area() > rectangle.area());
}
rectangleType rectangleType::operator -- ()
{
++length;
++width;
return *this;
}
rectangleType rectangleType::operator -- ( int u)
{
rectangleType temp = *this;
length++;
width++;
return temp;
}
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()
{
rectangleType rectangle1(6, 4);
rectangleType rectangle2(10, 15);
rectangleType rectangle3;
rectangleType rectangle4;
cout << "Rectangle1: " << rectangle1 << endl;
cout << "Rectangle2: " << rectangle2 << endl;
if (rectangle1 <= rectangle2)
cout << "Rectangle1 less than or equal"<< " to rectangle2." << endl;
else
cout << "Rectangle1 greater than rectangle2." << endl;
if (rectangle1 < rectangle2)
cout << "Rectangle1 less than rectangle2." << endl;
else
cout << "Rectangle1 greater or equal to rectangle2." << endl;
cout << "Set both the length and width of rectangle3 to 5." << endl;
cin >> rectangle3;
cout << endl << "Rectangle3: " << rectangle3 << endl;
rectangle3--;
cout << "After decrement the length and width of "<< "rectangle3 by one unit, rectangle3: "<<rectangle3<< endl;
rectangle4 = --rectangle3;
cout << "New dimension of rectangle3: " << rectangle3 << endl;
cout << "New dimension of rectangle4: " << rectangle4 << endl;
rectangle4 = rectangle3--;
cout << "New dimension of rectangle3: " << rectangle3 << endl;
cout << "New dimension of rectangle4: " << rectangle4 << endl;
if (rectangle4 >= rectangle3)
cout <<"Rectangle4 greater than or equal to rectangle3." << endl;
else
cout <<"Rectangle4 less than rectangle3." << endl;
if (rectangle4 > rectangle3)
cout << "Rectangle4 greater than rectangle3." << endl;
else
cout << "Rectangle4 less than or equal to rectangle3." << endl;
frz;
return 0;
}
But I'm not sure if the output is correct. The reason i dont think it is correct is because it's saying that my Rectangle1 is greater than Rectangle2 when clearer it's not.
Explanation / Answer
/*in operator overloading for relational operators, you are comparing rectangle.area() with rectangle.area() instead of comparing present area(), I changed some statements, you can find my changes with comment "Added by chegg EA.. Below is the modified program.. Rate my answer if you like*/
#include <iostream>
#include <cassert>
using namespace std;
#define frz system("pause");
class rectangleType
{
//Overload the stream insertion and extraction operators
friend ostream& operator<<(ostream&, const rectangleType &);
friend istream& operator>>(istream&, rectangleType &);
public:
void setDimension(double l, double w);
double getLength() const;
double getWidth() const;
double area() const;
double perimeter() const;
//Overload the decrement operators
rectangleType operator -- (); //pre-decrement
rectangleType operator -- (int); //post-decrement
//Overload the relational operators
bool operator <= (const rectangleType&) const;
bool operator < (const rectangleType&) const;
bool operator >= (const rectangleType&) const;
bool operator > (const rectangleType&) const;
//Constructors
rectangleType();
rectangleType(double l, double w);
private:
double length;
double width;
};
void rectangleType::setDimension(double l, double w)
{
if (l >= 0)
length = l;
else
length = 0;
if (w >= 0)
width = w;
else
width = 0;
}
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);
}
rectangleType::rectangleType(double l, double w)
{
setDimension(l, w);
}
rectangleType::rectangleType()
{
length = 0;
width = 0;
}
/**********************************************************************
Code the operator overload function definitions for pre and post
decrement and the relational operators prototyped in the class
definition here.
***********************************************************************/
bool rectangleType::operator <= (const rectangleType& rectangle) const
{
//Changed by Chegg EA we need to compare this object area with rectangle area
//return (rectangle.area() <= rectangle.area());
return (area() <= rectangle.area());
}
bool rectangleType::operator < (const rectangleType& rectangle) const
{
//return (rectangle.area() < rectangle.area());
//chenged by chegg EA
return (area() < rectangle.area());
}
bool rectangleType::operator >= (const rectangleType& rectangle) const
{
//return (rectangle.area() >= rectangle.area());
//chenged by chegg EA
return (area() >= rectangle.area());
}
bool rectangleType::operator > (const rectangleType& rectangle) const
{
//return (rectangle.area() > rectangle.area());
//chenged by chegg EA
return (area() > rectangle.area());
}
rectangleType rectangleType::operator -- ()
{
//changed by chegg EA to decreament instead of increament
--length;
--width;
return *this;
}
rectangleType rectangleType::operator -- (int u)
{
rectangleType temp = *this;
//changed by chegg EA to decreament instead of increament
length--;
width--;
return temp;
}
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()
{
rectangleType rectangle1(6, 4);
rectangleType rectangle2(10, 15);
rectangleType rectangle3;
rectangleType rectangle4;
cout << "Rectangle1: " << rectangle1 << endl;
cout << "Rectangle2: " << rectangle2 << endl;
if (rectangle1 <= rectangle2)
cout << "Rectangle1 less than or equal" << " to rectangle2." << endl;
else
cout << "Rectangle1 greater than rectangle2." << endl;
if (rectangle1 < rectangle2)
cout << "Rectangle1 less than rectangle2." << endl;
else
cout << "Rectangle1 greater or equal to rectangle2." << endl;
cout << "Set both the length and width of rectangle3 to 5." << endl;
cin >> rectangle3;
cout << endl << "Rectangle3: " << rectangle3 << endl;
rectangle3--;
cout << "After decrement the length and width of " << "rectangle3 by one unit, rectangle3: " << rectangle3 << endl;
rectangle4 = --rectangle3;
cout << "New dimension of rectangle3: " << rectangle3 << endl;
cout << "New dimension of rectangle4: " << rectangle4 << endl;
rectangle4 = rectangle3--;
cout << "New dimension of rectangle3: " << rectangle3 << endl;
cout << "New dimension of rectangle4: " << rectangle4 << endl;
if (rectangle4 >= rectangle3)
cout << "Rectangle4 greater than or equal to rectangle3." << endl;
else
cout << "Rectangle4 less than rectangle3." << endl;
if (rectangle4 > rectangle3)
cout << "Rectangle4 greater than rectangle3." << endl;
else
cout << "Rectangle4 less than or equal to rectangle3." << endl;
frz;
return 0;
}
------------------------------------------------------------------------------------------------------
output:
Rectangle1: Length = 6 Width = 4
Rectangle2: Length = 10 Width = 15
Rectangle1 less than or equal to rectangle2.
Rectangle1 less than rectangle2.
Set both the length and width of rectangle3 to 5.
5 5
Rectangle3: Length = 5 Width = 5
After decrement the length and width of rectangle3 by one unit,
rectangle3: Length = 4 Width = 4
New dimension of rectangle3: Length = 3 Width = 3
New dimension of rectangle4: Length = 3 Width = 3
New dimension of rectangle3: Length = 2 Width = 2
New dimension of rectangle4: Length = 3 Width = 3
Rectangle4 greater than or equal to rectangle3.
Rectangle4 greater than rectangle3.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.