[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

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..
0
Loggytronic
Asked:
Loggytronic
  • 4
  • 4
  • 3
  • +2
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now