C++ Lesson 1 - Methods and Output

Nov
29

is like many other OOP programming languages with it's support of classes, abstract classes, and many other "cool" types. Anyone who already knows similar languages like Java may find some of the earlier lessons too basic.

To start with it's best to learn how to print text to the screen. As most other tutorials will show you, the typical example is printing "Hello World".

This can be done using:

#include <iostream>

int main() {
    std::cout << "Hello World" << std::endl;
    return 0;
}

At this point I can imagine you are feeling a little confused as to what all this means. So here's a brief explanation:

Most commands that are used have to be loaded from other files called "libraries". These libraries contain the commands that are used in making your programs. To have access to them you must "include" them. This can be done by typing #include followed by the name of the library to include.

For basic input (from keyboard) and output (to screen) you need to have access to the contents of iostream.h (as shown in example 1 on line 1).

All programs in order to run must have what is known as a main method. A method is a routine that has the following format:

<return-type> <name> (<variable list>) {
}

Example 2:

int addInteger (int x, int y) {
    return x + y;
}

The return type is what the method returns as a result of completing it. If it returns nothing then the type is void. If the method is going to return the result of adding two numbers as shown in example 2, then the return type is int (short for integer). Other types will be discussed later in these tutorials. The method name is whatever you want to refer to the method as when you come to use it. Inside the parentheses are any variables that are passed into the method. This means anything that the method is going to use. In Example 2 it is two integers called x and y. Then inside braces (represented by a { and } ) is the actual code of the routine. If the method returns something then a return statement must be used along with whatever result it is returning.

It is traditional for the main method to return an integer hence the int main() part. The main method MUST be called main, and usually takes no arguments (i.e. nothing inside the parentheses).

On line 4, it uses std::cout as a way of printing text. Cout is a function inside iostream and so needs to be used as std::cout so that the compiler knows where to look for the function. std:: in front of a method means that the method resides in the namespace called std.

In printing or inputting it is necessary to indicate the direction of the flow. For outputting the symbol is << and for input it is >>

Next is the text that will be printed, followed by another method called std::endl. endl has the purpose of ending the line and going onto the next line. All statements end in a semi-colon unless it is a method declaration line (Example 1: Line 3), an include line (Example 1: Line 1) or after a closing brace (Example 1: Line 6).

The next line of Example 1 (line 5) returns 0 to say that the program is complete and has terminated normally.

#include <iostream>

int main() {
    int sum = addInteger(3, 5);
    std::cout <<  sum << std::endl;
    printNum();
    return 0;
}

int addInteger(int x, int y) {
    return x + y;
}

void printNum(int x) {
    std::cout << x << std::endl;
}