An interface in C++

Last time I wrote about an interface in C language. Today, I will describe how to implement an interface in C++.

In C++, like in C, we haven’t got any special keyword for an interface. We create it using an abstract class – class with at least one pure virtual function. Pure virtual function hasn’t got its own definition – implementation. It is only a declaration within the class, like below:

virtual void fun() = 0;

Pure virtual function has to be implemented in a corresponding derived class. It will be checked during the linking process. Typical interface is presented below

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

/**
* Interface example - abstract class
*/
class CW_Interface_Body
{
public:

    /**
     * Default constructor
     */
     CW_Interface_Body() {};

    /**
     * Default destructor
     */
    virtual ~CW_Interface_Body() {};

    /**
     * Some function
     *
     * @param CW_data - shared data
     */
    virtual void CW_fun( CW_Interface_Data& CW_data ) = 0;

    /**
     * Destroy shared data
     *
     * @param CW_data - shared data
     * @param CW_error - error
     */
    virtual void CW_destroy( CW_Interface_Data& CW_data ) = 0;

};

Full code with the example application, which process the personal data, are available on my repository CWorking.

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