This might sound like a noob question, but that is because I am a noob. Anyways, I was just wondering what's good about inheritance and polymorphism in game development? I'm learning about them now and I can understand it mostly, but I can't wrap my head around an important use and any help would be very, very appreciated.

Thanks in advance,
Ichi

Recommended Answers

All 2 Replies

Well there is a very simple example (in C++):

//create an interface class for a visible objects.
class VisibleObject {
  public:
    virtual void render() = 0; //pure virtual function that renders the object.
};

class Box : public VisibleObject {
  private:
    ..
  public:
    void render(); //implement this such that it renders a box.
};

class Cylinder : public VisibleObject { .... };

class Model3D : public VisibleObject { .... };

//etc..

//later on:
class Scene {
  private:
    vector<VisibleObject*> visible_object_list;
  public:
    void render() {
      for(int i=0; i<visible_object_list.size(); ++i)
        visible_object_list[i]->render(); //polymorphic call to render whichever type of visible object this is.
    }; //the implementation should be in the cpp file.. but inlined for sake of example.
};

I can't wrap my head around an important use and any help would be very, very appreciated.

I'm an old school programmer. When I started the standard of the day was top down programmer. You would right the code, keeping it separate from the data stack, and it worked fine for some problems. If you wrote a routine that did a whiz bang job of taking care of a problem space, we patted ourselves on the back for a good job, and then moved on.
This meant having to re-create the wheel every time we wanted to solve a problem. Long story short, it was tedious to write programs that way, and a waste of good resources. Code was not very much of a re-usable thing and a whole lot of rewriting was done over and over. Enter OOP programming and that was a whole different ballgame. Now you could think of the code and data as an object. Each object could be thought of as a separate code and data together. Think of something simple as a canvas for a moment. Your going to paint something on it. All kinds of shapes make up a picture you want to draw. So you design objects to do things like make a circle, triangle, square, etc. each one can then be re-used as is, in many types of pictures.

with polymorphism, you can take the basic shape from the code and data above and re-use to make other shapes like rectangles, trapezoids and so forth, just by making the new objects an extension of one of the others by inheritance, and then extending the extra functionality. As an example, you already have a square coded. it will take some data provided in the interface such as x,y position, length width. All you have to do is call the object drawSquare(x,y,length, width). You can then declare a trapezoid or other things by declaring the base class square and adding new data and functions to make a trapezoid. The new function inherits the functions, data, and methods of the base class, while allowing you to add only what you need that is different from this base class.

It will be a little intimidating at first, but as you work with it more and more, it becomes easier. I promise, that if you don't give up, one day a light bulb will click in your brain, and you'll say to yourself "AHA" and form that point on you'll wonder what you ever did without it....

Sorry to be so long winded, and I admit its a simplification of a complex problem, but stop and think about it for a moment. Procedural programming still has a place for some problems, but OOP is here to stay. Keep plugging away and you will do fine. DON'T give up no matter what. Programming is fun! I really enjoy it, and I can assure you many more do as well.....

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.