C++ class Interface implementation

Hey guys ,

I’m a new in c++ and i was trying to create a interface to implement in another class but unfortunately i had no good results .

Somebody knows some good resource or have some tricks to help me ?

Cheers

The way that C++ does this is by using the “virtual” keyword in front of the method name. So:

  
  
  
class IInterface {  
    virtual void methodOne() = 0; // virtual!  
    virtual void methodTwo() = 0; // virtual  
    virtual ~IInterface() { }  
};  
  
  

and then:

  
  
  
class Something : public IInterface {  
    void methodOne(); // no virtual  
    void methodTwo(); // no virtual  
};  
  
  

There are some pretty good posts on StackOverflow about this topic and also at http://www.parashift.com/c+±faq-lite/virtual-functions.html

But when you implement the interface you have the commitment to implement the methods like in the other languages ?

Can you help me to figure out why my implementation is not working ?

Thanks

IDragable.h

  
  
  
class IDragable  
{  
    virtual float getCoeficient();  
};  
  
  

ofLiquid.h

  
  
  
#include "ofMain.h"  
#include "IDragable.h"  
class ofLiquid : public IDragable  {  
      
    float getCoeficient();  
  
    public:  
      
        float x,y,w,h;  
        float c;  
      
        ofLiquid();  
        ofLiquid(float _x ,float _y , float _w , float _h);  
        void display();  
        void update();  
      
};  
  
  

ofLiquid.cpp

  
  
  
#include "ofLiquid.h"  
  
  
ofLiquid::ofLiquid()  
{  
   
}  
  
ofLiquid::ofLiquid(float _x ,float _y , float _w , float _h)  
{  
    x = _x;  
    y = _y;  
    w = _w;  
    h = _h;  
}  
  
void ofLiquid::display()  
{  
    ofEnableAlphaBlending();  
      
    ofSetColor(175, 175, 175,100);  
    ofRect(x, y, w, h);  
      
    ofDisableAlphaBlending();  
}  
  
void ofLiquid::update()  
{  
      
}  
  
float ofLiquid::getCoeficient()  
{  
    return 0;  
}  
  
  

Change your declaration getCoeficient to:

  
virtual float getCoeficient() = 0;  

You need to give the compiler something to replace with the actual defined method when it replaces the inherited classes functionality in the inheriting class (hopefully that makes sense).

Thanks Joshua,

Thats really solve the problem … I still have not sure if i got the reason why wee need to do that but i will keep studying oop in c++

Thanks for your Help

if the virtual function in the base class has “= 0” after it, it’s called “pure virtual”. See e.g. http://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-interface-classes/

it’s also important to have a virtual destructor in this kind of classes:

  
  
    class IDragable    
    {    
        virtual ~IDragable(){}  
        virtual float getCoeficient()=0;    
    };    
  

or you can end up with memory leaks

Guys ,

And about the commitment to implement the methods ? In flash or unity(c#) since i say than i’m using an interface i need to implement the methods … Have one way to do that in c++ ?

Cheers

i’m not sure i understand what you’re asking about correctly. If your interface class function declaration is pure virtual (i.e. virtual float getCoeficient() = 0;), your derived class _has_ to implement the function, otherwise the compiler complains.

Yes exactly !

I always used flash like a reference because is that one than i know better but anyway there if i extend a class i got the functions and properties from that class and i can or not override some methods .
But if i’m using an Interface i can ensuring than the class will have just that methods declared on the interface . Otherwise the compiler will complain .

if i extend a class i got the functions and properties from that class and i can or not override some methods .

this you can do when you declare them virtual in the base class, without the = 0 part.

In general, I can recommend http://www.learncpp.com/ as an awesome resource!