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()

Recommended Answers

All 3 Replies

Your setWidth and setHeight functions have paths that do not return anything. That doesn't seem to matter since you never call these functions anyway. You never set the width and length of rectangle a; not even to default values. They could be anything.

What are the rectangle class variables x and y for? They never get used.

I have modified my code greatly...but I am getting one error message:

Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::Rectangle(void)" (??0Rectangle@@QAE@XZ) referenced in function _main
1>C:\Users\Lindsey Gladson\Documents\Visual Studio 2010\Projects\Rectangle\Debug\Rectangle.exe : fatal error LNK1120: 1 unresolved externals

I am not sure what I have wrong the code is:

Rectangle.h

class Rectangle
{
private:
	float width;
	float length;
	double area;
	double perimeter;

public:
Rectangle();
Rectangle(float, float);

void setWidth(float width);
void setLength(float length);
float getWidth();
float getLength();
double setArea();
double getArea();
double setPerimeter();
double getPerimeter();
};

Rectangle.cpp

#include <iostream>
#include "Rectangle.h"

	float initialWidth;
	float initialLength;

Rectangle::Rectangle(float width, float length)
{
	width = initialWidth;
	length = initialLength;

	setWidth(initialWidth);
	setLength(initialLength);
}

float Rectangle::getWidth()
{
	return width;
}

float Rectangle::getLength()
{
	return length;
}

double Rectangle::getArea()
{
	area = width * length;
	return area;
}

double Rectangle::getPerimeter()
{
	perimeter = (width * 2) + (length * 2);
	return perimeter;
}

void Rectangle::setWidth(float newWidth)
{
	width = newWidth;
	
	if ((width < 0) || (width > 20))
		width = newWidth;
	else 
		width = 1;
}

void Rectangle::setLength(float newLength)
{
	length = newLength;
	
	if ((length < 0) || (length > 20))
		length = newLength;
	else
		length = 1;
}

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()

It means you haven't defined this function:

Rectangle();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.