6

I'm new to programming in c++ and struggle with organizing my project. I have got a class named StateManager, which has a header file and an cpp file. The cpp contains all implementations.

If I now want to make an Interface class:

class IStateManager
{
    public:
        virtual ~IStateManager() {}   
        virtual void SomeMethod {}
};

I know interfaces don't really exist as they do in c# or Java, but I want multiple classes to inherit from this "interface".

Does this class also need an header and a cpp file? Or can I just put it in a header file?

Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
Ruben-J
  • 2,663
  • 15
  • 33
  • 1
    Interface should only need header since it has no implementation. It could go in a header by itself, or in another larger header file. (no restrictions like Java here) –  Jan 14 '14 at 16:41
  • 1
    @ebyrob: These methods are not declared to be pure virtuals. – John Dibling Jan 14 '14 at 16:42
  • C++ compiler doesn't compile header files. Compilation units are always CPP files. If your header (because it's an _interface_) will be included somewhere by a cpp then you don't need an empty cpp for it. That said, if yuo're talking about interfaces...I would make that methods pure (instead of empty implementation). – Adriano Repetti Jan 14 '14 at 16:42
  • @Adriano, the empty implementation for the destructor is OK but I agree with you on the methods. – Mark Ransom Jan 14 '14 at 16:44
  • @MarkRansom I didn't even pay attention it's destructor! :| – Adriano Repetti Jan 14 '14 at 16:46
  • @JohnDibling Does that mean there **must** be a .cpp file? Note: If he wants advice on what his interface should look like, at least point him to a complete solution instead of alluding to it: http://stackoverflow.com/questions/1216750/how-can-i-simulate-interfaces-in-c –  Jan 14 '14 at 16:48
  • @ebyrob: No, it only means that there must be an implementation somewhere. – John Dibling Jan 14 '14 at 16:48
  • @JohnDibling I really don't see where you have a point here. –  Jan 14 '14 at 16:58
  • @ebyrob: Your first comment said that there is no implementation. However that's not true in this specific case. There must be an implementation because the methods aren't pure. That's it, that's my whole point. – John Dibling Jan 14 '14 at 17:00

2 Answers2

6

Technically, c++ doesn't have interfaces. However, one can "create" interfaces by way of multiple inheritance (or single inheritance if your class is a "base" class and doesn't need to inherit from multiple classes). Where your "interface" lives is entirely up to you. But if you plan on using a class as an interface (without any actual implementation because technically an interface doesn't have an implementation until the functions are defined in a subclass), I would put it in it's own header file and declare each function pure virtual:

class IStateManager
{
    public:
        virtual ~IStateManager() {}
        virtual void SomeMethod() = 0;
        virtual void AnotherMethod() = 0;
};

class TheState : public IStateManager, public SomeOtherParentClass
{
    virtual void SomeMethod(); // Defined in this class
    virtual void AnotherMethod(); // Also defined in this class
    //..
};

If you are defining some implementation in a .cpp for the IStateManager class, then you really have more of an abstract class and not an interface.

So in conclusion what I'm saying is: Any implementation of an "interface" should be defined in the .cpp file of its implementing class. And if you plan on using the interface in multiple files, I would create a separate .h file for it.

Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
1

You can put the implementation of class methods in the header file. That doesn't mean you should. It also has nothing to do with the fact that this is an "interface" class, as you call it.

I wouldn't call this an interface class by the way, because your virtual methods aren't pure.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • I know interfaces don't really exist as they do in c# or Java, but I want multiple classes to inherit from this "interface". So I want it isolated from other files or am I missing your point? – Ruben-J Jan 14 '14 at 16:43
  • 2
    I certainly wouldn't create a .cpp file just to store a couple sets of empty curly brackets `{}`. (Though "how should I create an interface in C++" is certainly a related question, which wasn't directly asked) –  Jan 14 '14 at 16:44