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