Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. write program that uses a class named Rectangle. The class has floating point

ID: 3550156 • Letter: 1

Question

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


2. The class will have 3 overloaded contructor functions. The first will have no parameteres.(In this function set the length and width to 1.0 in the body of the fucntion.) 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 contructor will set length and width to 1.0 in the body of the function if the values for these members are invalid.


3. error messages will indicate that an attempt has been made to create an object with invalid parameters.


4. A header file will be used to declare the class. Other header files may be used if you wish. A separate .cpp file will be used to implement the class member functions. Hence, your program will consist of at least one header file and at least 2 .cpp files

Explanation / Answer

HEADER FILE (Rectangle.h)

// prevent multiple inclusions of header

#ifndef RECTANGLE_H

#define RECTANGLE_H

class Rectangle // RECTANGLE class declaration

{

public:

     Rectangle(double = 0.0, double = 0.0);      // default constructor

     void setLength(double);                         // Set functions

     void setWidth(double);   

     double getLength() const;                       // Get functions

     double getWidth();

     double getArea();

     double getPerimeter();

     void isSquare();                                // Square testing

private:

     double length;

     double width;

};

#endif

****************************************************************************

CLASS Member FUNCTIONS Definitions (RECTANGLE.cpp)

#include <iostream>

#include "Rectangle.h"

using namespace std;

Rectangle::Rectangle(double L, double W) // default constructor

{

      setLength(L);

      setWidth(W);

}

// START from HERE ALL the definitions of Class member functions

void Rectangle::setLength(double L)

{

      if (L > 0 && L <= 20.0)

      {

            length = L;

      }

      else

      {

            cout << " Invalid Length input, Default Length will be assigned. " << endl;

            length = 1.0;

      }

}

void Rectangle::setWidth(double W)

{

      if ((W <= 20.0) && (W > 0.0))

      {

            width = W;

      }

      else

      {cout << " Invalid Width input, Default Width will be assigned. " << endl;

            width = 1.0;

      }

}

double Rectangle::getLength() const

{

      return length;

}

double Rectangle::getWidth()

{

      return width;

}

double Rectangle::getArea()

{

      return (length * width);

}

double Rectangle::getPerimeter()

{

      return (2 * (length + width));

}

void Rectangle::isSquare() // Testing if Square or NOT

{

      cout << (length == width ? " Yes, It is a SQUARE " : " NO, It's NOT a Square ");

}                                                                                                                                                                   // End of class functions

********************************************************************************

MAIN Function File (Main.cpp)

// This is EXTRA ...... JUST for PRACTICE

// Declaring 5 objects and then displaying the Length, Width, Perimeter, Area

// Then testing each object if they are square or Not

#include <iostream>

#include "Rectangle.h"

using namespace std;

int main()

{                                               // Declaring the 5 Objects

      Rectangle r1; // No parameters

      Rectangle r2(7.1, 3.2); // with parameters

      Rectangle r3(6.3); // Only one parameter

      Rectangle r4(45, 23); // Invalid parameters

      Rectangle r5 = r2; // Assign one object to another

// Displaying Length, Width, Perimeter, Area and Square testing for all 5 Objects

      cout << " Length of Object 1 = " << r1.getLength() << endl;

      cout << " Width of Object 1 = " << r1.getWidth() << endl;

      cout << " Perimeter of Object 1 = " << r1.getPerimeter() << endl;

      cout << " Area of Object 1 = " << r1.getArea() << endl;

      r1.isSquare();

      cout << " Length of Object 2 = " << r2.getLength() << endl;

      cout << " Width of Object 2 = " << r2.getWidth() << endl;

      cout << " Perimeter of Object 2 = " << r2.getPerimeter() << endl;

      cout << " Area of Object 2 = " << r2.getArea() << endl;

      r2.isSquare();

      cout << " Length of Object 3 = " << r3.getLength() << endl;

      cout << " Width of Object 3 = " << r3.getWidth() << endl;

      cout << " Perimeter of Object 3 = " << r3.getPerimeter() << endl;

      cout << " Area of Object 3 = " << r3.getArea() << endl;

      r3.isSquare();

      cout << " Length of Object 4 = " << r4.getLength() << endl;

      cout << " Width of Object 4 = " << r4.getWidth() << endl;

      cout << " Perimeter of Object 4 = " << r4.getPerimeter() << endl;

      cout << " Area of Object 4 = " << r4.getArea() << endl;

      r4.isSquare();

      cout << " Length of Object 5 = " << r5.getLength() << endl;

      cout << " Width of Object 5 = " << r5.getWidth() << endl;

      cout << " Perimeter of Object 5 = " << r5.getPerimeter() << endl;

      cout << " Area of Object 5 = " << r5.getArea() << endl;

      r5.isSquare();

                 

// NOW Changing the values for object 1 and Object 4

      r1.setLength(5.4);                  // Changing Length of object 1

      r1.setWidth(10.5);                  // Changing Width of object 1

      r4.setLength(15.6);                 // Chnaging Length of object 4

      r4.setWidth(15.6);                  // Changing Width of object 4

// Displaying Length, Width, Perimeter, Area and Square testing for all 5 Objects... AGAIN

      cout << " After Changing L1=5.4 , W1=10.5 and L4=15.6 , W4=15.6 " << endl;

      cout << " Length of Object 1 = " << r1.getLength() << endl;

      cout << " Width of Object 1 = " << r1.getWidth() << endl;

      cout << " Perimeter of Object 1 = " << r1.getPerimeter() << endl;

      cout << " Area of Object 1 = " << r1.getArea() << endl;

      r1.isSquare();

      cout << " Length of Object 2 = " << r2.getLength() << endl;

      cout << " Width of Object 2 = " << r2.getWidth() << endl;

      cout << " Perimeter of Object 2 = " << r2.getPerimeter() << endl;

      cout << " Area of Object 2 = " << r2.getArea() << endl;

      r2.isSquare();

      cout << " Length of Object 3 = " << r3.getLength() << endl;

      cout << " Width of Object 3 = " << r3.getWidth() << endl;

      cout << " Perimeter of Object 3 = " << r3.getPerimeter() << endl;

      cout << " Area of Object 3 = " << r3.getArea() << endl;

      r3.isSquare();

      cout << " Length of Object 4 = " << r4.getLength() << endl;

      cout << " Width of Object 4 = " << r4.getWidth() << endl;

      cout << " Perimeter of Object 4 = " << r4.getPerimeter() << endl;

      cout << " Area of Object 4 = " << r4.getArea() << endl;

      r4.isSquare();

      cout << " Length of Object 5 = " << r5.getLength() << endl;

      cout << " Width of Object 5 = " << r5.getWidth() << endl;

      cout << " Perimeter of Object 5 = " << r5.getPerimeter() << endl;

      cout << " Area of Object 5 = " << r5.getArea() << endl;

      r5.isSquare();

     

}                                   // END of MAIN

****************************************************************************************

Good Luck!