uml uml - 3 months ago 20
C++ Question

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

I have a question regarding C++ virtual functions. The

virtual
keyword is used in C++
class
declaration on a function in a base class in order to notify that a sub-class implementation of that function may differ from sub-class to sub-class. Different sub-classes may have different implementations of the function.

I don't get it a bit. When you define interface in C++ it is not like in Java as far as I can see by now.

In fact I don't understand what the word interface in C++ means. You specify a function as
virtual
in the header file. Then sub-classes or derived classes of base class can override it in any way you want as long it is a virtual function.

Is the interface in C++ a header file?

Cheers

Answer

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

In Java the interface key word indicates that a class provides a set of services described by the interface class specified. A Java class may implement multiple different interfaces.

The way that the word interface is used in discussions with C++ is a somewhat similar concept since it has to do with the parameter types of a class or function or method. However there is no interface keyword in C++. The word interface is used in a more generic, descriptive way with C++ as in "The function interface takes two shorts and a long." which is indicating the function calling arguments and their types, the interface between the caller of the function and the function body.

So think of interface in C++ as a kind of contract between the class implementing the interface and any objects that are using objects instantiated from the class. A class can also actually provide several different related services each of which has a particular interface.

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

What a virtual method does is to provide a mechanism so that objects using derived classes of the class can depend on a particular interface while leaving the details of the implementation to the derived class. So this is similar to the Java interface. C++ virtual methods is a way to provide a way to implement a Java interface using a compiled and static checked approach rather than a run time approach.

With a virtual method you create a super class type that can be used to create pointer variables that can be assigned variables from the derived types and when you use the virtual method, the correct method from the derived class will be called. Figuring out which method to call is done at compile time not run time. The major idea for C++ is to be as efficient as C while providing object oriented language constructs along with static type checking at compile time to reduce reliance on error detect at run time with its 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 do need to recognize the difference between an object and a pointer to an object because of object slicing which can happen when a variable containing a derived object is assigned to a variable containing the derived object's super class. This "object slicing" will happen because the derived object does not fit into the base class or super class object from which it is derived. The derived object has additional stuff the super class object does not have so with the assignment (default assignment is straight memory copy) only the super class part of the derived object is copied into the super class 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
Comments