Thursday 11 September 2014

#1 : Bare Basics - Quick C++ Tutorial

How to use an IDE

You can use any IDE (if you are going to use one) out there. The one I prefer is Code::Blocks, which is a free IDE which can compile and run C/C++ programs. Later on, you may use a more sophisticated IDE like Microsoft Visual C++ or Eclipse. Open it up, create a new project, open up 'main.cpp' from the side panel, and get cracking!

First C++ Program

Copy (or type, which is much better) this code into the IDE.
#include <iostream>

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

Now click Build -> Build and Run to compile and run the program. If you have copied/typed correctly, you will now see the output now, which will say, "Hello, World!". Congratulations on running your first C++ program.




Now let's break down what is happening.

  • #include <iostream> 'include's or gets the program access to all the things inside the 'library' iostream (which stands for input-output stream). You can think of iostream as a folder, inside which there are certain files that are required to do input/output operations. By 'include'ing the library in your program, the program can access these files.
  • int main() is a function. A function is simply an entity or a block of code that does something. main() function is the most fundamental block of code in your program. As the name indicates, it is the most important function in your program, without which, your program will not run. Since our aim is to output a message to the user, we do this inside the main function. int stands for integer. This is called a return type, indicates that the function which we are working on has something to do with numbers, As per the new standards, every main() function must have a return type int. We will learn to make other functions as per our needs later on. return 0; at the end of the code puts an end to the main function. 
  • std::cout<< is the used to output the message to the screen. Anything that comes after std::cout<< is put to the screen. In this case a string (which is enclosed by two double inverted commas) is output. std::endl puts anything that comes after it, on the next line. Since std::endl is not a string (it is not to be enclosed between two double inverted commas) it requires another <<.
  • As you cans see, most of the lines in this program ends with a semicolon (;). This is to let the compiler know the particular operation is over. Also the all the operations inside the main() function is enclosed by a pair of flower brackets. Everything inside one pair of flower braces is called one block of code. As the programs get more complex number of blocks in your program will increase.
A peek on namespaces

Include a line 'using namespace std;' just below the first line of code. Now you can write just cout<< instead of std::cout<< or just endl instead of std::endl<< in your codes. We will learn why this happens later in the course. Run the modified code given and you will see that the output is exactly the same as the first one

#include <iostream>
using namespace std;

int main()
{
    cout<<"Hello, world!"<<endl;
    return 0;
}

Exercises
  • Remove the semicolon in your code and try running the program. What do you see? 
  • Write your own message in the program.
  • Write the words 'hello' and 'world' in two separate lines using endl.

You might also like