Question: Write the following code in C++. Please explain! The class should have two private data members, a width and a height. A constructor function that
Write the following code in C++. Please explain!
The class should have two private data members, a width and a height.
A constructor function that sets the width and height of a rectangle to 0.
A function that takes two integers as inputs and sets width and height of a rectangle object. The function should check that both values are positive and if either value is negative make it positive. For example, if the user tries to set the height to -9, set it to 9 instead. Zero values are acceptable.
A constructor function that takes two arguments and sets the width and height of a rectangle to the two given values (checking for negative numbers as described above).
A print function that prints a rectangle object as: width = ... height = ... where ... has the correct value.
An area() function that returns, but does not print, the area of a rectangle.
An perimeter() function that returns, but does not print, the perimeter of a rectangle.
To test the rectangle class create at least three different rectangle objects and use them to test all of the above functions. Note that for the area() and perimeter() functions the main() function will need to print the results because they are not printed by the functions.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
