flipCode - Tech File - Jeroen Bouwens [an error occurred while processing this directive]
Jeroen Bouwens
Click the name for some bio info

E-Mail: flipcode@jeroen.ws



   09/14/1999, Polymorphism and Inheritance


Well, there seems to be at least some interest in object design topics, judging by the positive reactions I received on my last techfile entry. Therefore, I’ll talk about this a bit more. First of all, here’s an example I encountered in real life which indicates one advantage of encapsulation (Which I talked about in my previous entry).

I wanted to keep track of the values of an array of booleans in what we call an inspection dialogue. In order to do this, the routine “UpdateDialogue” has to be called each time one of the array-elements is changed. Now, if the class had been nicely encapsulated, this array would be accessible only through a “Set….” function. This means the only place where I would have had to add the “UpdateDialogue” call was in that particulair set-function. Unfortunately, the array was a public member, accessed by other classes directly. This meant a complex, error-prone find/replace action was needed to update the 300+ places where the array was accessed.

I repeat here that in my view encapsulation is the most important aspect of OOP. Without it, class-reuse and the concepts of inheritance and polymorphism come to nothing.

Which brings me to the remaining two fundamental concepts of OOP, you guessed it: Inheritance and polymorphism. Probably all of you are familiar with inheritance, so I won’t explain it here. Polymorphism is less well known, and trickier to explain. Time for a classic example:

class CShapeBase
{
  public:
    virtual void Draw();
}

class CRectangle : public CShapeBase
{
  public:
    virtual void Draw();
}

class CCircle : public CShapeBase
{
  public:
    virtual void Draw();
}

class CTriangle : public CShapeBase
{
  public:
    virtual void Draw();
}
What happens here is that a pure virtual class (A class with only virtual functions and without any implementation) is used as a base for three descending classes. All thee descendants implement their own version of the “Draw” method, for obvious reasons. The nice thing is, a variable of type CShapeBase can become any of its descendant’s types. For example:

CCircle *Circle = new CCircle();
CTriangle *Triangle = new CTriangle();
CShapeBase *aShape;

main()
{
  aShape = Circle;
  aShape->Draw();

  aShape = Triangle;
  aShape->Draw();
}
The first call to aShape->Draw() draws a circle, while the second, completely and utterly identical call draws a triangle! It’s amazing! Now this is a rather trivial example, but the principle of polymorphism is equally applicable to more interesting subjects. How about a base class for 3D objects which declares a “Render()” method. You could use and mix any number of different rendering algorithms, by simply inheriting classes from this base-class and implementing a different “Render()” method each time.

Now you know a bit about OOP on a practical level, I think next time I will tell you something about high-level design patterns. Traditionally, a design pattern is defined as “A solution to a problem in a context”, which is quite an infuriating definition in my view. I’ll try to make it clearer next time.

As final words, the three building blocks of OOP are linked nicely by the following statement:
By using the mechanism of inheritance, and applying the concept of encapsulation, the goal of polymorphism is achieved.


Happy coding,
      Jeroen





  • 04/01/2002 - Thoughts On Software Development
  • 09/10/2000 - Observe Your Objects
  • 01/31/2000 - Factories
  • 09/14/1999 - Polymorphism and Inheritance
  • 08/14/1999 - Object Design
  • 06/04/1999 - OpenGL Mania
  • 05/04/1999 - 3D Clipping
  • 04/29/1999 - Introduction

  • This document may not be reproduced in any way without explicit permission from the author and flipCode. All Rights Reserved. Best viewed at a high resolution. The views expressed in this document are the views of the author and NOT neccesarily of anyone else associated with flipCode.

    Download The Sample Program: ManicMotion.exe

    [an error occurred while processing this directive]