### Rectangle Class.

The question is:

create a rectangle class 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 verify that legnth and width are each floating-point numbers larger than 0.0 and less than 20.0
thank you.
Sounds like a great homework problem.

Post what you have so far. Tell us what you are having problems with, and we'll try to help you solve them.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181`` `````` /*Header File Rectangle.h */ class Rectangle { private: double length; double width; public: Rectangle (); Rectangle (double len, double wid); double getLength () const; double getWidth () const; void setLength (double l); void setWidth (double w); double getArea () const; double getPerimeter () const; void displayStatistics () const; }; /* Rectangle.cpp*/ #include #include "Rectangle.h" using namespace std; Rectangle::Rectangle () { length =1.0; width = 1.0; } Rectangle::Rectangle (double len, double wid) { length = len; width = wid; } void Rectangle::setLength (double l) { length = l; } void Rectangle::setWidth (double w) { width = w; } double Rectangle::getLength () const { return length; } double Rectangle::getWidth () const { return width; } double Rectangle::getArea () const { return (length*width); } double Rectangle::getPerimeter () const { return (2*(length+width)); } void Rectangle::displayStatistics () const { cout < #include "Rectangle.h" using namespace std; int main () { Rectangle unitRectangle; Rectangle myRectangle(2.0,5.0); cout <<"Displaying the statistics of unit rectangle :"<>num1 >> num2; myRectangle.setLength(num1); myRectangle.setWidth (num2); cout <

where i have to set `the set functions should verify that legnth and width are each floating-point numbers larger than 0.0 and less than 20.0`
Input validation can be interesting. The validation statement in the assignment is part of the set functions, so that simplifies things. Because you are using a double for an argument, you are guaranteed a floating point value in the `set` function, so that part is taken care of for you. What you need to do is make sure the value is within the legal range before making the assignments in lines 74 and 83.

Validating user input to make sure that it is a legal floating point value (no letters, at most 1 period, etc.) is a more complex endeavor and probably not part of the `set` function.
Topic archived. No new replies allowed.