![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Location: Glasgow, Scotland
Posts: 8
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
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;
}; |
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2006
Location: Glasgow, Scotland
Posts: 8
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
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();
}void Talk(animal &a)
{
a.speak();
}int main()
{
cat c;
Talk(c); // prints meow meow
dog d;
Talk(d); // prints bow wow
} |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Feb 2006
Location: Glasgow, Scotland
Posts: 8
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
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());
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|