Interfaces in C++?

Hi,

In Java there is an element called interface,
an interface is used to handle things as multiple inheritance.

Are there interfaces in C++?
hgonzalezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yonatCommented:
A class with only pure abstract functions is similar to a Java interface. For example:

class Foo
{
public:
    void Bar() = 0;
    int Baz(char c) = 0;
    double Oof() = 0;
};

0
kyoto001Commented:
use abstract functions.
0
hgonzalezAuthor Commented:
need more detail.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

TriskelionCommented:
See the example class delaration below
This shows that CData inherits from CTest
The full 16 bit project is at
http://home.sprintmail.com/~hines/clstest.zip
//////////////////////////////////////////////
class      CData : public CTest  //inheritance
{
public:
      CData(void);
      ~CData();
      char      *      GetSecondVariable(void);
      void            SetSecondVariable(const char * spNewString);
private:
      char       m_sSecondVariable[128];
};
0
hgonzalezAuthor Commented:
Are you saying that CTest acts like an interface?
0
TriskelionCommented:
The entire class definition is the interface.
The top line tells which classes CData will inherit from -- public or private.

To get a full flavor for how C++ will respond with member functions and variables, you will need to read more than I should type here.
0
hgonzalezAuthor Commented:
I think that you don't understand my question.
The example you give is just a normal class with member methods and attributes.
Those members define the interface of the class you are right.  What I am talking about is a Java type interface.

The comment from yonat is right. Defining a class with purely abstract methods
is similar to a java interface.
0
TriskelionCommented:
Can you explain more of the behavior for which you are looking?
0
yonatCommented:
Triskelion, take a look at any Java intro to learn what a Java interface is.
0
chensuCommented:
Just to elaborate yonat's comment,

An abstract class

class Foo
{
public:
    virtual void Bar() = 0;   // pure virtual function
    virtual int Baz(char c) = 0;   // pure virtual function
    virtual double Oof() = 0;  // pure virtual function
};

It can be used only as a base class of some other class. No objects of an abstract class can be created except as sub-objects of a class derived from it.
0
chensuCommented:
A COM interface is implemented as an abstract class in C++.
0
hgonzalezAuthor Commented:
The designers of Java decided to avoid a lot of complexity coming from
implementing multiple inheritance and decided to use instead interfaces.
Interfaces (in Java) are just a collection on abstract methods (No implementation).
Classes can inherit from one other class and multiple interfaces.  The child class then has to implement the methods defined in the interfaces it inherits from.

I am asking because it is been a long time since I have worked with C++ and
I was wandering if the language had changed into the direction of Java (Not
saying that I prefer that direction).
0
hgonzalezAuthor Commented:
Doesn't address the issue.
0
TriskelionCommented:
Chensu is right, then.
0
yonatCommented:
C++ has not changed in this regard for a long time. I don't know how long it has been since you used C++, but the most recent change relevant here was the addition of multiple inheritance (late eighties). In C++, you can inherit from any number of classes, regardless of their abstractness. When this causes ambiguities, the compiler says so.

Using classes with only pure abstract functions is in effect equivalent to using interfaces in Java.
0
mbormannCommented:
C++ has been enhanced with STL from I think 1995/96  onwards.
0
shrifCommented:
hgonzalez, you're on the right track.  If you want to mimic Java interfaces in C++, simply create an abstract class where every method is pure virtual.  In addition, do not implement any of the methods of the abstract class.

If you're using Visual C++ and are including windows.h, then you can even use the "interface" keyword, as in:
interface DataReceiver
{
    virtual void  getData() = 0;
    virtual void  setData() = 0;
    virtual void  getStatus() = 0;
};

The "interface" keyword is simply a #define for "struct".  It's somewhere in the COM headers.

In any case -- that's all there is to doing it in C++.  You just have to be sure to make every method pure virtual and not implement any of the methods.

(For those that don't understand the last rule -- it's because in C++, you can implement a pure-virtual method in the abstract class itself.  However, because it's abstract, you still can't instantiate it -- although, technically, all the implementation could be there.  Example.  The following is legal:

struct D
{
      virtual void get() = 0;
};

void D::get()
{
      std::cout << "d::get" << std::endl;
}


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yonatCommented:
STL was added to C++ at 1994, IIRC. But I fail to see how STL is realted to this question.
0
hgonzalezAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.