114

Possible Duplicate:
Preferred way to simulate interfaces in C++

I was curious to find out if there are interfaces in C++ because in Java, there is the implementation of the design patterns mostly with decoupling the classes via interfaces. Is there a similar way of creating interfaces in C++ then?

Community
  • 1
  • 1
helpdesk
  • 1,984
  • 6
  • 32
  • 50
  • 1
    Do you really need interfaces? `C++` has a vast support for templates. Just write your functions receiving templates, and ensure the passed types have the require methods to don't cause compile errors, so you don't impose your users to inherit from anything to use your methods. – ABu Oct 01 '19 at 18:38

3 Answers3

162

C++ has no built-in concepts of interfaces. You can implement it using abstract classes which contains only pure virtual functions. Since it allows multiple inheritance, you can inherit this class to create another class which will then contain this interface (I mean, object interface :) ) in it.

An example would be something like this -

class Interface
{
public:
    Interface(){}
    virtual ~Interface(){}
    virtual void method1() = 0;    // "= 0" part makes this method pure virtual, and
                                   // also makes this class abstract.
    virtual void method2() = 0;
};

class Concrete : public Interface
{
private:
    int myMember;

public:
    Concrete(){}
    ~Concrete(){}
    void method1();
    void method2();
};

// Provide implementation for the first method
void Concrete::method1()
{
    // Your implementation
}

// Provide implementation for the second method
void Concrete::method2()
{
    // Your implementation
}

int main(void)
{
    Interface *f = new Concrete();

    f->method1();
    f->method2();

    delete f;

    return 0;
}
Jason Basanese
  • 690
  • 6
  • 20
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
22

An "Interface" is equivalent to a pure abstract class in C++. Ideally this interface class should contain only pure virtual public methods and static const data members. For example:

struct MyInterface
{
  static const int X = 10;

  virtual void Foo() = 0;
  virtual int Get() const = 0;
  virtual inline ~MyInterface() = 0;
};
MyInterface::~MyInterface () {}
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 3
    What about a virtual destructor? – CB Bailey Mar 18 '12 at 08:18
  • 2
    I'd add a virtual dtor to avoid possible memory leaks in the future. – barankin Mar 18 '12 at 08:19
  • @CharlesBailey, missed that. Edited the post. Added as pure `virtual` just to maintain the pure `abstract`ness of the class and `inline` so that it can reside in the header file without linker errors. – iammilind Mar 18 '12 at 08:20
  • 2
    It would be simpler just to define the destructor in the class definition. You could just do `virtual ~interfaceA() {}`. Your class is already abstract so you don't gain anything by making the destructor a pure virtual function. – CB Bailey Mar 18 '12 at 08:39
  • 5
    @barankin: Memory leaks have nothing to do with virtual destructors. It's simply about writing correct C++. – Kerrek SB Mar 18 '12 at 09:58
17

There is no concept of interface in C++,
You can simulate the behavior using an Abstract class.
Abstract class is a class which has atleast one pure virtual function, One cannot create any instances of an abstract class but You could create pointers and references to it. Also each class inheriting from the abstract class must implement the pure virtual functions in order that it's instances can be created.

sanderr
  • 94
  • 5
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • And how is this not the concept of interfaces? Because abstract base classes allow in theory even more? Or because the keyword is not “interface”? – IceFire Sep 16 '21 at 16:54
  • @IceFire: Because C++ does not have a keyword `interface` – Alok Save Sep 16 '21 at 17:38
  • Ah. So if a language had something “like” interfaces but had a short form like “ifc”, it would also not have the concept of interfaces? And JS (before EC6) had no classes because you would need to write “function”? Does Java also have no generics because it has no “generic” keyword? – IceFire Sep 16 '21 at 19:37