Interface and virtual functions in C ++ compared to Java interface

I have a question regarding C ++ virtual functions. The virtual used in a C ++ class declaration for a function in the base class to notify that the implementation of a subclass of this function may differ from subclass to subclass. Different subclasses may have different function implementations.

I do not understand. When you define an interface in C ++, it does not look like Java, as far as I can see.

Actually, I donโ€™t understand what the interface means in C ++. You specify the function as virtual in the header file. Then, subclasses or derived classes of the base class can override it in whatever way you want, since it is a virtual function.

Is an interface in C ++ a header file?

Greetings

+4
source share
5 answers

An interface in Java is a specific language construct or keyword. Java has an interface keyword that is used to indicate that the Java class does not provide the actual implementation, but instead describes the interface that the derived class should implement. This is checked by the Java compiler to ensure that the class that claims to implement the interface provides all the necessary methods for the interface.

In Java, the interface keyword indicates that the class provides a set of services described by the specified interface class. The Java class can implement several different interfaces.

The way the word interface is used in discussions with C ++ is somewhat similar, since it is related to the types of parameters of a class or function or method. However, in C ++, the interface keyword is missing. The word interface is used in a more general descriptive way with C ++, as in "The function interface takes two short and long." which indicates the function calling the arguments and their types, the interface between the calling function and the body of the function.

Therefore, think of the C ++ interface as some kind of contract between a class that implements the interface and any objects that use objects created from the class. A class can also actually provide several different related services, each of which has a specific interface.

In C ++, you can do something similar to the concept of a Java interface by using pure virtual methods in the class description to create an abstract class. This creates a C ++ class without implementation. See C ++: create an abstract class with an abstract method and override the method in a subclass .

What a virtual method does is provide a mechanism so that objects using derived classes of a class can depend on a particular interface, leaving implementation details for the derived class. So it looks like a Java interface. C ++ virtual methods are a way to provide a Java interface implementation using a compiled and static proven approach, rather than a run-time approach.

Using a virtual method, you create a type of superclass that can be used to create pointer variables to which variables from derived types can be assigned, and when you use the virtual method, the correct method from the derived class is called. Figuring out which invocation method is executed at compile time, rather than runtime. The basic idea for C ++ should be as effective as C, providing object-oriented language constructs along with static type checking at compile time to reduce the dependency on runtime error detection with additional overhead.

 class Joe { public: virtual int thingOne() { return 1;} // standard, not pure virtual method .. }; class JoeTwo : public Joe { public: int thingOne() { return 2;} // derived class provides its own version of the method .. }; Joe *myJoe = new JoeTwo; int i = myJoe->thingOne(); // value of 2 put into i and not value of 1 

With C ++, you need to recognize the difference between an object and a pointer to an object due to the splitting of objects that can occur when a variable containing a derived object is assigned to a variable containing the superclass of the class. This " object slice " will happen because the derived object does not fit into the base class or the superclass object from which it is derived. The derived object has additional material for which the superclass object does not have this purpose (the default assignment is a direct copy of the memory) only part The superclass of the derived object is copied to the superclass object.

 class Joe { public: virtual int thingOne() { return 1;} // standard, not pure virtual method .. }; class JoeTwo : public Joe { public: int thingOne() { return 2;} // derived class provides its own version of the method .. }; Joe *myJoe = new JoeTwo; // no object slicing since this is pointer JoeTwo myJoeTwo; Joe myJoeSliced = myJoeTwo; // JoeTwo object myJoeTwo sliced to fit into Joe object myJoeSliced 
+5
source

The main difference between Java and C ++ is that in Java, every function is virtual .

Historically, when C ++ was developed, the fact of placing an abstraction layer between each function call (that is, what happens using the virtual method table) was not what you really wanted for each method call, because the call to the virtual method is slower.

So what happens, you must indicate that the method should be called through dynamic binding , otherwise the method is selected at compile time according to the variable declaration. This means that if you declare Base *b = new Derived() and you call a method on b that is not virtual, then the method is selected at compile time and it will be Base::method .

Motto: you do not pay for what you do not use and what it is.

Interfaces in C ++ do not exist, but you can have a class with pure virtual functions that behave basically the same. In fact, you can have two kinds of virtual methods:

 class Base { virtual void method() { //implementation } virtual void pureMethod() = 0; } 

The first method() is virtual and obeys dynamic binding, but it is implemented even in the base class, and pureMethod() is still pureMethod() by dynamic binding, but it is declared as pure virtual, so it has no implementation, so Base cannot be created this way and you need to subclass it and override at least a pure virtual method.

+6
source

C ++ does not have interfaces like Java. The closest thing you get is when you define a class that has only pure virtual functions, so everyone who gets this class is forced to implement all of them.

+4
source

A virtual method in C ++ is a method that can be overridden. A pure virtual method (defined as virtual foo () = 0 ) is an abstract method. To create an interface similar to the ones you use in Java, you simply indicate that all methods are purely virtual (abstract).

However, sometimes the โ€œinterfaceโ€ refers to the public elements and types of the class, which is usually located inside the header.

+2
source

The C ++ class interface is defined in the header file. Virtual functions are only used if you want to use a subclass and use inheritance.

Closest to you, you are connecting to the java interface in C ++ when using a pure abstract class. Implementing classes that have this interface should subclass this abstract class.

Do not forget to make your deconstructor virtual or the subclass deconstructor is not called when referring to the base class.

example:

 class PuppetInterface { public: virtual ~PuppetInterface() {}; virtual void walk() = 0; virtual void tellALie() = 0; }; class Pinocchio : public PuppetInterface { public: ~Pinocchio() { //lie down look dead. } void walk() { //try moving wooden legs. } void tellALie() { //let nose grow look serious. } }; int main(int argc, const char * argv[]) { PuppetInterface* pinocchio = new Pinocchio(); pinocchio->walk(); pinocchio->tellALie(); delete pinocchio; return 0; } 

Edit : Since the original pointers are bad, not an exception, the exception is the probability of a leak, etc. The above code should be rewritten as

 //using c++ 11 int main(int argc, const char * argv[]) { auto pinocchio = std::unique_ptr<PuppetInterface>(new Pinocchio()); pinocchio->walk(); pinocchio->tellALie(); } 
+2
source

All Articles