Create a struct Rectangle where a rectangle is defined by Length & Width. Each
Rectangle has an area and perimeter. You can also compare two rectangles with
respect to the area.
//A structure that implements a rectangle
struct Rectangle
{
private:
double Length,
Width,
Area,
Perimeter;
public:
//Constructor without parameters
Rectangle()
{
Length = 0;
Width = 0;
Area = 0;
Perimeter = 0;
}
//Constructor with parameters: len - length, wid - width
Rectangle(double len, double wid)
{
Length = len;
Width = wid;
Area = len * wid;//Calculating the area of a rectangle
Perimeter = 2*(len + wid);//Calculating the perimeter of a rectangle
}
//Get the rectangle area value
double GetArea(){ return Area; }
};
/*****************************************************************************/
//Comparing two rectangles by area
int rectcmp(Rectangle & rect1, Rectangle & rect2)
{
double temp = rect1.GetArea() - rect2.GetArea();
if(temp < 0) return -1; //Returns -1 if rect1 is less than rect2
if(temp == 0) return 0; //Returns 0 if rect1 is equal to rect2
if(temp > 0) return 1; //Returns 1 if rect1 is greater than rect2
}
/******************************************************************************/
Comments
Leave a comment