Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2006, 4:57 PM   #1
gizzy
Newbie
 
Join Date: Feb 2006
Location: Glasgow, Scotland
Posts: 8
Rep Power: 0 gizzy is on a distinguished road
Java-style interfaces in C++

I have been searching many places for information on how to create interface classes with C++. I have read about one way which involves pure virtual functions, but that does not solve my problem. I have been practicing Java for about 1.5 years, and would like to know how to create Java type interface classes with C++. I have been practicing C++ for a few months now. I coded a binary search tree in Java, and I now want to code it in C++. An example of what I want to be able to do:

public Interface Comparator...
...public bool eq(Comparator x);

public class MyCharacter implements Comparator
...public bool eq(Comparator x)
   {
       return (getValue() == x.getValue());
   }...

Basically, in Java I would be able to pass a MyCharacter object into a method as a Comparator object. Any ideas as to how I could go about this in C++?

Thanks.

Last edited by gizzy; Feb 15th, 2006 at 5:07 PM.
gizzy is offline   Reply With Quote
Old Feb 15th, 2006, 6:12 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by gizzy
I have been searching many places for information on how to create interface classes with C++. I have read about one way which involves pure virtual functions, but that does not solve my problem.
Why doesn't it?

Unlike Java, C++ has multiple inheritance, so interfaces are not strictly needed. The nearest thing to them is an abstract class, which is any C++ class with pure virtual member functions. A pure virtual function is defined thus:
class Foobar {
    public:
        virtual void pure_virtual_function() = 0;
};
An "interface" would be a class consisting entirely of virtual functions.
Arevos is offline   Reply With Quote
Old Feb 15th, 2006, 6:47 PM   #3
gizzy
Newbie
 
Join Date: Feb 2006
Location: Glasgow, Scotland
Posts: 8
Rep Power: 0 gizzy is on a distinguished road
Whenever I try to use 'Comparator' in a function, like this:

public bool eq(Comparator x)

I get an error saying that I can't create an instance of an abstract class. I'm just unsure as to how I would create a Comparator interface class, so that more than 1 type of class could implement it, and pass instances of those classes into the functions, such as eq

Thanks.
gizzy is offline   Reply With Quote
Old Feb 15th, 2006, 7:48 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Don't instantiate it; derive from it and provide the methods. A virtual function can't DO anything.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 15th, 2006, 8:36 PM   #5
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
Originally Posted by DaWei
A virtual function can't DO anything.
Just for the sake of clarification - a pure virtual function, that is. A regular virtual method (using just the virtual keyword) can have an implementation and do stuff.
Cerulean is offline   Reply With Quote
Old Feb 15th, 2006, 9:15 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Correct the missing word in my clarification by extrapolating from Arevos' post .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 16th, 2006, 8:26 AM   #7
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
Originally Posted by Dawei
Don't instantiate it; derive from it and provide the methods. A virtual function can't DO anything.
Here's an example:
class animal
{
  public:
     virtual void speak()=0;
};

class dog:public animal
{
   public:
      void speak()
        {
          cout<<"bow bow...";
          }
};

class cat:public animal
{
   public:
        void speak()
          {
            cout<<"meow meow...";
            }
 };

int main()
{
   animal *p;
   p = new dog;
   p->speak();//prints bow bow...
   delete p;
   p = new cat;
   p->speak();//prints meow meow...
   delete p;
   return 0;
  }

Hope that helps.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Feb 16th, 2006, 9:39 AM   #8
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
1) An abstract base class in C++ is a class that declares one or more pure virtual member functions. Optionally, in C++, any virtual member function can be defined whether it is pure or not (i.e. contrary to Dawei's and Cerulean's comments, an abstract base class CAN actually provide a body for any virtual function, whether it is pure or not). The existence of any virtual member functions, however, means that the class cannot be instantiated (just as it is not possible to create an instance of a Java interface).

2) A Java interface is approximately functionally equivalent to an abstract base class in C++, with three additional properties;

a) ALL member functions (other than constructors, which are optional) are pure virtual
b) The class has no data members;
c) If the abstract base clase is derived from another, the base class also has these properties.

3) There is no "implements" keyword in C++. The C++ equivalent of implementing a Java interface is deriving from the abstract base class, declaring all pure virtual function(s), and implementing them. C++ supports multiple inheritence of classes, so the Java notion of implementing multiple interfaces can be achieved by inheriting from multiple abstract base classes. In doing this, if ANY inherited pure virtual functions are not overridded (i.e. implemented by the derived class) the derived class is also an abstract base class and cannot be instantiated. Unlike Java, C++ also supports multiple inheritence of class implementation, not just interface.

4) One other property of an abstract base class is that it cannot be passed by value as an argument to a function. It must be passed by reference. Using InfoGeek's example, we can't do this;
void Talk(animal a)
{
     a.speak();
}
but we can do this;
void Talk(animal &a)
{
     a.speak();
}
The second version of the function can be used like this;
int main()
{
        cat c;
        Talk(c);   // prints meow meow 
        dog d;
        Talk(d);   // prints bow wow
}
The reason passing an abstract base class by value is not allowed is that passing by value implicitly creates a copy of the object. Creating a copy implies creating an instance of the abstract base class. And that is not allowed......
grumpy is offline   Reply With Quote
Old Feb 16th, 2006, 6:23 PM   #9
gizzy
Newbie
 
Join Date: Feb 2006
Location: Glasgow, Scotland
Posts: 8
Rep Power: 0 gizzy is on a distinguished road
Thanks for the help. I have progressed a bit, but am faced with another problem. My Comparator class is like this...

class Comparator
{
public:
	virtual bool eq(Comparator &x) = 0;
	virtual bool ge(Comparator &x) = 0;
	virtual bool gt(Comparator &x) = 0;
};

I have a class CharCon (a character container class) which inherits from the Comparator class, like so...

class CharCon : public Comparator
{
public:
	CharCon();
	CharCon(char initChar);
	~CharCon();

	void setValue(char newChar);
	char getValue();
	void printValue();

	bool eq(Comparator &x);
	bool ge(Comparator &x);
	bool gt(Comparator &x);

private:
	char character;
};

In my implementation of the eq, ge, and gt functions in the CharCon.cpp file, I need to be able to cast the Comparator argument as a CharCon, but the compiler tells me I cannot convert from Comparator to CharCon. This is the eq method in CharCon.cpp...

bool CharCon::eq(Comparator &x)
{
	return (character == ((CharCon)x).getValue());
}

I try to cast x as a CharCon, but it can't be done, and I don't know how I could go about it in another way. Help appreciated. Thanks again.
gizzy is offline   Reply With Quote
Old Feb 16th, 2006, 6:38 PM   #10
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
But you can convert a pointer to x to a CharCon* like so:
bool CharCon::eq(Comparator &x)
{
	return (character == ((CharCon*) &x)->getValue());
}

or:

bool CharCon::eq(Comparator *x)
{
	return (character == ((CharCon*) x)->getValue());
}
Kaja Fumei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC