3 Years
Discussion Span
Last Post by rubberman

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 {

    int m_int;
    float m_float;


    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; }

Edited by rubberman
