Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Interfaces in C++?

Posted on 1999-06-27
19
Medium Priority
?
305 Views
Last Modified: 2010-04-16
Hi,

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

Are there interfaces in C++?
0
Comment
Question by:hgonzalez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
  • +4
19 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1198575
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
 

Expert Comment

by:kyoto001
ID: 1198576
use abstract functions.
0
 

Author Comment

by:hgonzalez
ID: 1198577
need more detail.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Triskelion
ID: 1198578
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
 

Author Comment

by:hgonzalez
ID: 1198579
Are you saying that CTest acts like an interface?
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1198580
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
 

Author Comment

by:hgonzalez
ID: 1198581
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
 
LVL 6

Expert Comment

by:Triskelion
ID: 1198582
Can you explain more of the behavior for which you are looking?
0
 
LVL 5

Expert Comment

by:yonat
ID: 1198583
Triskelion, take a look at any Java intro to learn what a Java interface is.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1198584
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
 
LVL 23

Expert Comment

by:chensu
ID: 1198585
A COM interface is implemented as an abstract class in C++.
0
 

Author Comment

by:hgonzalez
ID: 1198586
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
 

Author Comment

by:hgonzalez
ID: 1198587
Doesn't address the issue.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1198588
Chensu is right, then.
0
 
LVL 5

Expert Comment

by:yonat
ID: 1198589
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
 
LVL 5

Expert Comment

by:mbormann
ID: 1198590
C++ has been enhanced with STL from I think 1995/96  onwards.
0
 
LVL 1

Accepted Solution

by:
shrif earned 200 total points
ID: 1198591
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
 
LVL 5

Expert Comment

by:yonat
ID: 1198592
STL was added to C++ at 1994, IIRC. But I fail to see how STL is realted to this question.
0
 

Author Comment

by:hgonzalez
ID: 1198593
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question