this code shows error 'len' and 'wid' was not declared in this scope how can I fix it?
class Shape {
private:
double length;
double width;
public:
Shape(double len, double wid) {
length = len;
width = wid;
}
double getArea() {
return len*wid;
}
};
int main() {
Shape Shape(2,4);
cout << Shape.getArea();
}
Your getArea()
function should return length*width
.
len
and wid
are the local variables defined in the constructor whereas length
and width
are class variables which can be accessed by other functions. So len
and wid
go out of scope when the definition of the constructor ends.
Collected from the Internet
Please contact javaer1[email protected] to delete if infringement.
Comments