[comp.lang.c] Object oriented programming

ADLER1%BRANDEIS.BITNET@wiscvm.wisc.EDU (09/01/87)

A friend of mine has advised me that C++ has object oriented programming
features. It seems desirable to have such options but it is not possbile
for me to switch to C++ for a number of reasons. On the other hand, maybe
it is possible to obtain object oriented programming features with the
C compilers I do use. Perhaps this could be accomplished through certain
programming practices, by writing some auxiliary programs and using them
or otherwise.

My admittedly naive question is this: is it possible to do this without
essentially reinventing object oriented programming ?

Allan
ADLER1@BRANDEIS.BITNET

kim@geovision.UUCP (Kim Fillmore) (09/04/87)

In article <9105@brl-adm.ARPA> ADLER1%BRANDEIS.BITNET@wiscvm.wisc.EDU writes:
>maybe it is possible to obtain object oriented programming features with the
>C compilers I do use. Perhaps this could be accomplished through certain
>programming practices, by writing some auxiliary programs and using them
>or otherwise.
>
>My admittedly naive question is this: is it possible to do this without
>essentially reinventing object oriented programming ?
>
>Allan
>ADLER1@BRANDEIS.BITNET

The answer to your question is both yes and no. Yes, an *object-oriented*
approach to writing programs can be taken in any language.  By structuring
your code with the data, as opposed to the operators, as the active players,
you tend to produce code that is structured and modular.  Similarly, data 
hiding and the separation of non-primitive routines from those that actually
depend upon the data representation being used, can also be done in C, or the
language of your choice (this is comp.lang.c though... :-) ).

However, the two main things that give object-oriented languages like C++ and
Smalltalk their power are polymorphism and inheritance.  Polymorphism is a
characteristic of object-oriented languages whereby different objects respond
to the same message with different behavior.  It lets you use entirely new
classes of objects in existing applications, as long as the objects implement
the message protocol required by the application.  This faciliates the use of
generic code.  Inheritance uses the class hierarchy to permit derived classes
to inherit methods (modules, routines, ... insert your favourite term) from
base classes, without having to reimplement them.  At the same time, the 
derived classes can add new methods, or reimplement any of the base class 
methods it wishes to change.  Thus, when an object of a particular derived
class receives a message, it first searches its own methods for a match, and
if it can't find one, it passes the message to its immediate base class, and
so on up the inheritance tree, until the correct method is found, or the root
is reached.

So, to implement anything more than a simple subset of an inheritance 
mechanism or polymorphism, you would basically have to do some "reinventing".
However, this is not to say you can't acheive some of the effects and
benefits of object-oriented programming by faking it in C.  An excellent
book on the topic of writing object-oriented programs is:
			Object-Oriented Programming - An Evolutionary Approach
			Brad J. Cox (Productivity Products International)
			Addison-Wesley, 1986
This is a good book to read for anyone wishing to become more familiar with
the whole concept of object-oriented programming, and gives practical advice
on the writing of such programs.  Most of the example are given in 
Objective-C (a product of PPI), but it is still useful for those interested in
writing o-o programs in any language. BTW - As a matter of personal opinion, I
prefer C++ over Objective-C, but that discussion doesn't belong here...

---Kim

(Except for stray unicorns, no one else is responsible for my opinions.)
-- 
Kimberly Fillmore     (613) 722-9518 |  Duct tape is like the Force - it has
GeoVision Corp, 350-1600 Carling Ave |     a light side and a dark side, and 
Ottawa Canada K1Z 8R7                |     holds the universe together.
UUCP: ...!utzoo!dciem!nrcaer!cognos!geovision!kim

u2@hpcupt1.HP.COM (C Class) (09/04/87)

/ hpcupt1:comp.lang.c / ADLER1%BRANDEIS.BITNET@wiscvm.wisc.EDU /  2:31 pm  Sep  1, 1987 /
A friend of mine has advised me that C++ has object oriented programming
features. It seems desirable to have such options but it is not possbile
for me to switch to C++ for a number of reasons. On the other hand, maybe
it is possible to obtain object oriented programming features with the
C compilers I do use. Perhaps this could be accomplished through certain
programming practices, by writing some auxiliary programs and using them
or otherwise.

My admittedly naive question is this: is it possible to do this without
essentially reinventing object oriented programming ?

Allan
ADLER1@BRANDEIS.BITNET
----------

you can do object-oriented programming with vanilla C but it takes a lot
of knowledge of how OOP is supposed to work and a lot of code.  we have
been working on some code that implements a quasi-Objective-C OOP model
without the overhead of Objective-C's general-purpose messaging scheme or
the (stupid) Objective-C garbage-collection mechanism.

wanna hint?  use C++ it will be easier in the long run...

Steve Witten                   UUCP:  ...!hplabs!hpda!hpdsla!hpiacla!steve
Hewlett-Packard Co.            SMTP:  steve@hpiacla.HP.COM
Industrial Application Co.

lmiller@venera.isi.edu (Larry Miller) (09/07/87)

In article <5940001@hpcupt1.HP.COM> u2@hpcupt1.HP.COM (C Class) writes:
>/ hpcupt1:comp.lang.c / ADLER1%BRANDEIS.BITNET@wiscvm.wisc.EDU /  2:31 pm  Sep  1, 1987 /
>  [Questions on how to dow object oriented programming in C, and
>   what C++ supports.]

I recommend the following highly.  It gives good general ideas on
information hiding, and allows for OOP.

%A Dick Grune
%T Generic Packages in C
%J SIGPLAN Notices
%V 21
%N 8
%P 31-39
%D August, 1986
%K Software engineering