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.

General Purpose non-OO Languages

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

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.

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:

If you are going to submit a language that is already on the list, make sure the new version is different in an interesting way. (In other words, minor formatting differences or variable renamings are not interesting)

The code may be presented in as many files as need for your language. If you wish to have the files sorted in a particular order, prefix the filename with a number and an underscore. (e.g. 01_shape.c, 02_rect.c).

Include the output of the program in a file named '/articles/poly/output.txt'.

Package the whole thing together for emailing in a tar or zip file.

If you would rather have a blog or website URL linked to your name in the contributors table, just say the word. Otherwise I will link with your email address. If you would prefer no link, that's ok too.

Other Languages

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