Write a class called Rectangle with double fields for its le
Write a class called Rectangle with double fields for its length and width. It should have set methods for both fields. It should have a constructor that takes two double parameters and passes them to the set methods. It should also have a method called area that returns the area of the Rectangle and a method called perimeter that returns the perimeter of the Rectangle.
Write a class called Square that inherits from Rectangle. It should have a constructor that takes one double parameter and passes it to the base class constructor for both parameters (the body of the constructor will be empty). Square will also need to override the setLength() and setWidth() functions of its base class such that if either of its dimensions is set to a new value, then both of its dimensions will be set to that new value (so that it remains a square). Hint: you can have the overridden versions call the versions in the base class.
The files must be called: Rectangle.hpp, Rectangle.cpp, Square.hpp and Square.cpp
Solution
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
public:
Rectangle(double,double);
void setWidth(double);
void setLength(double);
double getArea();
double getPerimeter();
protected:
private:
double length,width;
};
#endif // RECTANGLE_H
#include \"Rectangle.h\"
Rectangle::Rectangle(double l,double w)
{
//ctor
length=l;
width=w;
}
void Rectangle::setWidth(double w)
{
width=w;
}
void Rectangle::setLength(double l)
{
length=l;
}
double Rectangle::getArea()
{
return width*length;
}
double Rectangle::getPerimeter()
{
return 2*(width+length);
}
#ifndef SQUARE_H
#define SQUARE_H
#include \"Rectangle.h\"
class Square : public Rectangle
{
public:
Square(double );
void setWidth(double);
void setLength(double);
protected:
private:
};
#endif // SQUARE_H
#include \"Square.h\"
Square::Square(double side):Rectangle(side,side)
{
//ctor
}
void Square::setWidth(double w)
{
Rectangle::setWidth(w);
}
void Square::setLength(double l)
{
Rectangle::setLength(l);
}

