Learning C++ for Life

Submitted on: 1/3/2015 1:01:00 PM
By: Alex Bylund (from psc cd)  
Level: Beginner
User Rating: By 16 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 330
     This is a tutorial I wrote to teach C++ to people who want to learn it. I decided to post it here to help as many people as possible. Please leave feedback, and if you like it vote ;)

				Learning C++ for Life -- Part 1
Revision 1.1.3
By: AxE (


This is my first text, I don't do spell check, and I don't write a lot.
This text is also ment for someone not knowing much about C++.

If you know another language, C++ wont be hard to learn. First if you don't
have a compiler go to and download the Dev-C++
IDE. It comes with everything you need. Its for Windows, but I'm guessing
if you are in Linux, you already have a compiler...

===Getting started==================================================

I'll start by a basic C++ program you could write, Hello world :)

/* Hello world */

#include <iostream.h>
int main()
cout << "Hello world" << endl;
return 0;

This code will print "Hello world" to the screen. If you run it from a
DOS window from windows, it will look like it doesnt work because will
flash up and then close. It only stays open while the program is runing,
and will close after the program is done (the program only prints to the
screen). You should run so you can see the results. Later in
this text, you will learn how to make it so the user must press a key to

Now for the explanations:

The first line '/* Hello world */' is completly ignored by the compiler.
These are called comments. Everything between '/*' and '*/' is ignored.
Even line returns. Other valid comments would look like this:

/* This is also
a valid comment */

This is also a valid comment

Just about anything works as long as you start the comment, and then end
it. You can comment out entire lines of code if needed. These type of
comments are usally called C comments, and can be used in both. The other
type of comment is using '//' and only works in C++. These comments only
work for the same line. After you hit return it ends the comment. Here is
a example of a correct comment:

// This is a comment

Here is a example of a incorrect comment:

// This is not
A comment

This is incorrect because the compiler will only ignore everything on the
same line after it. The compiler will thing you were meaning 'A comment'
as some instruction, and come up with some error.

Use comments when you think something is important to remember. So you don't
need comments when just printing to the screen. More like, leaving a comment
above a function saying what it does (More on that later).

The next line of code (third line in text) '#include ' Means to
include other code already writin so you don't have to re-program it when
ever you want to use it. iostream.h is a header file that holds the code you
need to use cout.

The next line of code 'int main()' starts the main function. Every program
needs a main function. The '{' and the '}' are parts of the main function.
Everything between { and } is the main function, this is where you put all
of your code. { and } are there to let the compiler know thats the start ({)
and the end ({) of the main function. The main function executes commands
and calles to other functions (more later).

The next line of code 'cout << "Hello world" << endl;' prints Hello world
to the screen... you probably already guessed that though :P endl means
to end the line. This is so if you print out anything else they will be
on different lines. When you put ';' at the end of the line, its telling
the compiler thats the end of that instruction. The insertion operation
(<<) is used to send the data to the stream. You could also put it like

cout <<
"Hello world"

You can do this because the compiler will ignore white space (spaces, line
returns, etc.). You wouldnt want to put this over multiple lines though
because its easyer to read it when its just on one line. You could also
do it like this if you wanted:

cout<<"Hello world" < ;

Multiple line and spaced around ;) Like I said though, you wouldnt want to
do that because it just makes it harder to read.

The last line of code in the main function 'return 0;' tells it that the
program is done. When you return zero in the main function it will end
the program.

===For loops========================================================

Lets edit the program to print "Hello world" to the screen ten times,
without typing "cout << "Hello world" << endl;" ten times. To do this
we need a loop. A loop will execute the same instructions until what
you have programmed it for is complete.

Here is the full Hello World loop program:

/* Hello world */
#include <iostream.h>
int main()
for (int x = 0; x < 10; x++)
cout << "Hello world" << endl;
return 0;

Looking at the for loops:

for (int x = 0; x < 10; x++)

The first part 'int x = 0;' initializes x, and sets it to zero. More on
variables on the next section.

The second part 'x < 10;' means the loop will keep running until this
statement is false. So it will keep running as long as x is less than 10.

The last part 'x++' is what the loop should do every time it runs threw
it. 'x++' is like 'x = x + 1'. So every time the loop is ran it will add
one to the value of x.

Everything between the '{' and '}' will be done. Don't confuse the
main {} with the for {}. The for loops also need {} to know everything
part of it. You can make a loop without them, but it can only execute
one line of code. Example would be:

for (int x = 0; x < 10; x++)
cout << "Hello world" << endl;

It looks the same, but anything under the cout will be considered out
of the loop. A loop you would need brackets would be like this:

for (int x = 0; x < 10; x++)
cout << x << ": ";
cout << "Hello world" << endl;

Of corse that same loop could be done in one statement, but its just a
example. If you're wondering what it would look like in one statement
it would be like this:

for (int x = 0; x < 10; x++)
cout << x << ": " << "Hello world" << endl;


Now you will learn how to make variables to hold numbers. This is a
demonstration on how to get two numbers from a user, and then add them

#include <iostream.h>
int main()
int number1, number2, number3;
cout << "Please enter a number ";
cin >> number1;
cout << "Please enter another number ";
cin >> number2;
number3 = number1 + number2;
cout << endl << number1 << "+" << number2 << "=" << number3 << endl;
return 0;

This example introduces you do cin. cin will take a input from the user.

'int number1, number2, number3;' tells the compiler those variables will
hold numbers. Putting the varialbes on one line seperating them with a
comma is like killing three birds with one stone ;) Instead of telling
each one what they are, you can just put commas and the compiler knows you
want them all to be ints.

If you int a variable, it will hold values -2,147,483,648 to
2,147,483,647 (32-bit). There are other ways to tell the computer you are
going to use numbers.

The way they are different is because they hold different values. For
example, if you did 'double number1;' it would hold values 2.2e-308 to
1.8e308. Thats WAY to much to get user input on two numbers to add. So using
int is probably good for now.

After you have told the compiler you want three variables, you can use them.
:P You must declare the variable before you use it.

'cin >> number1;' will get the number for variable number1, and the same
will happen for 'cin >> number2;'

'number3 = number1 + number2;' does exactly what it says. It makes variable
number3 equal the sum of number1 and number2.

'cout << endl << number1 << "+" << number2 << "=" << number3 << endl;'
might look strange at first glance, but its easy if you look through
it ;) It first goes to a new line, then prints the value of number1,
"+", number2, "=", number3, and then ends that line.


