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; }

10th Jul 2020, 9:50 AM
Mostafa Ibrahim
Mostafa Ibrahim - avatar
2 Respostas
+ 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; }
10th Jul 2020, 10:59 AM
Shobhith J B
Shobhith J B - avatar
0
Thank you
10th Jul 2020, 4:17 PM
Mostafa Ibrahim
Mostafa Ibrahim - avatar