Description
Construct a 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 can easily 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.
A sample program has been created for you to illustrate trapping for Ctrl-Z as well as non-numeric and/or negative data. You may find this useful to incorporate into your lab. Note that cin goes into the fail state not only when non-numeric data is entered into an integer variable, but also when Ctrl-Z is entered. We will learn in a later chapter that cin has various bit flags that indicate whether an eof (end of file) or other type of error has occurred. Each one of these bits can trigger cin to enter the fail state. Therefore, this sample program illustrates the use of the eof function to determine if the user has entered Ctrl-Z. The sample program is entitled “Ctrl_Z_Test” and is located in this lab folder.
Your lab should be constructed such that separate files are used for the class header, class implementation, and driver program. Refer to your notes for creating these separate files as we will discuss how to do this in class lectures.
Use good prompting, output labeling, and modularization in your program and class. Be sure to avoid using global variables in your program.
Deliverables:
- Complete the programming assignment described above and submit your completed assignment in accordance with the lab submission policies.