How to Simulate Interfaces in C++

How can I simulate interfaces in C++?

Since C++ has multiple inheritance unlike C# and Java, yes you can make a series of abstract classes.

As for convention, it is up to you; however, I like to precede the class names with an I.

class IStringNotifier
{
public:
virtual void sendMessage(std::string &strMessage) = 0;
virtual ~IStringNotifier() { }
};

The performance is nothing to worry about in terms of comparison between C# and Java. Basically you will just have the overhead of having a lookup table for your functions or a vtable just like any sort of inheritance with virtual methods would have given.

how to implement Interfaces in C++?

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

How can I simulate OO-style polymorphism in C?

The first C++ compiler ("C with classes") would actually generate C code, so that's definitely doable.

Basically, your base class is a struct; derived structs must include the base struct at the first position, so that a pointer to the "derived" struct will also be a valid pointer to the base struct.

typedef struct {
data member_x;
} base;

typedef struct {
struct base;
data member_y;
} derived;

void function_on_base(struct base * a); // here I can pass both pointers to derived and to base

void function_on_derived(struct derived * b); // here I must pass a pointer to the derived class

The functions can be part of the structure as function pointers, so that a syntax like p->call(p) becomes possible, but you still have to explicitly pass a pointer to the struct to the function itself.

Making a GUI simulator in C

I saw the video that you want to make. After watching the video I feel you don't need a GUI library for this simulator program.

Here is a list of libraries that you can use.

1.OpenGL This is a 3D graphics API which also can be used for 2D and can be used with both c/c++

2.SDL This library is easy to understand for a beginner. For your program this library is better and can be used with both c/c++.

3.winBGIm This is same as the graphics.h that you found and can be used both c/c++ but it is only for windows.

If you are looking for GUI library then here's a short list.

1.GTK This is written in c and is a popular GUI library for c. You can find a GUI editor for gtk forms called glade which enables quick & easy development of user interfaces.

2.WxWidgets This is written in c++ so you have to use c++ rather than c.

3.FLTK

There are many more libraries besides these which you can find in google. You said

I do not mind having to code in another language like Java or Python to accomplish my goal.

Then for java you can use swing and If you are windows developer then use the windows form application in visual c++; then development of your program will be very easy.

How to simulate multiple inheritance in C#

C# does not have multiple inheritance, so the line

Class C : A , B {}

will never work. You can do similar things with interfaces though, along the lines of

interface InterfaceA { void doA(); } 
class A : InterfaceA { public void doA() {} }

interface InterfaceB { void doB(); }
class B : InterfaceB { public void doB() {}}

class C : InterfaceA, InterfaceB
{
m_A = new A();
m_B = new B();

public void doA() { m_A.doA(); }
public void doB() { m_B.doB(); }
}

Best way to declare an interface in C++11

What about:

class Testable
{
public:
virtual ~Testable() { }
virtual void test() = 0;
}

In C++ this makes no implications about copyability of child classes. All this says is that the child must implement test (which is exactly what you want for an interface). You can't instantiate this class so you don't have to worry about any implicit constructors as they can't ever be called directly as the parent interface type.

If you wish to enforce that child classes implement a destructor you can make that pure as well (but you still have to implement it in the interface).

Also note that if you don't need polymorphic destruction you can choose to make your destructor protected non-virtual instead.

Where to put an interface class in c++

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.



Related Topics



Leave a reply



Submit