OO Example Code

The Setup ...

An email converversion 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?

The Problem ...

Here is a small example of a OO program that uses the following classes:


              |<<inteface>>|
              |   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.

The Challenge ...

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). Anyone can submit a version, just send it to me, Jim Weirich, at jim@weirichhouse.org.

The Results ...

OO Languages

Languages in this table provide some sort of support for the Object Oriented programming.

LanguageOutputContributed By
Beta Output Chris Rathman
C++ Output Jim Weirich
Cecil Output Scott Anderson
CLOS Output Chris Rathman
Component Pascal Output Chris Rathman
Delphi Output Chris Rathman
Dylan Output Scott Anderson
Eiffel Output Jim Weirich
Itcl Output Chris Rathman
Java Output Jim Weirich
JavaScript Output Chris Rathman
LPC Output Scott Anderson
Modula3 Output Chris Rathman
Oberon Output Chris Rathman
Objective-C Output Chris Rathman
Ocaml Output Chris Rathman
Perl Output Bradley Kuhn
Php Output Chris Rathman
Python Output Monty Stein
Ruby (original)
Ruby2 (improved version)
Output Chris Rathman
Guy N. Hurst
Sather Output Chris Rathman
Simula N/A Chris Rathman
Smalltalk Output Chris Rathman
Visual Basic Output Chris Rathman
VBScript Output Chris Rathman
Visual Foxpro Output Chris Rathman

General Purpose non-OO Languages

Languages in this category are general purpose languages that don't provide any features for Object Oriented programming.

LanguageOutputContributed By
ANSI C Output Jim Weirich
Erlang Output Chris Rathman
Forth Output Jim Weirich
Haskell Output Chris Rathman
Icon Output Chris Rathman
Modula2 Output Chris Rathman

Special Purpose Languages

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.

LanguageOutputContributed By
AdvSys Output Scott Anderson
awk Output Scott Anderson
DOS Batch Output Scott Anderson
Foxpro Output Scott Anderson
Postscript Output Scott Anderson
Prolog Output Chris Rathman
sed Output Scott Anderson
Shell Script Output Monty Stein

Almost, but not quite ...

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.
Jim Weirich / jim@weirichhouse.org