(Rectangle Class) Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide member functions that calculate the perimeter and the area of the rectangle. Also, provide set and get functions for the length and width attributes. The set functions should ver- ify that length and width are each floating-point numbers larger than 0.0 and less than 20.0.

What will be an ideal response?


```
#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle
{
public:
Rectangle( double = 1.0, double = 1.0 ); // default constructor
void setWidth( double w ); // set width
void setLength( double l ); // set length
double getWidth(); // get width
double getLength(); // get length
double perimeter(); // perimeter
double area(); // area
private:
double length; // 1.0 < length < 20.0
double width; // 1.0 < width < 20.0
}; // end class Rectangle

#endif
```
```
// Member-function definitions for class Rectangle.
#include "Rectangle.h" // include definition of class Rectangle

Rectangle::Rectangle( double w, double l )
{
setWidth(w); // invokes function setWidth
setLength(l); // invokes function setLength
} // end Rectangle constructor

void Rectangle::setWidth( double w )
{
width = w > 0 && w < 20.0 ? w : 1.0; // sets width
} // end function setWidth

void Rectangle::setLength( double l )
{
length = l > 0 && l < 20.0 ? l : 1.0; // sets length
} // end function setLength

double Rectangle::getWidth()
{
return width;
} // end function getWidth

double Rectangle::getLength()
{
return length;
} // end function getLength

double Rectangle::perimeter()
{
return 2 * ( width + length ); // returns perimeter
} // end function perimeter

double Rectangle::area()
{
return width * length; // returns area
} // end function area
```
```
#include
#include
#include "Rectangle.h" // include definition of class Rectangle
using namespace std;

int main()
{
Rectangle a, b( 4.0, 5.0 ), c( 67.0, 888.0 );

cout << fixed;
cout << setprecision( 1 );

// output Rectangle a
cout << "a: length = " << a.getLength() << "; width = "
<< a.getWidth() << "; perimeter = " << a.perimeter()
<< "; area = " << a.area() << '\n';

// output Rectangle b
cout << "b: length = " << b.getLength() << "; width = "
<< b.getWidth() << "; perimeter = " << b.perimeter()
<< "; area = " << b.area() << '\n';

// output Rectangle c; bad values attempted
cout << "c: length = " << c.getLength() << "; width = "
<< c.getWidth() << "; perimeter = " << c.perimeter()
<< "; area = " << c.area() << endl;
} // end main
```
a: length = 1.0; width = 1.0; perimeter = 4.0; area = 1.0
b: length = 5.0; width = 4.0; perimeter = 18.0; area = 20.0
c: length = 1.0; width = 1.0; perimeter = 4.0; area = 1.0

Computer Science & Information Technology

You might also like to view...

In Access, a(n) ________ allows you to review and enter information into a database without having to open a table

Fill in the blank(s) with correct word

Computer Science & Information Technology

In Virtual Hard Disk Management, the task that reduces the storage capacity of the VHD is ________

a. Compact b. Shrink c. Store d. Convert

Computer Science & Information Technology

A security analyst discovers accounts in sensitive SaaS-based systems are not being removed in a timely manner when an employee leaves the organization. To BEST resolve the issue, the organization should implement:

A. federated authentication. B. role-based access control. C. manual account reviews D. multifactor authentication.

Computer Science & Information Technology

A(n) ________ is created when two wireless devices connect to each other directly

a.ad hoc network b.infrastructure wireless network c.personal area network (PAN) d.virtual private network (VPN)

Computer Science & Information Technology