Some words about OOP in plain <tt>C</tt>



next up previous
Next: Naming conventions Up: A reference Guide Previous: A reference Guide

Some words about OOP in plain C

When designing Unifpack it was noticed that an object oriented approach will simplify things enormously, instead of using a traditional OO programming language I decided to make it in plain C partly because I didn't feel confident in any other programming language and partly as an exercise on how to implement OO in a traditional language gif, although many people are quite familiar with OOP and its implementation (at least at the very simple level of Unifpack) I think that some people will benefit from what follows.

Unifpack is ``object based'' that is, the basic unit of programming are objects defined by some access functions and not by the data they contain, the ``user'' doesn't need to know the way each object is represented, he only needs to know those access functions (often called the ``interface'') furthermore, the access functions limit the way in which an object can be used, thus making the keep of ``consistency'' easy. The most natural way to represent complex data in C is using struct's, but we have to avoid direct access to to the fields of an structure (object), otherwise the interface can be superseeded by the user. This is achieved by the use of a simple but efficient data-hiding mechanism. As an example let us consider the Word class gif, this class will represent words in certain generators of type Symbol that we don't need to know right now a possible structure is

where Card is simply an unsigned integer, array_size is the size of factor, size is the number of elements in factor that are being used. Instead of that we define

typedef struct __Word *Word;

the type is well defined because the size of a pointer is well known, routines that only have this type cannot access the fields in Word, because they do not even know how are called those fields, on a separate file, for instance word.ph, gif we define the structure __Word as

in this way the access functions can know what are the fields and can manipulate them.



next up previous
Next: Naming conventions Up: A reference Guide Previous: A reference Guide



coryan@mat.puc.cl