On Tuesday, 17 May 2022 at 00:10:55 UTC, Alain De Vos wrote:
Let's say a shape is ,a circle with a radius ,or a square with a rectangular size. I want to pass shapes to functions, eg to draw them on the screen,
draw(myshape) or myshape.draw();
But how do i implement best shapes ?

In addition to all the answers, just remember that if you want to make a square class then it should inherit from the shape class and not the rectangle class.

It might seem like the obvious rule is for square to inherit rectangle, since a square is a rectangle, but it's only true on the surface, not in functionality.

An example is below:

```d
void changeRectangle(Rectangle rectangle, int amount)
{
  rectangle.length = rectangle.length + amount;
}

...

// While this works, then it's functional wrong as you must change both the length/height of a square, since they cannot differ.
changeRectangle(new Square(100, 100), 50);
```

You might think that it's easy to just not call changeRectangle with a square, but what if you store the rectangles in a list, map, get it from an external data source etc. then it starts being more and more complex and for no reason at all!

I know your post really isn't about such things, but I think it's a good thing to learn already.

Reply via email to