Object oriented programming in C

Everyone who ever wrote something in C certainly knows that it is a structural language. However not everyone know that object oriented programming in C is possible and quite simple. To start talking about this we need to know what is an interface and how to implement it.

An interface is a set of functions and rules which can be used to communicate with some specified object. An interface may be common for many different modules which have similar functionality. In my opinion, the most important purpose of the interface is a separation between control plane and algorithms. It is not hard to imagine that if we define interfaces for our application we can implement each module separately. The interfaces help team to cooperate and divide their work. Developer, who knows how to use all required modules before their implementation, will be more confident and will make less mistakes. Well organized project gives a greater chance to finish it earlier without sacrificing its quality.

That’s theory, let’s have some practice. The code below shows one of the way to define an interface in C language. You can download full version of this code with additional examples from my repository CWorking.

/**
* Forward declaration of data required by interface functions
*/
struct CW_Interface_Data;

/**
* Interface example
*/
typedef struct CW_Interface_Body
{
    /**
     * Initialize interface body
     *
     * @param CW_interface - interface body
     * @param CW_error - error
     */
    void (*CW_init)( struct CW_Interface_Body* const CW_interface, CW_Error* const CW_error );

    /**
     * Some function
     *
     * @param CW_data - shared data
     * @param CW_error - error
     */
    void (*CW_fun)( struct CW_Interface_Data* const CW_data, CW_Error* const CW_error );

    /**
     * Destroy shared data
     *
     * @param CW_data - shared data
     * @param CW_error - error
     */
    void (*CW_destroy)( struct CW_Interface_Data* const CW_data, CW_Error* const CW_error );

} CW_Interface_Body;

An interface was implemented as a structure with pointers. A set of functions assigned to the pointers, directly describes functionality of specified module. Furthermore, we could imagine some function assigned to CW_init which will set CW_fun and CW_destroy. It is something very similar to constructor known from C++ language.

In an example available on my repository, I create two objects, which process personal data in two different ways. Thanks to the common interface we could control them using the same commands. It is very important when we are working with a module written by someone else. We do not need to worry how something works in details. We know how to use some object and that is all we need.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s