Link to home
Start Free TrialLog in
Avatar of hgonzalez
hgonzalez

asked on

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++?
Avatar of yonat
yonat

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

use abstract functions.
Avatar of hgonzalez

ASKER

need more detail.
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];
};
Are you saying that CTest acts like an interface?
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.
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.
Can you explain more of the behavior for which you are looking?
Triskelion, take a look at any Java intro to learn what a Java interface is.
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.
A COM interface is implemented as an abstract class in C++.
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).
Doesn't address the issue.
Chensu is right, then.
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.
C++ has been enhanced with STL from I think 1995/96  onwards.
ASKER CERTIFIED SOLUTION
Avatar of shrif
shrif

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
STL was added to C++ at 1994, IIRC. But I fail to see how STL is realted to this question.
Thanks