Hello everyone! I am trying to use polymorphism to have a little physics simulator that draws objects to the screen.
Each object has properties like mass, friction, position, velocity, acceleration and such similar things, as well as a pointer to a SHAPE object which describes the shape of the object.
The SHAPE class by itself has nothing in it, but I have several classes that inherit from shape.
I have a Circle class that stores the radius. I have a Rectangle class that stores the width and height. I'm also going to implement more classes, but i'm working with these for now.
A problem I'm encountering is drawing these classes. I understand that I could easily have the SHAPE class have a virtual method for drawing the shape, and simply call that method on the pointers later. However, I'm trying to keep my graphics and physics as separate as possible.
I do have functions for drawing circles and rectangles that work just fine when I give them the right information. For example,
drawCircle(double radius) would draw a a circle to the screen, and drawRectangle(double width, double height) would draw a rectangle. What I'm trying to accomplish is to have a drawShape(SHAPE* s) function that will call the appropriate draw function based on what type the shape really is.
Right now, I have drawShape(SHAPE* s), drawShape(Circle* c), and drawShape(Rectangle* r).
I was hoping to just call drawShape(pointer) and have the appropriate function be called automatically based on what the pointer really points to. But instead, the SHAPE function (default one that just draws a dot) is called all the time, since the pointer is of type SHAPE (even though it points to a Circle or Rectangle).
How would I make this polymorphism work for nonmenber functions without having a virtual draw() function in each shape?