In: Computer Science
Overload the following operators: -
Operators |
Description |
- (unary) |
Length and width will change sign |
+ |
Add bother areas and then find the ratio of the first Rectangle to find the new values of the length and the width |
- |
Subtract second area from first area and then find the ratio of the first Rectangle to find the new values of the length and the width |
* |
Multiply first area by second area and then find the ratio of the first Rectangle to find the new values of the length and the width |
/ |
Divide first area by second area and then find the ratio of the first Rectangle to find the new values of the length and the width |
< |
Compare the areas |
> |
Compare the areas |
<= |
Compare the areas |
>= |
Compare the areas |
== |
Compare the areas |
!= |
Compare the areas |
= |
Assign the length and width to the first Rectangle |
+= |
Same as + operator and assign the result to the first Rectangle |
-= |
Same as - operator and assign the result to the first Rectangle |
*= |
Same as * operator and assign the result to the first Rectangle |
/= |
Same as / operator and assign the result to the first Rectangle |
// C++ program to create the class Rectangle
#include <iostream>
#include <cmath>
using namespace std;
const double EPSILON = 1e-5; // used for comparison
template <class T>
class Rectangle
{
private :
T length, width;
public:
void setLength(T length);
void setWidth(T width);
T perimeter() const;
T area() const;
void show() const;
bool sameArea(const Rectangle<T> &other) const;
void operator-();
Rectangle<T>& operator+(const Rectangle<T> &other);
Rectangle<T>& operator-(const Rectangle<T> &other);
Rectangle<T>& operator*(const Rectangle<T> &other);
Rectangle<T>& operator/(const Rectangle<T> &other);
bool operator<(const Rectangle<T> &other) const;
bool operator>(const Rectangle<T> &other) const;
bool operator<=(const Rectangle<T> &other) const;
bool operator>=(const Rectangle<T> &other) const;
bool operator==(const Rectangle<T> &other) const;
bool operator!=(const Rectangle<T> &other) const;
void operator=(const Rectangle<T> &other);
void operator+=(const Rectangle<T> &other);
void operator-=(const Rectangle<T> &other);
void operator*=(const Rectangle<T> &other);
void operator/=(const Rectangle<T> &other);
};
// function to set the length of rectangle
template <class T>
void Rectangle<T>:: setLength(T length)
{
this->length = length;
}
// function to set the width of rectangle
template <class T>
void Rectangle<T>::setWidth(T width)
{
this->width = width;
}
// function to return the perimeter of rectangle
template <class T>
T Rectangle<T>::perimeter() const
{
return(length+width);
}
// function to return the area of rectangle
template <class T>
T Rectangle<T>::area() const
{
return(length*width);
}
// function to display the length and width of rectangle
template <class T>
void Rectangle<T>:: show() const
{
cout<<"Rectangle of length : "<<length<<" and width : "<<width<<endl;
}
// function to return if the two rectangles have same area
template <class T>
bool Rectangle<T>::sameArea(const Rectangle<T> &other) const
{
return(fabs(area()-other.area()) < EPSILON);
}
// function to decrement the length and width of the rectangle by 1
template <class T>
void Rectangle<T>::operator-()
{
length = -1*length;
width = -1*width;
}
// function to add both areas and then find the ratio of the first Rectangle to find the new values of the length and the width
template <class T>
Rectangle<T>& Rectangle<T>::operator+(const Rectangle<T> &other)
{
Rectangle<T> *result = new Rectangle<T>();
T req_area = area()+other.area();
T ratio = area()/req_area;
result->length = result->length*ratio;
result->width = result->width*ratio;
return result;
}
// function to subtract both the second area from the first and then find the ratio of the first Rectangle to find the new values of the length and the width
template <class T>
Rectangle<T>& Rectangle<T>::operator-(const Rectangle<T> &other)
{
Rectangle<T> *result = new Rectangle<T>();
T req_area = fabs(area()-other.area());
T ratio = area()/req_area;
result->length = result->length*ratio;
result->width = result->width*ratio;
return result;
}
// function to multiply both areas and then find the ratio of the first Rectangle to find the new values of the length and the width
template <class T>
Rectangle<T>& Rectangle<T>::operator*(const Rectangle<T> &other)
{
Rectangle<T> *result = new Rectangle<T>();
T req_area = area()*other.area();
T ratio = area()/req_area;
result->length = result->length*ratio;
result->width = result->width*ratio;
return result;
}
// function to divide the first area by the second and then find the ratio of the first Rectangle to find the new values of the length and the width
template <class T>
Rectangle<T>& Rectangle<T>::operator/(const Rectangle<T> &other)
{
Rectangle<T> *result = new Rectangle<T>();
T req_area = area()/other.area();
T ratio = area()/req_area;
result->length = result->length*ratio;
result->width = result->width*ratio;
return result;
}
// function to return if first rectangle is less in area than second rectangle
template <class T>
bool Rectangle<T>::operator<(const Rectangle<T> &other) const
{
return((fabs(area()-other.area()) >= EPSILON) && (area() < other.area()));
}
// function to return if first rectangle is greater in area than second rectangle
template <class T>
bool Rectangle<T>:: operator>(const Rectangle<T> &other) const
{
return((fabs(area()-other.area()) >= EPSILON) && (area() > other.area()));
}
// function to return if first rectangle is less than or equal in area than second rectangle
template <class T>
bool Rectangle<T>::operator<=(const Rectangle<T> &other) const
{
return((fabs(area()-other.area()) >= EPSILON) && (area() <= other.area()));
}
// function to return if first rectangle is greater than or equal in area than second rectangle
template <class T>
bool Rectangle<T>:: operator>=(const Rectangle<T> &other) const
{
return((fabs(area()-other.area()) >= EPSILON) && (area() >= other.area()));
}
// function to return if first rectangle is equal in area to second rectangle
template <class T>
bool Rectangle<T>::operator==(const Rectangle<T> &other) const
{
return(sameArea(other));
}
// function to return if first rectangle is not equal in area to second rectangle
template <class T>
bool Rectangle<T>::operator!=(const Rectangle<T> &other) const
{
return(!sameArea(other));
}
// function to assign the length and width of second area to first
template <class T>
void Rectangle<T>::operator=(const Rectangle<T> &other)
{
if(this != &other) // avoid self assignment
{
this->length = other.length;
this->width = other.width;
}
}
// function same as + operator and assign the result to the first Rectangle
template <class T>
void Rectangle<T>::operator+=(const Rectangle<T> &other)
{
T req_area = area()+other.area();
T ratio = area()/req_area;
length = length*ratio;
width = width*ratio;
}
// function same as - operator and assign the result to the first Rectangle
template <class T>
void Rectangle<T>::operator-=(const Rectangle<T> &other)
{
T req_area = fabs(area()-other.area());
T ratio = area()/req_area;
length = length*ratio;
width = width*ratio;
}
// function same as * operator and assign the result to the first Rectangle
template <class T>
void Rectangle<T>::operator*=(const Rectangle<T> &other)
{
T req_area = area()*other.area();
T ratio = area()/req_area;
length = length*ratio;
width = width*ratio;
}
// function same as / operator and assign the result to the first Rectangle
template <class T>
void Rectangle<T>::operator/=(const Rectangle<T> &other)
{
T req_area = area()/other.area();
T ratio = area()/req_area;
length = length*ratio;
width = width*ratio;
}
//end of rectangle class