Hello everyone! I've made the mistake of not asking my teacher for help when I had the time. Now I'm stuck and lost. I didn't do this because I felt I should be able to figure this out myself since this is my major. So, I'm giving up and needing advice. I would love any help.
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, hopefully my code so far isn't awful. I'm sorry if it is. I understand that area and perimeter need to be passed by value returning, but i'm unsure on how to do this. I also am confused by the destructor, and I'm sure there are other things I did wrong that I need pointed out. Here's what I have so far.
This is my header file Rectangle.h
#ifndef Rectangle_H
#define Rectangle_H
class Rectangle
{
public:
Rectangle();
Rectangle(float length);
Rectangle(float length, float width);
~Rectangle();
void setLengthAndWidth(float, float);
void setLength(float Length);
void setWidth(float Width);
void calculatePerimeter();
void calculateArea();
void isSquare();
void printInfo();
float getLength();
float getWidth();
private:
float length;
float width;
float area;
float perimeter;
};
#endif
This is my Member function 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 Len, float Wid)
{
setLength(Len);
setWidth(Wid);
}
void Rectangle::setLength(float length)
{
if (length >= 0 || length <= 20.0)
length = length;
else
length = 1.0;
}
void Rectangle::setWidth(float width)
{
if (width >= 0 || width <= 20.0)
width = width;
else
width = 1.0;
}
void Rectangle::calculatePerimeter()
{
(length * 2) + (width * 2) = perimeter;
return perimeter;
}
void Rectangle::calculateArea()
{
length * width = area;
return area;
}
float Rectangle::getLength()
{
return length;
}
float Rectangle::getWidth()
{
return width;
}
void Rectangle::isSquare()
{
return fabs(length - width) < .0001;
}
void Rectangle::printInfo()
{
cout << "the length is " << length << endl << "the width is " << width << endl;
cout << "the perimeter is " << perimeter << endl << "the area is " << area << endl;
if(Rectangle.isSquare)
cout << "the rectangle is square" << endl;
else
cout << "The rectangle is not a square " << endl;
}
Rectangle::~Rectangle()
{
cout << "the object has gone out of scope. ";
}
And this is my main cpp file
#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(200,300);
Rectangle objectFive = objectTwo;
cout << "The first objects information is\n ";
objectOne.printInfo();
cout << "The second objects information is\n ";
objectTwo.printInfo();
cout << "The third objects information is\n ";
objectThree.printInfo();
cout << "The fourth objects information is\n ";
objectFour.printInfo();
cout << "The fifth objects information is\n ";
objectFive.printInfo();
}
I would be forever thankful for help. This is due tomorrow and I'm worried I'm light years behind from finishing it, but I guess it's my own fault for thinking I could do it on my own. Thanks guys!