Answer to Question #171785 in C++ for Esha

Question #171785

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.


1
Expert's answer
2021-03-16T00:08:17-0400
//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
}
/******************************************************************************/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS