Question

Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length, width, perimeter, and area. The class should use appropriate protection levels for the member data and functions. It should also follow “principles of minimalization”: that is, no member data should be part of a class unless it is needed by most member functions of the object.  A general rule of thumb is that “if you caneasily calculate it, don’t store it.”

Use your class in a program that creates an instance of a Rectangle (utilizing the zero-argument constructor), prompts a user for a length and width, calls the setLength() and setWidth() functions to set the rectangle’s length and width, and then calls showData() to display the resulting inputs, perimeter, and area. Your program should allow the user to enter rectangle dimensions until the user enters Ctrl-Z.  Be sure to include appropriate error checking.  Does a negative length or width make sense? No.  Therefore, you should ensure that the user only enters positive numbers. Also, does it make sense to enter “abc” as the length of a rectangle?  No, again.  Therefore, you should also ensure that the user enters numeric data for both length and width. Your code should be constructed such that separate files are used for the class header, class implementation, and driver program. Use good prompting, output labeling, and modularization in your program and class. Be sure to avoid using global variables in your program

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Screenshot:

Rectangle.h

Rectangle.cpp

driver.cpp

Sample output:

Code to copy:

Rectangle.h

#ifndef RECTANGLE_H

#define RECTANGLE_H

#include <iostream>

using namespace std;

//define the class Rectangle

class Rectangle

{

private:

       // declare the floating - point data members named length and width.

       float length;

       float width;

public:

       //zero-argument constructor

       Rectangle();

       //declare the memeber functions.

       float calcPerimeter();

       float calcArea();

       void setLength(float len);

       void setWidth(float wid);

       float getLength();

       float getWidth();

       void showData();

      

};

#endif

Rectangle.cpp

#include "Rectangle.h"

//zero-argument constructor that initializes each data member to 0.

Rectangle::Rectangle()

{

       length = 0;

       width = 0;

}

//definition of the function calcPerimeter

//calculates the perimeter of the rectangle

float Rectangle::calcPerimeter()

{

       return 2 * (length + width);

}

//definition of the function calcArea

//calculates the area of the rectangle

float Rectangle::calcArea()

{

       return (length * width);

}

//definition of the function setLength

// sets the length

void Rectangle::setLength(float len)

{

       length = len;

}

//definition of the function setWidth

// sets the width

void Rectangle::setWidth(float wid)

{

       width = wid;

}

//definition of the function getLength

//returns the length

float Rectangle::getLength()

{

       return length;

}

//definition of the function getWidth

//returns the width

float Rectangle::getWidth()

{

       return width;

}

//definition of the function showData.

//displays the rectangle’s length, width, perimeter, and area.

void Rectangle::showData()

{

       cout << "The length of the rectange: " << getLength() << endl;

       cout << "The width of the rectange: " << getWidth() << endl;

       cout << "The perimeter of the rectange: " << calcPerimeter() << endl;

       cout << "The area of the rectange: " << calcArea() << endl;

}

Driver.cpp

#include "Rectangle.h"

bool EndOfInputStream(istream&);

int main()

{

       float length, width;

       //create an instance of a Rectangle

       Rectangle r1;

       //prompts a user for a length

       cout << "Enter length (Ctrl-Z to exit): ";

       cin >> length;

       //iterate a loop until the user enters Ctrl-Z

       //call the function EndOfInputStream() to test the input is Ctrl-Z

       while (!EndOfInputStream(cin))

       {

              //if the user is less than 0 or a string,

              //then print an error message and read again until user enter valid input.

              while (cin.fail() || length < 0)

              {

                     cout << "You must enter a positive number. Please try again." << endl;

                     cin.clear();

                     cin.ignore(numeric_limits<streamsize>::max(),'\n');

                     cout << "Enter length (Ctrl-Z to exit): ";

                     cin >> length;

              }

              //prompts a user for a width

              cout << "Enter width (Ctrl-Z to exit): ";

              cin >> width;

              //if the user is less than 0 or a string,

              //then print an error message and read again until user enter valid input.

              while (cin.fail() || width < 0)

              {

                     cout << "You must enter a positive number. Please try again." << endl;

                     cin.clear();

                     cin.ignore(numeric_limits<streamsize>::max(), '\n');

                     cout << "Enter width (Ctrl-Z to exit): ";

                     cin >> width;

              }

              //call the setLength() to set the rectangle’s length.

              r1.setLength(length);

              //call the setWidth() to set the rectangle’s width.

              r1.setWidth(width);

              cout << endl;

              //calls showData() to display the resulting inputs, perimeter, and area.

              r1.showData();

              cout << endl << endl;

              cout << "Enter length (Ctrl-Z to exit): ";

              cin >> length;

       }

       return 0;

}

//definition of the function EndOfInputStream().

bool EndOfInputStream(istream& input)

{

       bool endOfInput{ false };

       if (input.eof())

       {

              endOfInput = true;

       }

       return endOfInput;

}

Add a comment
Know the answer?
Add Answer to:
Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • I need help with the C++ for the following problem: Write a program uses objected oriented...

    I need help with the C++ for the following problem: Write a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(),...

  • Use C++ to implement a program uses objected oriented programming to calculate the area of a...

    Use C++ to implement a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(), and getArea() member function should get the...

  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • Use DevC++ to implement a program uses objected oriented programming to calculate the area of a...

    Use DevC++ to implement a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(), and getArea() member function should get the...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • a. construct a class named room that declares two-double precision data members named length and width....

    a. construct a class named room that declares two-double precision data members named length and width. include a constructor that initializes each datavmember to 1.0 when a room object is created. add two accessor funtions for displaying the length and width values of a room object and two mutator functions that allow changing these data member values. b. include the class written for 6a in the context of a complete program

  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that...

  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT