skip to Main Content

What is the error in this code?

I tried this code in Visual Studio, and I’m facing an error while running this code. Visual Studio is showing the error on line 29.

Point out the error and also explain it, please.

#include <iostream>
using namespace std;

class Rectangle {

    int width;
    int height;

public:
    Rectangle(int w, int h) {
        width = w;
        height = h;
    }

    void setDimensions(int w, int h) {
        width = w;
        height = h;
    }

    int calculateArea() {
        return width * height;
    }
};

int main() {
    Rectangle myRectangle(3, 4);

    cout << "Initial Area: " << myRectangle.calculateArea() << endl;
    myRectangle.width = 5; 

    cout << "Updated Area: " << myRectangle.calculateArea() << endl;

    return 0;
}

3

Answers


  1. .width is private member of class. It cannot be accesed from outside of the class. You should use setDimensions() to change it.

    Since your class hasn’t any members that depend on width, nothing will break if you make it public.

    If for example you wish to keep area as a class member to avoid calculating it every time the area is requested, then you must keep width private because any change of width will invalidate precalculated value of area. In this case keep it private and add special methods for class: one to get width wtithout right to change it and one that allows to set width and updates area with each change. (You only have second method in form of setDimensions())

    On unrelated note:

    1. Dimentions of rectangle cannot be negative so store them as unsigned value
    2. calculateArea() with high probability will encounter integer overflow. Consider using long long as a return value (which is usualy stored in 8 bytes instead of 4 bytes for int).
    Login or Signup to reply.
  2. The error in your code is on the line myRectangle.width = 5;. In the main function, you are trying to access the width member variable directly, but it is declared as private within the Rectangle class. Direct access to private members from outside the class is not allowed.

    To fix this, you should use the public member function setDimensions to update the dimensions of the rectangle.

    Login or Signup to reply.
  3. The error in the code is that the width and height member variables of the Rectangle class are private. Therefore, they cannot be accessed directly from outside the class.

    In the main() function, the line rectangle.width = 5; tries to access the width member variable of the rectangle object, which is not allowed and causes a compile-time error.

    To fix this error, you can add a public member function to the Rectangle class that allows you to set the width and height member variables. For example, you can add the following function to the Rectangle class:

    void set_dimensions(int w, int h) {
        width = w;
        height = h;
    }
    

    Then, you can use this function to set the width and height of the rectangle object in the main() function, like this:

    myRectangle.setDimensions(5, 6);
    

    This will set the width of the rectangle object to 5 and the height to 6, and the program will run without errors.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search