I have an assignment to get the area and perimeter of a rectangle. I have to set the width and length to 1 initially and use set and get functions. I have the following code but am not getting the correct answers for area and perimeter. Any suggestions?
Rectangle.h
class Rectangle
{
public:
int x, y;
int width, length;
double area;
int perimeter;
public:
Rectangle ();
Rectangle (int, int);
int setWidth(int x);
int setHeight(int y);
int getWidth();
int getLength();
int setArea();
int getArea();
int setPerimeter();
int getPerimeter();
};
Rectangle.cpp
#include <iostream>
#include "Rectangle.h"
Rectangle::Rectangle()
{
x = 1;
y = 1;
}
Rectangle::Rectangle(int width, int length)
{
x = width;
y = length;
}
int Rectangle::getWidth()
{
return width;
}
int Rectangle::getLength()
{
return length;
}
int Rectangle::getArea()
{
area = width * length;
return area;
}
int Rectangle::getPerimeter()
{
perimeter = (width * 2) + (length * 2);
return perimeter;
}
int Rectangle::setWidth(int x)
{
if ((x < 0) || (x > 20))
width = 1;
else
return getWidth();
}
int Rectangle::setHeight(int y)
{
if ((y < 0) || (y > 20))
length = 1;
else
return getLength();
}
Main.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "Rectangle.h"
int main()
{
float length, width;
Rectangle a;
cout << "Enter The length: ";
cin >> length;
cout << "Enter The width: ";
cin >> width;
cout <<"The area of the rectangle is : "<< a.getArea() << endl;
cout <<"The perimeter is: " <<a.getPerimeter()<<endl;
system("pause");
return 0 ;
} // End of main()