{ |one, step, back| }

OO Example Code

The Setup ...

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?

The Problem ...

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.

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

The Results ...

OO Languages

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

Language Contributed By
Beta Chris Rathman
Cecil Scott Anderson
Clojure Paul Barry
CLOS Chris Rathman
Component Pascal Chris Rathman
C++ Jim Weirich
D Leonardo
Delphi Chris Rathman
Dylan Scott Anderson
Eiffel Jim Weirich
Go Jim Weirich
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
Itcl Chris Rathman
Java Jim Weirich
JavaScript Chris Rathman
LPC Scott Anderson
Modula3 Chris Rathman
Oberon Chris Rathman
Objective-C Chris Rathman
Ocaml Chris Rathman
Perl Bradley Kuhn
Perl6 Jim Weirich
Php Chris Rathman
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
Sather Chris Rathman
Scala Giordano Scalzo
Simula Chris Rathman
Smalltalk Chris Rathman
Visual Basic Csaba Urbaniczky
VBScript Chris Rathman
Visual Foxpro 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.

Language Contributed By
ANSI C Jim Weirich
Erlang Chris Rathman
Forth Jim Weirich
Haskell Chris Rathman
Icon Chris Rathman
Lua Marcelo da Silva Gomes
Modula2 Chris Rathman
Scheme Jim Weirich

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.

Language Contributed By
AdvSys Scott Anderson
awk Scott Anderson
DOS Batch Scott Anderson
Foxpro Scott Anderson
Prolog Chris Rathman
Postscript Scott Anderson
sed Scott Anderson
Shell Script 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.

Submitting a new language

Anyone can submit a version, just send it to me, Jim Weirich, at jim.weirich@gmail.com.

If you do send in an example, the following guidelines will make it easier on my end:

Other Languages

This article is translated to the Serbo-Croatian language by WHG Team .