Write a program that uses a class named Rectangle. The class has floating point attributes length and width. It has member functions that calculate the perimeter and the area of the rectangle. It also has set and get functions for both length and width, The set functions verify that length and width are each floating point numbers large than 0.0 and 20.0. If invalid length or width are given, then length and width will be set to 1.0. A member Boolean function will determine if the rectangle is a square (A square exists if the length and the width differ by less than .0001) The class will have a destructor that displays a message indicating that an object has "gone out of scope".

The class will have 3 overloaded constructor functions. The first will have no parameters ( in this function set the length and width to 1.0 in the body of the function. The second will have one parameter (length). (in this function set the width to 1.0 in the body of the function.) The third will have two parameters (length and width). This third constructor will set length and width to 1.0 in the body of the function if the values for these members are invalid.

Error messages will indicate that an attempt has been made to create an object with invalid parameters. Test the performance of your class by performing the following tasks in your program in the given order: Declare object 1 with no parameters. Declare object 2 with valid parameters for length (7.1) and width (3.2. Declare object 3 with only a length (6.3). Declare object 4 with invalid parameters for length and width. Declare object 5 and initialize it by assigning object 2. Display the length, width, perimeter, area, of all 5 objects and indicate wether or not they are squares. Write all output data to a file.

So my problem is my output. The program runs but my output isn't looking great. I get basically a bunch of junk for length, width, area, and perimeter. And I can't figure out how to make my destructor show up that it's gone out of scope after each object. I can only make it show up at the very end. Please help.

Here is my header file

#ifndef Rectangle_H
#define Rectangle_H

class Rectangle
{
public:
	Rectangle();
	Rectangle(float length);
	Rectangle(float length, float width);
	~Rectangle();

	void setLengthAndWidth(float, float);
	void testLength(float length);
	void testWidth(float width);
	void calculatePerimeter();
	void calculateArea();
	void printInfo() const;

	float getLength() const;
	float getWidth() const;
	bool isSquare() const;

private: 
	float length;
	float width;
	float area;
	float perimeter;

};

#endif

Here is my member functions cpp file

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

using namespace std;

Rectangle::Rectangle()
	{length = width = 1.0;}

Rectangle::Rectangle(float length)
	{setLengthAndWidth (length, 1.0);}

Rectangle::Rectangle(float length, float width)
	{setLengthAndWidth (length, width);}

void Rectangle::setLengthAndWidth(float length, float width)
{
	testLength(length);
	testWidth(width);
}

void Rectangle::testLength(float length)
{
	if (length >= 0 || length <= 20.0) 
		length = length;
	else
		length = 1.0;
}

void Rectangle::testWidth(float width)
{
	if (width >= 0 || width <= 20.0) 
		width = width;
	else
		width = 1.0;
}

void Rectangle::calculatePerimeter() 
{
	perimeter = (length * 2.0f) + (width * 2.0f);
		
}

void Rectangle::calculateArea() 
{
	 area = length * width;
		
}

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

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

bool Rectangle::isSquare() const
{
	return fabs(length - width) < .0001;
}

Rectangle::~Rectangle()
{

	cout << "the object has gone out of scope. " << endl << endl;
}

void Rectangle::printInfo() const
{
	if(isSquare())
		cout << "the rectangle is square" << endl;
	else 
		cout << "The rectangle is not a square " << endl;

	cout << "the length is " << length << endl << "the width is " << width << endl;
	cout << "the perimeter is " << perimeter << endl << "the area is " << area << endl << endl << endl;
	

	
}

And here is my main

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

using namespace std;

int main()
{
	Rectangle objectOne;
	Rectangle objectTwo(7.1,3.2);
	Rectangle objectThree(6.3);
	Rectangle objectFour(22.0,33.0);
	Rectangle objectFive = objectTwo;

	cout << "The first objects information is " << endl;
	objectOne.printInfo();

	cout << "The second objects information is " << endl;
	objectTwo.printInfo();

	cout << "The third objects information is " << endl;
	objectThree.printInfo();

	cout << "The fourth objects information is " << endl;
	objectFour.printInfo();

	cout << "The fifth objects information is " << endl;
	objectFive.printInfo();
}

I've tried making some of them references and I've tried making some of them constants but my output seems to still display junk. Here is my output

http://i.imgur.com/kRmym.png

Hmm.... should this => if (length >= 0 || length <= 20.0) be => if (length>=0 && lenght<=20.0) ?? You are getting "true" with any value using the current condition in your if statement.

Also, do you really need a deconstructor??? I don't see that you use any pointer (memory allocation) in the class. Do you still need to do it???

PS: Please use "code" tag instead of "text" because it displays color and easier to look at...

Edited 5 Years Ago by Taywin: n/a

Well for one, you've never calculated perimeter and area, so its going to be the crazy value because the value was never set. However the other objects generating strenuous results with the length and width I'm not sure if they will remain when you fix the calculating of the perimeter and area, but you may want to make a constructor of Rectangle type especially since object5 is set to object2

Rectangle(const Rectangle& rect)
{
    length = rect.length;
    width = rect.width;
    perimeter = rect.perimeter;
    area = rect.area;
}

hope this helps, reply if the problem persists.

Well for one, you've never calculated perimeter and area, so its going to be the crazy value because the value was never set. However the other objects generating strenuous results with the length and width I'm not sure if they will remain when you fix the calculating of the perimeter and area, but you may want to make a constructor of Rectangle type especially since object5 is set to object2

Rectangle(const Rectangle& rect)
{
    length = rect.length;
    width = rect.width;
    perimeter = rect.perimeter;
    area = rect.area;
}

hope this helps, reply if the problem persists.

Alright I changed my or to and thanks for pointing that out! And unfortunately we have to put the destructor in our program. Sorry, I'm still a bit new, tried my best to make it pretty.

I'm trying to see what you are saying. Would I want to basically make getfunctions for area and perimeter? I just thought I had calculated them in this code

void Rectangle::calculatePerimeter() 
{
	perimeter = (length * 2.0) + (width * 2.0);		
}

void Rectangle::calculateArea() 
{
	 area = length * width;
		
}

Edited 5 Years Ago by Ravic85: n/a

If you would like, in your default constructor use your setLengthAndWidth(1.0,1.0) function then in that function also calculate the perimeter and area, so when you set the length or width the new area/perimeter's are calculated.

Okay, well first thing is first my teacher said "the third will have two parameters (length and width) this third constructor will set length and width to 1.09 in the body of the function if the values these members are invalid."

So I did this and got rid of my testLength and testWidth member functions, and put them inside of my third overloaded constructor like he said.

Rectangle::Rectangle(float length, float width)
{
		setLengthAndWidth (length, width);
	
	 if (( length >=0 && length <=20.0) || (width >= 0 && width <= 20.0))
	 {
		length = length, width = width; 
	 }
	else
	 {
		length = 1.0 , width = 1.0;
	 }

	 perimeter = (length * 2.0) + (width * 2.0);

	 area = length * width;

}

secondly hopefully I understood you correctly I put my perimeter and area calculations inside of my third constructor and have set my setLengthAndWidth function to (1.0, 1.0). but the problem with this is he said that I would use member functions that calculate the area and the perimeter.

Edited 5 Years Ago by Ravic85: n/a

This article has been dead for over six months. Start a new discussion instead.