Possible Duplicate:
How do you declare an interface in C++?
Interface as in java in c++?
I am a Java programmer learning C++, and I was wondering if there is something like Java interfaces in C++, i.e. classes that another class can implement/extend more than one of. Thanks. p.s. New here so tell me if I did anything wrong.
C++ does not provide the exact equivalent of Java interface : in C++, overriding a virtual function can only be done in a derived class of the class with the virtual function declaration, whereas in Java, the overrider for a method in an interface can be declared in a base class.
No. An interface defines how a class should look like (as a bare minimum). Whether you implement this in a base class or in the lowest subclass doesn't matter.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class. An interface can contain any number of methods.
With @interface you're defining annotations, with interface you're defining interfaces. From Oracle docs: "Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program.
In C++ a class containing only pure virtual methods denotes an interface.
Example:
// Define the Serializable interface. class Serializable { // virtual destructor is required if the object may // be deleted through a pointer to Serializable virtual ~Serializable() {} virtual std::string serialize() const = 0; }; // Implements the Serializable interface class MyClass : public MyBaseClass, public virtual Serializable { virtual std::string serialize() const { // Implementation goes here. } };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With