Saturday, 24 September 2011

C++ 101

C++ source programs are translated from text into an executable format the machine can understand by a tool known as a compiler. These come in all shapes and formats. For Windows, the most common compiler is Visual Studio- you can get C++ Express for free. For Linux, the most common compiler is G++. For Mac, the compiler is known as XCode. You will have to refer to your compiler's documentation on how to use it- they mostly follow platform conventions. Visual Studio has a pretty GUI, while G++ is usually invoked through the command line, for example.

The first source program we will convert is known as "Hello, World". It is traditionally the first program written by a novice in a language. In C++, this program looks like this:

#include <iostream>
int main() {
    std::cout << "Hello, World!\n";
}

This will output the text "Hello, World!" to your computer screen. Some compilers like Visual Studio will close the console as soon as the program is done running. In this case, you may need to use a breakpoint. Breakpoints are used to pause a program's execution. You can place a breakpoint on the last line of the program.

This small example program actually includes many both simple and advanced features of C++.











Whew, that's a lot- and it's a simplification, too. Fortunately, most of these topics we need to know next to nothing about to work with something a little easier and of more immediate interest. The only current line of interest is the indented one. The purpose of "std::cout << something;" is to print "something" to the screen. That "something" can be a string literal, as in this case, or it can be various other language-provided types, or it can even be custom user-defined types. For now, we will stick to using it with provided types. In addition, this effect can be chained to produce the same effect- that is, "std::cout << something << something_else;" will print both "something" and "something_else" to the screen.

Computer programs work with objects. These objects act as boxes into which values can be placed. The actual definition of these values can be simple, but it can also be very complex. Objects in C++ always have a known, fixed type that cannot change. Variables are simple, named objects, created and destroyed at predictable, known times. The simplest type available is int. int is a simple integral type that can represent integers of a certain range. C++ offers numerous integral types with varying ranges. In the following program, we will perform a simple computation using int.

#include <iostream>
int main() {
    int i = 5; // This is called a declaration.
    i = i + 4;
    std::cout << "Five plus four is " << i;
}

As you can see, each individual chunk of code in the "main" area must end with a semicolon. These chunks do not necessarily correspond to a line, but they almost always do. Furthermore, the language has no rules about the indentation of code, but again, there's a lot of convention about indenting. All professional programmers indent their code to a greater or lesser degree, and many languages or tools enforce certain styles.

First we declared a variable, called i, of type int. This means that we told the compiler of i's existence so that it could, for example, allocate the necessary memory. You cannot access a variable if it has not been declared. Conceptually, the declaration brings the variable into existence- it simply did not exist before that line. We gave it the initial value of 5. 5 is an integer within int's range and therefore a perfectly good int. The compiler will give you an error if you attempt to initialize an int with a value of a different type that it cannot convert- for example, a string. This kind of variable is technically known as an "automatic-lifetime" variable, but is usually referred to as a "stack" variable or a variable on the stack. There are other kinds of variable that we will meet later.

Next, we added 4 to it. This was achieved by giving it a new value- it's old value, plus four.

Finally, we output it to the screen. This example can be trivially modified to perform other, more complex operations. C++ can cope with arbitrarily complex mathematical expressions. In addition, you may create as many variables as you like.


#include <iostream>
int main() {
    int i = 5; // This is called a declaration.
    int j = 6;
    i = i * j; // Multiplication
    i = (i - j); // C++ also supports parenthesis, with the usual meaning
    i = i / j; // Division
    std::cout << "i is " << i << " and j is " << j;
}

An interesting caveat of integer division is that the result is an integer- any additional decimal places are simply removed. This means that the result of, for example, (3 / 5) is not 0.6, but simply 0. This will be addressed in the next lesson.

No comments:

Post a Comment