You should know that the main function is needed at all times. What about
other functions though? Now you will learn how to add two numbers like
before, except the addition will be done by a function other than main.

#include <iostream.h>
int add(int numb1, int numb2);
int main()
int number1, number2, number3;
cout << "Please enter a number ";
cin >> number1;
cout << "Please enter another number ";
cin >> number2;
number3 = add(number1, number2);
cout << endl << number1 << "+" << number2 << "=" << number3 << endl;
return 0;
int add(int numb1, int numb2)
return numb1 + numb2;
'int add(int numb1, int numb2);' is needed for all functions. You have to
declare the function before it is used. The int before the function means
it will return a int number. When you declare a function this way, it has
to be the same format as the actual function. So if its a int, and has two
ints that are sent to it, so must the declaration.

'number3 = add(number1, number2);' this sets the variable number3 to what
ever the add function returns. It sends number1 and number2 to the function.

The function:
int add(int numb1, int numb2)
return numb1 + numb2;
The function input variables are numb1 and numb2. You sent the values of
number1 and number2 to act as numb1 and numb2. The function then adds the
two values, and returns the product.

Sence number3 will equal what ever the function returns, it now equals
number1 plus number2.

===IF Statements====================================================

If statements will run the code if a expresion is ture. For example:

if (number1 > 10)
cout << "That value is greater than 10";

It would only display the message if the number was greater than 10.

If statements can also be multiple line, this is a example:

if (number1 > 10)
cout << "That value is greater than 10";
cout << endl;
You can also use ELSE. Here is a example:

if (number1 > 10)
cout << "That value is greater than 10";
cout << endl;
cout << "That value is less than 10";
cout << endl;

So if 'number1 > 10' is false, then it will go to the else section and do
what ever code you have there.


You should now understand more about how C++ works, and be able to learn new
things on your own.

This is the end right now, because I don't know what else to put in ;)
To get part 2 when it comes out, or a newer version of this text go to one
of these sites:

You may distribute this text as you wish, as long as you leave everything
the way it is. If you feel something needs to be changed or added, e-mail

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.