How to fix the 'variable' was not declared in this scope error?

Alinoor.Sakib

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();    
}   
therainmaker

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.

edited at
0

Comments

0 comments
Login to comment

Related