Get the width and height of a shape as user input I made this program to understand inheritance better.

Now that I'm at this basic level it's not really a problem but I can see this coming in the future and so I'm asking what's the best method to ask the user to insert the width and height of a rectangle (in this case).

The code I'm providing includes the simple way, by simply asking the user and inserting the number in a variable, then passing the variable to the setter inside the object (rect1).

I think when I will add up more shapes this can become a little confusing, is there a better way I can ask the user to input the number?

Since I'm asked to provide more context:
I'm generally asking what's the best or a better method to ask input from the user that is going inside an object.

This is my version of it, that I think is good in this simple level but it will get messy with more shapes or generally more classes, I use 2 variable to ask for 2 dimensions, I will need 1 variable for every dimension and when I will create an object if I pass the wrong variable the program will work anyway, giving the wrong result.

Another way that I thought is insert an "askSomething" method in each class so that I don't need variables and every time a new object is created the io stream will be asked automatically.
The "AskSomething" method is identical to a setter except it has cout and cin already inside

I don't really came up with other ideas.

The code I'm providing is a simple program that by asking height and width of a Rectangle calculate its Area, but of course by the class name Rectangle, the member names height and width and the method name getArea was pretty obvious.

main.cpp:

int main() {
    Rectangle rect1;
    int width, height;

    std::cout &lt;&lt; "Insert the width of the rectangle: ";
    std::cin &gt;&gt; width;

    std::cout &lt;&lt; "Insert the height of the rectangle: ";
    std::cin &gt;&gt;height;

    rect1.setHeight(height);
    rect1.setWidth(width);

    std::cout &lt;&lt; "The area of the rectangle is: " &lt;&lt; rect1.getArea() &lt;&lt; std::endl;


    std::cout &lt;&lt; "Drawing: " &lt;&lt; std::endl;
    std::cout &lt;&lt; " ";
    for (int i = 0; i &lt; width+1; ++i) {
        std::cout &lt;&lt; "-";
    }
    std::cout &lt;&lt; std::endl;
    for (int j = 0; j &lt; (height+1)/2; ++j) { // divided by 2 to have a better proportion
        std::cout &lt;&lt; "|";
        for (int i = 0; i &lt; width+1; ++i) {
            std::cout &lt;&lt; " ";
        }
        std::cout &lt;&lt; "|";
        std::cout &lt;&lt; std::endl;
    }
    std::cout &lt;&lt; " ";
    for (int i = 0; i &lt; width+1; ++i) {
        std::cout &lt;&lt; "-";
    }

    return 0;
}


Shape.h:

class Shape {
public:
    int getWidth() const {
        return width;
    }

    void setWidth(int width) {
        Shape::width = width;
    }

    int getHeight() const {
        return height;
    }

    void setHeight(int height) {
        Shape::height = height;
    }

protected:
    int width, height;
};


Rectangle.h:

class Rectangle: public Shape {
public:
    int getArea(){
        return width*height;
    }
};


Thanks to everyone! :)
I think you have chosen really bad way to model the inheritance relation between a rectangle and a shape.

I believe that every shape has an area. Unlike your Shape class, which does not allow this, only the Rectangle class does.

On other hand, I believe that not all shapes have width and height. If anything, they have a smallest outer rectangle which has a width and height. Unlike, your Shape class which always has width and height and allows to set it to any arbitrary value.

You should turn it inside out.

You can still have the shape be able to give you the smallest outer rectangle (with its width and height), but implementing that method for anything except the Rectangle (where it would just return copy of itself) would be a bit more complex and providing such code for you would go far beyond a code review :) But that way you can provide the width/height pair in a read only manner, making sure that it cannot be modified for shapes where it would lead to corruption (like setting width!=height for a circle).

I would also rather use double for the numeric values as you would soon find out that areas of shapes are not always integers even for shapes which themselves are defined with integers (although this is not the case for rectangles).

class Rectangle;

class Shape
{
public:
  virtual ~Shape() {}
  virtual double getArea() const = 0;
  virtual Rectangle getSmallestOuterRectangle() const = 0;
};

class Rectangle : public Shape
{
public:
  double width, height;

  Rectangle(double width, double height) : width(width), height(height) {}
  virtual ~Rectangle() {}
  virtual double getArea() const {return width * height;}
  virtual Rectangle getSmallestOuterRectangle() const {return *this;}
};


Further obviously, you cannot create all shapes from just height and width.
If I wanted to create a circle, two variables are too much.
If I wanted to create an arbitrary triangle, two variables are not enough.

In any way, having the Shape/Rectangle/Circle/Triangle classes know anything about streams is a mistake. You can always read the values into variables and pass them through constructor or update the respective property (if you made them public, which is ok here I believe, no setters/getters are needed).