Does separation of interface from implementation within classes provide advantage? If yes, then how.

In C++, interface classes are pure virtual classes. There is no such thing as an interface class per-se in C++, unlike Java and some other languages. The advantage is that you can have default behaviors (not all methods need to be pure virtual) that can be used or overridden by child classes. It can help reduce the amount of redundant code significantly, while allowing specialization for the child class as necessary. My main objection to interface classes in Java and other languages is that if a class is derived from an interface class, it MUST implement each method, whereas in C++ you can have implemented methods in the root class (pure virtual) that will suit 90% of the derived classes, eliminating this major source of errors in Java code. In fact, in C++, you can create a pure virtual class like this:

class virtualfoo {
private:

    int m_int;
    float m_float;

public:

    virtualfoo(int i = 0, float f = 0.0) : m_int(i), m_float(f) {}
    virtual ~virtualfoo() = 0; // This makes it a pure virtual class.

    int getInt() const { return m_int; }
    float getFloat() const { return m_float; }
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.