Modify the C++ program below: .Change the setLength and setWidth (mutator) membe
ID: 674425 • Letter: M
Question
Modify the C++ program below:
.Change the setLength and setWidth (mutator) member functions to first check if the parameter < 0. If it is, assign the absolute value of the parameter (just negate). If it's 0, assign a default of 10. If it's >0, just assign the parameter.
· Change the accessor functions (getLength and getWidth) so they're INLINE
· Add a function to the class declaration that calculates and returns the perimeter of the Rectangle (NO PARAMETERS). Then write the function below the declaration (NOT AN INLINE FUNCTION).
Explanation / Answer
// This program creates three instances of the Rectangle class.
#include <iostream>
using namespace std;
// Rectangle class declaration.
class Rectangle
{
private:
double width;
double length;
public:
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLength() const;
double getArea() const;
double peri()
{
return 2*(length+width);
}
};
//**************************************************
// setWidth assigns a value to the width member. *
//**************************************************
void Rectangle::setWidth(double w)
{
if(w<0)
w=-(w);
else if(w==0)
w=10;
width = w;
}
//**************************************************
// setLength assigns a value to the length member. *
//**************************************************
void Rectangle::setLength(double len)
{
if(len<0)
len=-(len);
else if(len==0)
len=10;
length = len;
}
//**************************************************
// getWidth returns the value in the width member. *
//**************************************************
INLINE double Rectangle::getWidth() const
{
return width;
}
//****************************************************
// getLength returns the value in the length member. *
//****************************************************
INLINE double Rectangle::getLength() const
{
return length;
}
//*****************************************************
// getArea returns the product of width times length. *
//*****************************************************
double Rectangle::getArea() const
{
return width * length;
}
//*****************************************************
// Function main *
//*****************************************************
int main()
{
double number; // To hold a number
double totalArea; // The total area
Rectangle kitchen; // To hold kitchen dimensions
Rectangle bedroom; // To hold bedroom dimensions
Rectangle den; // To hold den dimensions
// Get the kitchen dimensions.
cout << "What is the kitchen's length? ";
cin >> number; // Get the length
kitchen.setLength(number); // Store in kitchen object
cout << "What is the kitchen's width? ";
cin >> number; // Get the width
kitchen.setWidth(number); // Store in kitchen object
// Get the bedroom dimensions.
cout << "What is the bedroom's length? ";
cin >> number; // Get the length
bedroom.setLength(number); // Store in bedroom object
cout << "What is the bedroom's width? ";
cin >> number; // Get the width
bedroom.setWidth(number); // Store in bedroom object
// Get the den dimensions.
cout << "What is the den's length? ";
cin >> number; // Get the length
den.setLength(number); // Store in den object
cout << "What is the den's width? ";
cin >> number; // Get the width
den.setWidth(number); // Store in den object
// Calculate the total area of the three rooms.
totalArea = kitchen.getArea() + bedroom.getArea()
+ den.getArea();
// Display the total area of the three rooms.
cout << "The total area of the three rooms is "
<< totalArea << endl;
// Display the Perimeter of Rectangle.
cout << "The Perimeter of rectangle is "
<< den.peri << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.