Preview page as viewer
Home‎ > ‎

Lesson One - Getting Set Up

LESSON OVERVIEW

-History of C++ and how it came to be
-Setting up a compiler and learning what it does
-Compiling and running your first program
-Explaining the code


THE HISTORY OF C++

C++ was developed by a man named Bjarne Stroustrup at Bell Labs in Murray Hill New Jersey. It saw its first commercial release in 1985.


SETTING UP A COMPILER AND LEARNING WHAT IT DOES

A compiler is a program that transforms source code into code that the computer can read. Programming languages are designed so that humans can interact with computers, they are a mix between computer and human language, since computers cant understand human language and its extreamly difficult for humans to understand computer language they meet half way with programming languages. The compiler takes the half human and half computer language and turns it directly into language that the computer can understand and execute. There are hundreds of compilers out there today for many languages but we are going to be using Code::Blocks. Go to Code::Blocks main website, click on "Downloads" then click "Download the binary release" then you want to choose the one that says "codeblocks-12.11mingw-setup.exe", you must choose a download mirror. This compiler is 100% free. Once that is done continue on.


COMPILING AND RUNNING YOUR FIRST PROGRAM

Once you have downloaded and installed CB run the program and when you get to the main screen click on the "Create a new project". Once you have done that you should see a window with a bunch of different icons, waren't going to worry about all of these they are irrelevant to what we want to do. The only one we are concerned with is the one that says "Console application", double click on that and another window will pop up and the two choices in the box are "C" and "C++". Make sure "C++" is highlighted and click the next button. Now you need to name your program. Enter something in the field and click next. On this next screen dont click anything other than Finish. Now you should see this in your window:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    cin.get(); //You must include this line as it is not in the template
    return 0;
}

In the next segment I will explain what every bit of this code does but for now just go up in the toolbar and click on the "Build" option and click "Build and run". You should see a black console window with the text: Hello World!. Congratulations you have just sucessfully compiled and ran your first C++ program!


EXPLAINING THE CODE

Ok so now that you have compiled and ran the code you need to understand what its actually doing.


#include <iostream>

The first part is #include.  What this is, is a directive to the preprocessor to include the contents of that file, in this case iostream, into our program. Now what is iostream you may be asking? well IO stands for Input and Output stream. This lets us use cout, cin endl and a few other things that we wont worry about now in our program. If we did not include this we would get compiler errors. and here is what they would look like:

In function 'int main()':
error: 'cout' was not declared in this scope
error: 'endl' was not declared in this scope
error: 'cin' was not declared in this scope
||=== Build finished: 3 errors, 0 warnings ===|


using namespace std;

What this means is that we are using a namespace called std which stands for standard as in the standard c++ library. You also need std to use cout, cin and endl as they are apart of the std namespace. Now many programmers prefer to do something like one of the two ways here:

#include <iostream>

using std::endl;
using std::cout;
using std::cin;

int main()
{
    cout << "Hello world!" << endl;

    cin.get();
    return 0;
}

Or you can do this:

#include <iostream>

int main()
{
    std::cout << "Hello world!" << std::endl;

    std::cin.get();
    return 0;
}

The first way is probably the easiest as it saves time from writing std:: in front of everything.

int main()
{

}   

main is absolutely necessary in any C++ program, it is where the compiler first goes to to run your code.