CSE250 Java2C++ Checklist Fall 2010 -----------------------File headers and structure------------------------- 0. We will still use Javadoc conventions in C++: please put a /** comment at the top of every code file, with the name of the file, your name, and a short description. 1. Pretty much every C++ file we write will need these include-headers: #include //replaces Java "import java.io.*;" #include //unnecessary on many systems? but needed in Visual C++ #include //in place of Java arrays and ArrayList 2. Other important headers: #include //for file i/o. #include //string-streams, help parse strings into int/float/etc. 3. All items in the above libraries need a prefix "std::" by default. To skip this, you may use the line (note unlike #includes it has a semicolon): using namespace std; //which is like an import with a final "*" in Java. OR, you can just do "using" lines for the items the code actually uses: using std::cout; //like "out" in Java as short for "System.out" using std::cin; //ditto for "System.in" using std::endl; //Prefer "<< endl" to printing "\n". Mnemonic: Ivan Lendl using std::getline; //again not needed on "timberlake"; on your systems?... using std::string; //not "built-in" like Java "String", == vector using std::vector; //write vector for a vector of Foo objects 4. A C/C++ program need not have classes---and the executable routine "main" NEVER resides inside a class. The two standard forms of main are: int main() { ... } int main(int argc, char** argv) { ... } The latter translates the mandatory "(String[] args)" parameters for main in Java, but requires the extra first variable "argc"---which automatically gets the count of how many command-line args there are---because raw C arrays do not "know" their own length like Java arrays (and C++ vectors) do. Raw arrays are formally pointers, so char* can mean an array of characters, i.e. a string, so char** can mean an array of strings. Yuck! We provide boiler-plate code for transferring the arguments to C++ strings and vectors, which are safer. Finally, "main" returns int not void because the last line of main should be "return 0;" to indicate a successful completion, which is analogous to the old "System.exit(0)" for a Java application. Besides "main", C++ programs can have all kinds of functions and variables and etc. at top-level, not just classes. But we will prefer to use classes as much as possible, thus following good advice which Java chose to mandate. 5. You can use the same conventions for user-defined class and method and variable names as in Java, leaving lowercase type names like "string" and names_with_underscores for C++ library items. ------------------------Translating Code Elements-------------------------- 6. Primitive types int, double, float, char translate to themselves. (But C++ has many more character types, wchar, wchar_t etc.) 7. Java boolean becomes bool, while "true" and "false" are the same. 8. Java final before a variable becomes C++ "const". (But before methods C++ "const" has another meaning---not that the method can't be overridden.) 9. A Java object type Foo is translated by the C++ pointer type Foo*. (Just writing the type as "Foo" is legal in C++, but it gives *value* behavior.) 10. Object construction: Java "Foo x = new Foo(...);" becomes C++ "Foo* xp = new Foo(...);" 11. Object declaration Foo x; becomes C++ pointer declaration Foo* xp; **Curveball: In C++ "Foo x;" is not just a declaration---it is also a /definition/ using the zero-parameter constructor, with *value* behavior. A value construction with 0 parameters in C++ really should be "Foo x();", for consistency with the correct C++ syntax "Foo x(2,3);" when there are (say) two int constructor parameters. The reason C++ has this inconsistency is that "Foo x();" gets interpreted as a forward-declaration of a zero-parameter *function* that resturns an object of type Foo. We will of course do value construction later, but first we're learning the pointer translation, since it gives Java-like behavior. The "p" in "xp" is just a reminder of that---once you get it, you should drop the trailing "p" from the pointer names (which is generally called "Hungarian Notation"). 12. Member access x.bar becomes C++ xp->bar (mechanically changing every dot to an arrow is admittedly tedious...). 13. Java int[] arr; should be translated by C++ vector* arrp; (Again, later we will treat vectors as values rather than do pointer-to-vector.) 14. Java int arr[6]; becomes vector* arrp = new vector(6); 15. Java arr[i] becomes C++ arrp->at(i), both on LHS and RHS of assignments. **Curveball: C++ (*arrp)[i] is legal, and just arrp[i] ./may/ be legal. However, as with simply "arr[i]" when vectors are treated by-value, this does *not* check the bounds of the array as in Java. The text in chapter 4 jumps thru hoops to make a "KWvector" class whose "operator[]" *does* do such checking---but for now let's stick with pointers-to-arrays and "->at(i)". 16. Java "String st" technically becomes C++ "const string* stp", but it is AOK already just to treat strings by-value, as "string st". -------------------------Class Syntax--------------------------------------- 17. C++ classes have a closing "};" not just "}" as in Java (!!!) 18. C++ uses :: not . when the left-hand side is the name of a class (or namespace, which is sort-of like a Java package). 19. In C++, "public:" and "private:" denote /ranges/, not individual members. 20. C++ does not have Java "default scope" (also called "package scope"). 21. C++ class fields must be initialized by a constructor. 22. To get Java behavior, C++ methods must have "virtual" in front. 23. /Accessor/ methods should (must) also have "const" after their parameters. 24. Class extension uses ": public" in place of Java "extends". Mnemonic: "Colin Powell" (I said that in class but forgot on this list.) 25. Special constructor-init syntax...here's where we segue to lecture notes...