0
Why output is zero !!
#include <iostream> using namespace std; class rectangle { private: float lenth; float width; public: void setlength(float len) { if (len>=0) len=lenth; else cout<<"error,positive value only"; } float getlength() { return lenth; } void setwidth(float w) { if (w>=0) w=width; else cout<<"error,positive value only"; } float getwidth() { return width; } float getarea() { return lenth*width; } }; int main() { rectangle box; box.setlength(5.5); box.setwidth(46.7); cout<<"area of rectangle equal " <<box.getarea()<<endl; return 0; }
2 ответов
+ 1
Because in setlength and setwidth method you have to assign the passed parameter variable to class variables but you done it reverse.
You need to replace: len=lenth with lenth=len
w=width with width=w
Here is the corrected code::
#include <iostream>
using namespace std;
class rectangle
{
private:
float lenth;
float width;
public:
void setlength(float len)
{
if (len>=0)
lenth=len;
else
cout<<"error,positive value only";
}
float getlength()
{
return lenth;
}
void setwidth(float w)
{
if (w>=0)
width=w;
else
cout<<"error,positive value only";
}
float getwidth()
{
return width;
}
float getarea()
{
return lenth*width;
}
};
int main()
{
rectangle box;
box.setlength(5.5);
box.setwidth(46.7);
cout<<"area of rectangle equal "
<<box.getarea()<<endl;
return 0;
}
0
Thank you