Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!uc!uf.msc.umn.edu!ken From: ken@uf.msc.umn.edu (Ken Chin-Purcell) Newsgroups: comp.object Subject: Re: OOP in C Keywords: soc C oop Message-ID: <895@uc.msc.umn.edu> Date: 8 Dec 89 16:13:44 GMT References: <3356@hydra.Helsinki.FI> Sender: news@uc.msc.umn.edu Reply-To: ken@msc.umn.edu Organization: Minnesota Supercomputer Center Lines: 65 In article <3356@hydra.Helsinki.FI>, jokiniem@cs.Helsinki.FI (Jari Jokiniemi) writes: > There has been some discussion that one can use object oriented programming > techiques even when writing the programm with any conventional language. Since > the concept of what these OOP techniques really are in practise is not well > explained in any book we've found, the very idea of OOP has remained > fuzzy for us. > > Therefore I ask some help from the netlanders. Would someone > give some examples written in C or some other conventional language > about what OOP is in practice. I was inspired by a package called "oic" (Objects in C) that was posted in Macintosh circles about six months ago. oic was obviously written by a lisp programmer, and had some nice features. Unfortunately its method dispatching took two extra function calls, which I thought to be too slow for "C" work. It also looked like it would not port easily to our Cray. So I rolled my own :'). "soc" (Simple Object C) implements the object oriented features found in Object Pascal, simply through style. For cross reference, check out how the X window toolkit implements objects in C. As Ralph Swick (of project Athena) said, the X toolkit is object programming via cut and paste. Having written widgets, I didn't want to go the cut and paste route, since it's boring and creates many oportunities for programming errors. Besides, the X toolkit approach doesn't lend itself to method calls. So I used the following strategy: for a new class, define new instance variables in the .h file, methods in the .c file. The .h file #includes at the end a .oh file, and the .c file #includes a .oc file. The functions in the .c file are marked with either "method", to indicate a new method, or "override" to indicate the redefinition of a superclass's method. "method" and "override" are #defined to nothing, so the cc compiler ignores them. Function names are written as "classname_methodname". I then wrote a small program, soc, to scan the .c file and write the .oh and .oc files. The .oh file defines the class and instance structures (as structs), and the .oc file initializes the function pointers. Method calls are handled by function pointers. soc also writes out a #define for each new method. For example, for the Draw method, first mentioned in class Shape, the following would be in shape.oh; #define _Draw(self) (*(self)->class->Draw)(self) We could then later say Shape someKindOfShape; _Draw(someKindOfShape); In essence I have the preprocessor and soc do the cut and paste for me. To all you I-need-every-feature oop fans: Yes, I know what I am missing. However, I have the basic features of Object Pascal, I am writing in C, I debug the C code I wrote, it works on the Cray, and it's fast. soc takes about a second or two to scan and write files on a Sun 3/60. For now, that's enough :'). If there is any interest out there, I can clean soc up and mail it out. Just send me a note. Ken Chin-Purcell (aka ken@msc.umn.edu) Minnesota Supercomputer Center