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 |
#include<iostream>
using namespace std;
class Rectangle{
double length;
double width;
public:
double getlength()
{
return this->length;
}
double getwidth()
{
return this->width;
}
void setwidth(double width)
{
this->width=width;
}
void setlength(double length)
{
this->length=length;
}
double perimeter()
{
return 2*(length+width);
}
double area()
{
return length*width;
}
void show()
{
cout<<"The length of the rectangle:
"<<length<<"\n";
cout<<"The width of the rectangle:
"<<width<<"\n";
}
int sameArea(Rectangle R)
{
if(this->area()==R.area())
return 1;
else
return 0;
}
Rectangle operator -()
{
Rectangle res;
res.setlength(-this->length);
res.setwidth(-this->width);
return res;
}
Rectangle operator +(Rectangle R)
{
Rectangle res;
double newarea=this->area()+R.area();
double newlen=(newarea*this->length)/this->area();
res.setlength(newlen);
res.setwidth(newarea/newlen);
return res;
}
Rectangle operator -(Rectangle R)
{
Rectangle res;
double newarea=R.area()-this->area();
double newlen=(newarea*this->length)/this->area();
res.setlength(newlen);
res.setwidth(newarea/newlen);
return res;
}
Rectangle operator *(Rectangle R)
{
Rectangle res;
double newarea=this->area()*R.area();
double newlen=(newarea*this->length)/this->area();
res.setlength(newlen);
res.setwidth(newarea/newlen);
return res;
}
Rectangle operator /(Rectangle R)
{
Rectangle res;
double newarea=this->area()/R.area();
double newlen=(newarea*this->length)/this->area();
res.setlength(newlen);
res.setwidth(newarea/newlen);
return res;
}
bool operator <(Rectangle R)
{
if(this->area()<R.area())
return 1;
else
return 0;
}
bool operator >(Rectangle R)
{
if(this->area()>R.area())
return 1;
else
return 0;
}
bool operator >=(Rectangle R)
{
if(this->area()>=R.area())
return 1;
else
return 0;
}
bool operator <=(Rectangle R)
{
if(this->area()<=R.area())
return 1;
else
return 0;
}
bool operator ==(Rectangle R)
{
if(this->area()==R.area())
return 1;
else
return 0;
}
bool operator !=(Rectangle R)
{
if(this->area()!=R.area())
return 1;
else
return 0;
}
Rectangle operator =(Rectangle R)
{
this->setlength(R.length);
this->setwidth(R.width);
}
void operator +=(Rectangle R)
{
double newarea=this->area()+R.area();
double newlen=(newarea*this->length)/this->area();
this->setlength(newlen);
this->setwidth(newarea/newlen);
}
void operator -=(Rectangle R)
{
double newarea=R.area()-this->area();
double newlen=(newarea*this->length)/this->area();
this->setlength(newlen);
this->setwidth(newarea/newlen);
}
void operator *=(Rectangle R)
{
double newarea=this->area()*R.area();
double newlen=(newarea*this->length)/this->area();
this->setlength(newlen);
this->setwidth(newarea/newlen);
}
void operator /=(Rectangle R)
{
double newarea=this->area()/R.area();
double newlen=(newarea*this->length)/this->area();
this->setlength(newlen);
this->setwidth(newarea/newlen);
}
};
int main()
{
Rectangle R1,R2;
R1.setlength(5);
R1.setwidth(2);
R2.setlength(5);
R2.setwidth(18.9);
cout<<"The perimeter and area of first rectangle of length
"<<R1.getlength()<<" and width
"<<R1.getwidth()<<"is :
"<<R1.perimeter()<<" and "<<R1.area()<<"
respectively\n";
cout<<"The perimeter and area of second rectangle of length
"<<R2.getlength()<<" and width
"<<R2.getwidth()<<"is :
"<<R2.perimeter()<<" and "<<R2.area()<<"
respectively\n";
if(R1.sameArea(R2)==1)
cout<<"First Rectangle and Second Rectangle have same
area\n";
else
cout<<"First Rectangle and Second Rectangle have different
area\n";
R1.setlength(15);
R1.setwidth(6.3);
cout<<"The perimeter and area of first rectangle of length
"<<R1.getlength()<<" and width
"<<R1.getwidth()<<"is :
"<<R1.perimeter()<<" and "<<R1.area()<<"
respectively\n";
cout<<"The perimeter and area of second rectangle of length
"<<R2.getlength()<<" and width
"<<R2.getwidth()<<"is :
"<<R2.perimeter()<<" and "<<R2.area()<<"
respectively\n";
if(R1.sameArea(R2)==1)
cout<<"First Rectangle and Second Rectangle have same
area\n";
else
cout<<"First Rectangle and Second Rectangle have different
area\n";
return 0;
}