After some fiddling with XCode and C++, I finally got it compiling my Hello World. To my pleasure, the syntax in C++ is somewhat similar with Java, but there are still many differences.

In Java, to start a class we use:

public class fileName
{

}

In a command line C++ program, the following lines are needed:

#include <iostream>
using namespace std;

iostream is needed for input and output, std is needed for basic functions like cout. Some online tutorials use #include <iostream.h> but apparently it’s deprecated.

The standard structure of a main method is:

int main () {

//Neat stuff goes here
return 0;

}

Printing

To print something, use  “cout” (console output?) with the insertion operator “<<” like this:

cout << “Hellos!\n”;

int a = 1;
int b = 2;
int c = a + b;
cout << “Some variable: ” << c << “\n” ; //Prints c
cout << “Some variable: ” << a+b << endl ; //Prints the same as above

The “\n” is needed to start a new line. This replaces System.out.println and System.out.print in Java. Alternatively, you can also use “endl” (end line).

The syntax for variable declarations are identical to Java with one small exception:

string a = “Feed me”; //OK
string a (“Feed me”); //OK, Identical to the line above.

Arrays

Java:

int[] myArray1 = new int[5];
int[][] myArray2 = new int[5][6];

C++:

int myArray1 [5];
int myArray2 [5][6];

Variables

Now this is pretty cool. When assigning a value to a variable, we can use a literal constant, an octal (base 8) value, or a hex (base 16) value.

int a = 123; //Literal constant
a = 0173; //Octal (Start the variable with a 0)
a = 0x7b; //Hex (Start the variable with 0x)

Floating point numbers:

double a = 3.141592654
a = 6.02e23 //OK

Constants:

final int myVar = 100; //Java

const int myVar = 100; //C++

User Input

Use cin. Here is an example.

#include <iostream>
using namespace std;

int main () {
int i;
cout << “Enter a value…”;
cin >> i;
cout << “The value you entered is ” << i;
return 0;
}

Notice the use of the extraction operator “>>”. The line “cin >> i” takes the user input and stores it in the variable “i”. If the user enters a string instead of an integer, the program will not behave as you would expect. In Java, it will simply blow up. Also, the program will take whatever the user types up to a space, a tab, or a new line. Everything after these will be ignored unless you specifically store them in another variable. So, if a user enters a string which could possibly be composed of more than one word, it would be better to use the getline function.

The getline function works like this:

#include <iostream>
using namespace std;

int main () {
string i;
cout << “Enter a sentence…\n”;
getline (cin, i);
cout << “You said: ” << i;
return 0;
}

And its output may look something like this:

Enter a sentence…
C++ Rocks!
You said: C++ Rocks!

Control Structures

If, while, do while, and for statements are identical to Java. :)

Functions

Because C++ has a 1-pass compiler, any additional functions (a.k.a. “methods” in Java) must be declared before the main function.

For example, this will work:

#include <iostream>
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << “The result is “ << z;
return 0;
}

And this will not:

#include <iostream>
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << “The result is “ << z;
return 0;
}

Classes

A piece of sample code:

#include <iostream>
using namespace std;

class Point {
int x, y;
public:
Point (int, int);
void setPoint(int, int);
void printPoint() {
cout << “( ” << x << ” , ” << y << ” )” << endl;
}
};

Point::Point (int a, int b) {
x = a;
y = b;
}

void Point::setPoint(int a, int b) {
x = a;
y = b;
}

int main() {

Point foo(1,2);
foo.printPoint();
return 0;
}

This piece of code has an object with a constructor, two functions, and the main method creates one instance of the object.