Wednesday, November 21, 2007

Object Oriented Programming in C - Proof of Concept (Part 2)

Explanation of Code in the first part

Let us now try to understand the C code that was put in the first part of this post. The files new.h and class.h are files that will be common for any user-defined objects (i.e. stacks, queues, complex number or any class that you would like to implement in C). The purpose of new.h is that it declares two functions new and delete that will be used to create and destroy, respectively, any type of objects. The declaration of new tells us that its first argument is a pointer to the type of object that is going to be created. The ellipsis indicates that it can take a variable number of arguments. The arguments will be fetched and passed to the constructor function (here Stack_ctor) of the particular object. Now, there are still some issues like multiple constructors but believe me, that can be handled. I haven't given the implementation of new or delete as I want to convey to you the idea and not bog you down with the details. delete just accepts the pointer to the object to be destroyed, calls the object's destructor (here Stack_dtor) and finally frees the pointer to the object. Yes, we do not have the facility of automatic destruction like the one provided in C++, this is a limitation, you need to call delete explicitly.

Now for class.h, this contains a structure Class, which is nothing but a container for information common to any object. It is something similar to the Object class in Java from which all objects are derived. It contains the size of the object, and then four function pointers. These are for the constructor, destructor, clone and differ functions (see comments for clone and differ functions, they don't affect much the discussion here). Every object is now going to have a pointer to this Class structure (and more importantly, it is going to be the first member of every object, since it gives us an interface to access the constructor, destructor functions of any object).

Now, the header file stack.h, contains a void pointer named Stack. The user needs to be know this header file only for his task. The pointer Stack points to information represented by the structure Class. Later when we allocate an object using new, new will copy this information into the object and then call the object's constructor (here Stack_ctor). Keeping Stack a void pointer does not reveal any details to the user. Then, there are prototypes for the push and pop functions. Yes, you can point out that the void pointer arguments cannot be checked at compile time for the correct type. I have seen some compilers do that for the void pointers but yes this cannot be taken to be a standard behaviour. Proper runtime checks can be done with graceful exit in case of error.

Now, coming to the implementation of the stack in the file stack.c. First we see a structure Stack declared at the top. This structure holds the information generic to every object (the const void pointer class points to the Class structure for this object, this pointing is accomplished by the new function) and it may also hold some variables. Then we have the variables declared with static storage type (this is done to get the effect of private variables in classes in object-oriented languages, same can be done for functions). Next come definitions for the constructor, destructor, clone and differ functions for the stack. Finally we make an instance of the Class structure, _Stack, and then make the our pointer Stack from stack.h file to point to it. Later, new will use the pointer Stack to carry out complete initialization as I described earlier. The file ends with definitions for push and pop functions.

1 comment:

Wayne O. said...

hey thanks for sharing the knowledge that so many find not applicable in the C programming language. I discovered this exact concept by simply Google searching for "object oriented c". Once I did that, I discovered the free book by Axel-Tobias Schreiner. However, what I did not find was the diskette he referred to in the chapter on ADT's. Specifically on the section where he discusses the new.h library. But, thanks to your overview, as well as Axel-Tobias Schreiner overall, of what that consisted of, I am now much more comfortable with coding my current project in C with an object oriented approach.

From,
Wayne O.