Question on shapes

matheus matheu at gmail.com
Tue May 17 05:08:30 UTC 2022


On Tuesday, 17 May 2022 at 04:37:58 UTC, Ali Çehreli wrote:
> ...
> 2) If you want to have a shape hierarchy, then you can start by 
> defining its interface and implement that interface by concrete 
> shape types. Drawing is ordinarily handled by member functions:
> ...

Hi Ali, I'm not the author but I have a question, in your second 
example, let's say that sometimes it would be required to "draw" 
with some scale factor, so (As a newbie) I would do something 
like this:

interface Shape {
   void draw();
   void draw(float scale);
}

Then in Circle class:

   void draw(float scale) {
     writeln("This circle's radius is ", radius*scale);
   }
   void draw(){ draw(1); }

Then in Rectangular class:

   void draw(float scale) {
     writefln!"This rectangle's dimensions are 
%sx%s."(width*scale,height*scale);
   }
   void draw(){ draw(1); }


So calling shape.draw() would draw with the original scale, 
otherwise you could call as shape.draw(some_scale);

The problem is these are just 2 shapes and it could be much more, 
so it would required to repeat all this.

In D there would be a better way to do such thing?

Thanks,

Matheus.


More information about the Digitalmars-d-learn mailing list