# Introduction to C Plus Plus

## Hello World!

The first program that most aspiring programmers write is the classic "Hello World" program. The purpose of this program is to display the text "Hello World!" to the user. The "Hello World" example is somewhat famous as it is often the first program presented when introducing a programming language[1].

#include <iostream>

using namespace std;

int main()
{
cout << "Hello World!" << endl;
cin.get();

return 0;
}

## Understanding the Code

Before discussing the particulars, it is useful to think of a computer program simultaneously in terms of both its structure and its meaning.

A C++ program is structured in a specific, particular manner. C++ is a language and therefore has a grammar similar to a spoken language like English. The grammar of computer languages is usually much, much simpler than spoken languages but comes with the disadvantage of having stricter rules. Applying this structure or grammar to the language is what allows the computer to understand the program and what it is supposed to do.

The overall program has a structure, but it is also important to understand the purpose of part of that structure. By analogy, a textbook can be split into sections, chapters, paragraphs, sentences, and words (the structure), but it is also necessary to understand the overall meaning of the words, the sentences, and chapters to fully understand the content of the textbook. You can think of this as the semantics of the program.

A line-by-line analysis of the program should give a better idea of both the structure and meaning of the classic "Hello World" program.

## A Detailed Explanation of the Code

### #include

#include <iostream>

The hash sign (#) signifies the start of a preprocessor command. The include command is a specific preprocessor command that effectively copies and pastes the entire text of the file specified in the angled brackets into the source code. In this case the file is "iostream" which is standard file which should come with the C++ compiler. This file name is short for "input-output streams"; in short, it contains code for displaying and getting text from the user.

The include statement allows a program to "include" this functionality in the program without having to literally cut and paste it into the source code every time.

### using namespace std

using namespace std;

C++ supports the concept of namespaces. A namespace is essentially a prefix that is applied to all the names in a certain set. The using command tells the compiler to allow all the names in the "std" namespace to be usable without their prefix. The iostream file defines three names used in this program - cout, cin, and endl - which are all defined in the std namespace. "std" is short for "standard" since these names are defined in the standard C++ library that comes with the compiler.

Without using the std namespace, the names would have include the prefix and be written as std::cout, std::cin, and std::endl.

### main()

The entry point of all C++ programs is the main function. This function is called by the operating system when your program is loaded.

### Console in, Console out

cout << "Hello, World!" << endl;
cin.get();

The name cout is short for "console output" and cin, correspondingly, is an abbreviation for "console input". The << symbol is a special operator that can be viewed as put to since you put to the console. This means cin is console in.

In a typical C++ program, most function calls are of the form object.function_name(argument1, argument2), such as cin.get() in the example above (where cin is the object, get is the function name, and there are no arguments in the argument list). However, symbols such as << can behave a functions as well, as is done with cout in this case. This functionality is called operator overloading which will be discussed later.

### { }

A block of code is defined with the { } tokens. { signifies the start of a block of code and } signifies the end.

NOTE: The { } tokens have other functions.

### semicolons

Each statement in C++ is ended by a semicolon. This is very roughly the equivalent of a sentence in a language like English. This tells the compiler where one operation ends and the next begins.

Note that it is common to hear someone refer to a "line of C++ code". This often can actually mean a single statement rather than literally a single line of text, since most programmers prefer a style of one line of text per statement.

### return

The return keyword stops the function where it is and returns a value (the return type must match the function's type) to the calling function. Using the return keyword is mainly effective to stop the program.

## Compiling the code

In order for the computer to run the code you have written, it needs to first be compiled by a C++ compiler.

### What the compiler does

In very broad terms, the compiler is a translator that acts an intermediary between the programmer and the CPU on the computer. A high-level language like C++ is actually a sort of 'compromise' language between the native language of the CPU (generally referred to as machine language) and the native language of the programmer (say English). Computers do not natively understand human languages, yet for someone to write computer code in the native language of the machine would be too difficult and time consuming. Therefore, the purpose of the computer language itself is to define a mid-point that is closer to how humans think and organize procedures but is still unambiguously translatable to the native machine language.

The compiler therefore is reading in the code written by the programmer and translating it into machine language code, that the computer can understand directly.

### Running the compiler

The code needs to be compiled with a compiler to finish the process. What if you don't have one? Well, the good news is, there are tons of good compilers that are available for free. The GNU Compiler Collection (GCC) has versions available for most systems and does a good job of implementing the ISO C++ standard. However, many people prefer to use an Integrated Development Environment (IDE) which provides a user friendly environment for developing programs. For MacOS X, there is Xcode which uses gcc for compiling C++. For Windows, there is Dev C++ which also uses gcc for compiling C++, Microsoft Visual C++ (and it's free Express version), TCLITE, and ports of the GNU Compiler Collection distributed within Cygwin and MinGW.

Each compiler has its own way of compiling programs. If you use GCC, type the following in a terminal:

g++ example.cpp -o example


Replace example.cpp with the actual name of the file you wish to compile. Replace example with the name you wish to give the executable program.

If you want to use a specific compiler, you will need to read up on that particular compiler.