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

In this C++ exercise, I need to first redefine the class rectangleType by declar

ID: 3708332 • Letter: I

Question

In this C++ exercise, I need to first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts 1 to 3.

1. 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 positive.)

2. 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.

3. 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. Similarly, 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.

4. Write the definitions of the functions to overload the operators defined in steps 1 through 3.

5. Write a test program that tests various operations on the class rectangleType.

Explanation / Answer

rectangleType.h

#ifndef RECTANGLETYPE_H

#define RECTANGLETYPE_H

#include<iostream>
using namespace std;
#include "rectangleType.h"
class rectangleType
{
   public:
       rectangleType();
       rectangleType(double _length,double _width);
       void setDimension(double _length,double _width);
       double getLength();
       double getWidth();
       double area();
       double perimeter();
       void print();
       rectangleType doubleDimensions();
rectangleType operator++();
       rectangleType operator--();
       rectangleType operator-(rectangleType& other);  
       bool operator==(rectangleType& other);
       bool operator!=(rectangleType& other);
       bool operator>(rectangleType& other);
       bool operator<(rectangleType& other);

       friend ostream& operator<<( ostream &output, const rectangleType &r );
       friend istream& operator>>( istream &input, rectangleType &r );
   private:
       double length;
       double width;
};

#endif // RECTANGLETYPE_H

rectangleType.cpp (Implementation class)

#include "rectangleType.h"

rectangleType::rectangleType()
{
}
rectangleType::rectangleType(double _length,double _width)
{
   length = _length;
   width = _width;
}
void rectangleType::setDimension(double _length,double _width)
{
   length = _length;
   width = _width;
}
//get length function
double rectangleType::getLength()
{
   return length;
}
//get width function
double rectangleType::getWidth()
{
   return width;
}
// area function
double rectangleType::area()
{
   double area;
   area = length*width;
   return area;
}
// perimeter function
double rectangleType::perimeter()
{
   double perimeter;
   perimeter = 2*(length+width);
   return perimeter;
}
// doubleDimensions function
rectangleType rectangleType::doubleDimensions()
{
   length = 2 * length;
   width = 2 * width;
   return *this;
}
//print function
void rectangleType::print()
{
   cout<<"Rectanlge Details: ";
   cout << "Length : " << length << " Width : " << width;
   cout << "Area : " << area() << " Perimeter : " << perimeter()<<endl;

}
//++ operator overloading
rectangleType rectangleType::operator++()
{
   length++;
   width++;
   return *this;
}
//-- operator overloading
rectangleType rectangleType::operator--()
{
   length--;
   width--;
   return *this;
}
//- operator overloading
rectangleType rectangleType::operator-(rectangleType& other)
{
   if(length<other.getLength() || width<other.getWidth()) {
       cout<<"Do not perform the operation";
       return *this;
   }
   else
   {
       length = length - other.getLength();
       width = width - other.getWidth();
       return *this;
   }  
}
//== operator overloading
bool rectangleType::operator==(rectangleType& other)
{
   if(area()==other.area())
       return true;
   else
       return false;
}
//!= operator overloading
bool rectangleType::operator!=(rectangleType& other)
{
   if(area()!=other.area())
       return true;
   else
       return false;
}
//> operator overloading
bool rectangleType::operator>(rectangleType& other)
{
   if(area()>other.area())
       return true;
   else
       return false;
}
//< operator overloading
bool rectangleType::operator<(rectangleType& other)
{
   if(area()<other.area())
       return true;
   else
       return false;
}

//Input and Output operator overloading
ostream & operator<<( ostream &output, const rectangleType &r )
{
   output<<"Rectanlge Details: ";
   output << "Length : " << r.length << " Width : " << r.width;
   return output;
}

istream &operator>>( istream &input, rectangleType &r )
{
   input >> r.length >>r.width;
return input;
}

main.cpp

#include <iostream>

#include <iomanip>
#include "rectangleType.h"
using namespace std;
int main()
{
   rectangleType oldYard(20.00, 10.00);
   rectangleType newYard;
   cout << fixed << showpoint << setprecision(2);
   cout << "Line 10: Area of oldYard = "<< oldYard.area() << endl;
   newYard = oldYard.doubleDimensions();
   cout << "Line 12: Area of newYard = " << newYard.area() << endl;
  
   //Incrementing oldYard using ++ operator overloading
   cout<<"Incrementing oldYard rectangle object : ";
   oldYard++;
   cout<<"Rectangle details after incrementing : ";
   //Print rectangle description using << operator overloading
   cout<<oldYard;
   cout << " Area of oldYard after incrementing= "<< oldYard.area() << endl;
   //compare rectangles using == operator overloading
   if(oldYard==newYard)
       cout<<"Two rectangles are the same ";
   else
       cout<<"Two rectangles are not the same ";
  
   system("pause");
   return 0;
}