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