I also need some help to w rite a C++ program that calculates the area of a rect
ID: 3680591 • Letter: I
Question
I also need some help to write a C++ program that calculates the area of a rectangle and tests by calling the functions readData, area, and printOutput. All variables should be of type double. Make sure to identify call-by-reference and call -by-value variables. The program will utilize 3 (three) functions:
• The void function readData that will ask the user to enter the width and length of the rectangle. The values of these variables will be passed through the parameters back to the main program.
• The double function area that calculates the area of the rectangle using the values of width and length passed from main to the function through the parameters. The function will return the value of calculated area. Use the following equation to calculate the area:
rectangleAreaValue = width * length
• The void function printOutput that receives the values of length and width from the main program through the parameters and first calls the function area then prints the length, width and rectangleAreaValue.
Explanation / Answer
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double L=1, double W=1);
double getLength() const;
void setLength(double L);
double getWidth()const;
void setWidth(double W);
double area (double, double);
void printoutput(double L, double W);
private:
double length, width;
};
Rectangle::Rectangle(double L, double W)
{
setLength(L);
setWidth(W);
}
void Rectangle::setWidth(double W)//I'd like to change this to a do/ while loop??
{
if ((W < 0.0) || (W > 20.0))
{
width = 1.0;
}
else
{
width = W;
}
return;
}
double Rectangle::getWidth()const
{
return width;
}
void Rectangle::setLength(double L)//same here-do/while loop?
{
if ((L < 0.0) || (L > 20.0))
{
length = 1.0;
}
else
{
length = L;
}
return;
}
double Rectangle::getLength()const
{
return length;
}
double Rectangle::area(double L, double W)
{
return(L*W);
}
void Rectangle::printoutput(double L, double W)
{
double length,width;
cout<<length<<width;
double area();
}
int main()
{
double length, width;
Rectangle MyRectangle;
cout << "Enter The Length Of The Rectangle: ";
cin >> length;
cout << "Enter The Width Of Rectangle: ";
cin >> width;
cout <<"The area of the rectangle is : "<< MyRectangle.area(length, width) << endl;
return 0 ;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.