Simple Class | Java to C++ |
Description of Example
Classes in C++ are a little more complicated than in Java. In this example we are avoiding many of the differences and showing a simple translation from between a Java program with classes and an equivalent C++ program. pointersIn Java every object variable is actually a pointer to an object, Main.java[6,7]. In C++ the pointers must be explicitly declared, Main.cpp[7,8]. It is possible to create a C++ object without using a pointer, but for Java programmers it is recommended to always use a pointer to an object. The result will behave in C++ the same as in Java. This is also true for parameter passing to methods. In APoint.java[11] we just use the class name. In APoint.cpp[9] we use the class name with an * to indicate a pointer. Because they are pointers, not also that arrow notation is used rather than dot notation for the other point in APoint.cpp[11,12]. header filesA very important difference between Java and C++ is the use of header files for a declaration. In APoint.java the system figures out that the fields x and y along with the constructor APoint and the method distanceTo are public and available for other programs to use. In APoint.h this information is separately declared in a .h file. This declaration of the class APoint has all the information that other programs will need to use the class. Note that Main.cpp[3] includes this .h (header) file so that main() will know what to do with the APoint class. In Java this all happened automatically. Note that the class APoint is only one file in Java whereas in C++ it is two. This means that the declaration of a class also has two parts. In APoint.h[5,8] the constructors and the methods simply end in a semicolon. There is no code for these in the class declaration. The constructor APoint() appears again APoint.cpp[5-8]. This time it appears with all of its code. The constructor uses the syntax APoint::APoint. The name in front of the :: is the class name and afterwards is the method name. The method distance() appears at APoint.cpp[9-15]. Again the method name is preceded by APoint:: to indicate what class the method is for. Because a particular class may be used in other classes a header file may get included more than once. To keep this from happening, C++ provides the #pragma once directive shown at APoint.h[1]. This tells the compiler to include this file only one time no matter how many other include files may also include this one. It is good practice use this with all .h header files. Class semicolonUnlike Java, a class declaration in C++ always ends with a semicolon as shown at APoint.h[10]. #includeBecause the class APoint a header file APoint.h, that same declaration code can be used in both Main.cpp[3] and APoint.cpp[1]. The include mechanism brings the same declaration into both files. virtual methodsIn Java there is only one way to implement a method call. In C++ there are two possible ways. One is very efficient but handles method inheritance in unexpected ways. When moving from Java to C++ it is important to declare all of your methods as virtual as in APoint.h[8]. This will ensure that inheritance works the same way that it does in Java. destructorsEvery class must have a destructor. A destructor is a method that is called whenever the object is no longer needed. In Java this did not matter because Java would collect all unused memory automatically. There is no such garbage collection in C++. You need to clean everything up yourself. A destructor always has the form ~class(void) where class is the name of the class. We see this declaration in APoint.h[9]. A destructor is a special kind of method. Therefore the code for the destructor should appear in the .cpp file. For example APoint.cpp[17-19] contains the body of the destructor. This particular class is a simple one and there is nothing to be cleaned up. |