C++ Tutorial for beginners – Basics of C++

Basics of C++ | Variables | If statements | Loops | Functions

 

Welcome to this C++ tutorial. Before you start you will need to get a C/C++ compiler such as Visual C++ or Borland, if you do not have one you can download a free one at www.borland.com

 

Anything with a yellow background is code, and anything with a green background is a portion of code.

I have done this tutorial based on Microsoft VC++ 6.0 although it should work with all C/C++ compilers.

 

 

 

Ok lets start. First of all copy this into your compiler, then compile and run.

1

2

3

4

5

6

7

#include <iostream.h>

 

int main()

{

            cout << "Hello world" << endl;

            return 0;

}

You should now see Hello world written on the screen. Lets break this code down and explain how it works.

 

 

1

#include <iostream.h>

When creating a program you will need what are called header files, these are files that get included in to your program. If you have programmed in another language such as Visual Basic you may be use to having built-in commands. C++ doesn’t have many built in commands you normally have to include a header file. In this example we have included iostream.h, which stands for Input-Output stream, we need this file in order to be able to use the cout command and later on we will use the cin command.

 

3

int main()

For your program to work you need to have int main() this is the function that is called once you start your program.

 

4

{

This opening bracket tells the compiler that any code below this (until }) belongs to int main()

 

5

cout << "Hello world" << endl;

This will print the text on to the screen. Where you see "Hello world" you can put any string there. For example:

 

   int main()

   {

                        int numofplayers = 20;

                        cout << “There are “ << numofplayers << “ players” << endl;

                        return 0;

   }

 

This will return “There are 20 players”

The last expression printed to the screen is “endl”. This will make the cursor go to the next line.

The semi-colon used after cout is to tell the compiler to go to the next command.

 

6

return 0;

Because the function is an int (integer) we must return a value, in this case we will return 0 although it doesn’t matter as yet what we return.

 

 

 

Ok, now we have learnt the cout command and is time to learn the cin command. The cin command allows you to type in something from the keyboard etc. and will save it in a variable when you press enter.

1

2

3

4

5

6

7

8

9

10

#include <iostream.h>

 

int main()

{

            int YourAge;

            cout << "Please type in your age? ";

            cin >> YourAge;

            cout << "You are " << YourAge << endl;

            return 0;

}

This will print on the screen Please type in your age? And will wait until you type something and press enter. It will then print on the screen “You are” followed by the age you typed in.

 

 

5

int YourAge;

This is defining YourAge to a variable and it will be an integer.

 

6

cout << "Please type in your age? ";

This is a tiny bit different than last time we used cout because it doesn’t have “endl” at the end, this is because endl makes the cursor go to the next line and we want it to stay where it is.

 

7

cin >> YourAge;

This is the command that lets you type in something via the keyboard. When you are typing you will see what you type on the screen and when you press enter the variable YourAge will set to what you typed. Note: YourAge is an integer so if you do not type a number in then it will return 0. Once you press enter the cursor will go to the next line