(C++) I wrote this program, and it should calculate the area and perimeter of a
ID: 3844924 • Letter: #
Question
(C++)
I wrote this program, and it should calculate the area and perimeter of a plot. However, when I run the program, the values that show up for area/boundary are giant numbers that don't make sense. Can someone point out where I'm writing the code incorrectly? Thanks!
#include <iostream>
using namespace std;
class Plot
{
public:
void setLength();
void setWidth();
void area()
{
cout << "Area of the plot is: " << (length*width) << endl;
};
void boundary()
{
cout << "Boundary of the plot is: " << (2 * (length + width)) << endl;
};
private:
int length;
int width;
};
void Plot::setLength()
{
length = 0;
}
void Plot::setWidth()
{
width = 0;
}
int main()
{
Plot myPlot;
int length = 7;
int width = 9;
cout << "Length of the plot is: " << length << endl;
cout << "Width of the plot is: " << width << endl;
cout << endl;
myPlot.area();
myPlot.boundary();
}
Explanation / Answer
class Plot
{
public:
//get and set functions declarations
void setLength(int l);
void setWidth(int w);
int getLength();
int getWidth();
void area()
{
cout << "Area of the plot is: " << (length*width) << endl;
}
void boundary()
{
cout << "Boundary of the plot is: " << (2 * (length + width)) << endl;
}
private:
int length;
int width;
};
//definitions of set and get functions
void Plot::setLength(int l)
{
length = l;
}
void Plot::setWidth(int w)
{
width = w;
}
int Plot::getLength()
{
return length;
}
int Plot::getWidth()
{
return width;
}
int main()
{
Plot myPlot;
//set the length and width of myPlot
myPlot.setLength(7);
myPlot.setWidth(9);
//get the length and width of myPlot
cout << "Length of the plot is: " << myPlot.getLength() << endl;
cout << "Width of the plot is: " << myPlot.getWidth() << endl;
cout << endl;
myPlot.area();
myPlot.boundary();
}
Output:
Length of the plot is: 7
Width of the plot is: 9
Area of the plot is: 63
Boundary of the plot is: 32
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.