An email conversion on the CLUG mailing list got on the topic of doing Object Oriented program (with runtime polymorphism) in a non-OO language like C. What would it take, what would the code look like and is it worth it?
Here is a small example of a OO program that uses the following classes:
|<<interface>>| | Shape | A | +---------+-------+ | | |Rectangle| |Circle|
Shape provides an abstract interface that supports Draw(), MoveTo(x,y), and RMoveTo(dx,dy) methods. Rectangle and Circle support that interface as well as defining their own specific methods (e.g. SetRadius(newRadius) for Circle).
Note: My intention was to have the Shape class be a pure interface, with all implementation in the derived classes. A few of the examples move the MoveTo and RMoveTo methods implementations into the Shape class. This certainly reduces redundency and is an acceptable optimization for this example.
I initially produced the C and C++ versions of the program. Someone on the CLUG list offered a Perl version. So I thought that it would be interesting to see this small problem written in several different OO languages (or non-OO languages with manual polymorphism).
Languages in this table provide some sort of support for the Object Oriented programming.
|Component Pascal||Chris Rathman|
|Groovy (version 1)||Marcelo da Silva Gomes|
|Groovy (version 2)||Marcelo da Silva Gomes|
|Io (version 1)||Steve Dekorte|
|Io (version 2)||Marcelo da Silva Gomes|
|Ioke||Marcelo da Silva Gomes|
|PHP 5||Marcus Cavalcanti|
|Python (version 1)||Monty Stein|
|Python (version 2)||Rico Schiekel|
|Ruby (version 1)||Chris Rathman|
|Ruby (version 2)||Guy N. Hurst|
|Visual Basic||Csaba Urbaniczky|
|Visual Foxpro||Chris Rathman|
Languages in this category are general purpose languages that don't provide any features for Object Oriented programming.
|ANSI C||Jim Weirich|
|Lua||Marcelo da Silva Gomes|
Languages in this category are special purpose languages. This includes things like scripting languages and data base languages. The entries in this category really stretch the envelope to be able to do polymorphism.
|DOS Batch||Scott Anderson|
|Shell Script||Monty Stein|
Bryce Jacobs offers a XBase version. Unfortunately, Bryce's version only offers a limited form of polymorphism (where the base class must enumerate all subclasses). Bryce also offers some general criticism of the OO paradigm which you might enjoy.
Anyone can submit a version, just send it to me, Jim Weirich, at email@example.com.
If you do send in an example, the following guidelines will make it easier on my end:
This article is translated to the Serbo-Croatian language by WHG Team .