Interfaces (not GUI!)

I'm just looking for a source of information on creating and using interfaces in C++. I've read a couple of books on C++ now and put it to good use, but I'm also studying Java at the same time. I've made quite good use of interfaces in Java but I've not seen anything about it with C++ and I would like to use the equivalent. If anyone can point me to some information on the net somewhere..
LoggytronicAsked:
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.

WxWCommented:
You want to combine Java with C++ ? What for ?
0
chensuCommented:
The equivalent in C++ is pure virtual functions.

class A
{
    public:
        virtual int foo(int) = 0;
};
0
LoggytronicAuthor Commented:
I thought that was the equivalent of abstract classes.. Does C++ not make the distinction?
And while we're on virtual functions, I have some virtual functions in a class but my compiler still complains if I take out the blank method bodies. I want to take out the method bodies and declare the whole class virtual/abstract but it doesn't let me..

Is there a good book on all this??!! :)
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

chensuCommented:
>I thought that was the equivalent of abstract classes.

Right. Actually, the equivalent in C++ is pure abstract classes, which contain only pure virtual functions.

>I want to take out the method bodies and declare the whole class virtual/abstract but it doesn't let me..

Do you have "=0" at the end of the function prototype?

>Is there a good book on all this?

I think any C++ tutorial books should mention it.
0
GlennDeanCommented:
  I believe chensu is right on why the compiler complains about ripping out the blank method - most likely you don't have the =0;.
   To make the class abstract, you need only one pure virtual function.  For example, the following class is abstract:
  class A
  {
   int func1() {cout << "ZZ Top";}
   virtual void func2() {cout << "Eagles";}
   virtual long func3()=0;
  };
 
   Note that if you rip out the code for func2 you will get a compiler error.  
   Glenn
0
jasonclarkeCommented:
> Note that if you rip out the code for func2
> you will get a compiler error.  
 
you probably won't.  Methods that are declared but not defined are fine until you actually try to use them, at which point you will get a linker error.

Hence the common idiom of declaring and not defining a copy constructor and assignment operator in classes that are not copyable/assignable.
0
GlennDeanCommented:
Jason:
   I'm using VC++6.0 and when I rip out the code for func2 the program doesn't compile.  Here's what I did (class A now has func2 code ripped out):
class B : public A
{
   virtual void func3() {}
};
int main()
{
   B myClass;
   return 0;
}
 
0
jasonclarkeCommented:
I'm not sure exactly what you wrote, but the following compiles with no problems:

#include <iostream>

using namespace std;

class A
{
   void func1() {cout << "ZZ Top";}
   virtual void func2();
   virtual void func3()=0;
};
class B : public A
{
   virtual void func3() {}
};
int main()
{
   B myClass;
   return 0;
}
0
GlennDeanCommented:
  How weird, I changed my code to exactly as yours and still got errors.  I guess it's just 2 different compilers "acting" 2 different ways.
   Glenn
0
LoggytronicAuthor Commented:
Thanks chensu for pointing out =0. Why is it 0? Do other values mean anything?

Also, back to the real question, interfaces. I think I've got the hang now of pure virtual functions, and let's say a whole class of them.

I was just about to type "do interfaces exist" but then I remembered I've seen mentions of them in header files. Could you create the effect of a Java Interface with a class of pure virtual functions, and then inherit it..

Multiple inheritance solves the problem really if that works. In Java you don't get multiple inheritance but you can inherit once and then use interfaces for the rest - seems to do the job usually. You could simulate this with multiple inheritance and a pure virtual class in C++.

Comments?

And who get's the points?!
0
GlennDeanCommented:
 With VC++ I've never seen anything but =0.  On the other hand, with Borland C++ it doesn't have to be 0.  I'm speaking from memory (long time ago) but, in Borland C++ if you wrote
   virtual void f() = 85;
for example, 85 was somehow related to some global object table????  I want to say the words "Realtime Object Table" but....    The project I worked on before (which used Borland C++), I actually never saw =0, it was always =85 or =1375 or ....
   Glenn
0
chensuCommented:
"=0" is the standard C++ syntax. Nothing else.

I would suggest you take a look at how COM interfaces are implemented in C++. They use pure abstract classes.
0
chensuCommented:
There is a macro "interface" defined as "struct" for declaring COM interfaces. struct is the same as class except that its members are public by default.
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
jasonclarkeCommented:
A lot of C++ coding standards restrict  the use of 'implementation' multiple inheritance because there are lots of complicated issues in its use.  'Interface' inheritance, on the other hand is usually regarded as safe, and is quite commonplace.

I think that in effect a class that contains only pure virtual methods is semantically equivalent to a Java 'interface'.

So, your statement:

> You could simulate this with multiple > inheritance and a pure virtual class > in C++.

is correct, and is quite a commonplace technique.

I'm not -entirely- sure about looking at COM, because COM abstracts out the interface a further level (into IDL), and then the implementation of COM interfaces may not use pure virtual methods (may not even be in C++).  But the aim is somewhat similar.

0
LoggytronicAuthor Commented:
Thanks everyone for contributing to this thread. I think I now know what I was after and I will be trying to implement some of this in the next few days.
Since nobody has put forward any of these comments as an answer I'll give the points to chensu, although thanks to jasonclarke for that last comment - v.useful.

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.