Cours gratuit: Lesson language C++

Extrait du cours lesson language C++

Lesson 1: The basics of C++
I am writing this for those people who want to learn how to program in C++, especially those who had trouble. It is for those of you who want a sense of accomplishment every time your program works perfectly. If you want the sense of accomplishment, read on.
C++ is a programming language. It is a programming language of many different dialects, just like each language that is spoken has many dialects. In C though, they are not because the « speakers » live in the North, South, or grew up in some other place, it is because there are so many compilers. There are about four major ones: Borland C++, Microsoft Visual C++, Watcom C/386, and DJGPP. You can download DJGPP http://www.delorie.com/djgpp/ or you may already have another compiler.
Each of these compilers is a little different. The library functions of one will have all of the standard C++ functions, but they will also have other functions or, continuing the analogy, words. At times, this can lead to confusion, as certain programs will only run under certain compilers, though I do not believe this to be the case with the programs in these tutorials.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
If you don’t have a compiler, I strongly suggest you get one. A simple one is good enough for
my tutorials, but get one.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C++ is a different breed of programming language. It has only a few keywords for DOS, and it has no keywords to use for output. This means that almost everything is stored in a header file. This gives the use of many functions. But lets see a real program…
#include <iostream.h>
int main()
{
cout<<« HEY, you, I’m alive! Oh, and Hello World! »;
return 0;
}
That does not look too hard, right? Lets break down the program and then look at it. The #include is a preprocessor directive which tells the compiler to put code in the header file iostream.h into our program! By including header files, you an gain access to many different functions. For example, the cout function requires iostream.h.
The next thing is int main() what this is saying is that there is a function called main, and that it returns an integer, hence int.
Then those little braces ( { and } ) are used to signal the beginning and ending of functions, as well as other code blocks. If you have programmed in Pasal, you will know them as BEGIN and END.
Lesson 3: Loops
Loops are used to repeat a block of code. You should understand the concept of C++’s true and false, because it will be necessary when working with loops.There are three types of loops: for, while, (and) do while. Each of them has their specific uses. They are all outlined below.
FOR – for loops are the most useful type. The layout is for(variable initialization, conditional expression, modification of variable) The variable initialization allows you to either declare a variable and give it a value or give a value to an already declared variable. Second, the conditional expression tells the program that while the conditional expression is true the loop should continue to repeat itself. The variable modification section is the easiest way for a for loop to handle changing of the variable. It is possible to do things like x++, x=x+10, or even x=random(5);, and if you really wanted to, you could call other functions that do nothing to the variable. That would be something ridiculous probably.
Ex.
#include <iostream.h> //We only need one header file int main() //We always need this
{ //The loop goes while x<100, and x increases by one every loop for(int x=0;x<100;x++) //Keep in mind that the loop condition checks the x value before it
{ //Actually repeats, or loops, again. So if x==100 the loop will end. cout<<x<<endl;
//Outputting x
}
return 0; }This program is a very simple example of a for loop. x is set to zero, while x is less than 100 it calls cout<<x<<endl; and it adds 1 to x until the loop ends. Keep in mind also that the variable is incremented after the code in the loop is run for the first time. WHILE – WHILE loops are very simple. The basic structure is…WHILE(true) then execute all the code in the loop. The true represents a boolean expression which could be x==1 or while(x!=7) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x==5 || v==7) which says execute the code while x equals five or while v equals 7..
Ex.
#include <iostream.h> //We only need this header file
int main() //Of course…
{
int x=0; //Don’t forget to declare variables
while(x<100) //While x is less than 100 do
{
cout<<x<<endl; //Same output as the above loop
x++; //Adds 1 to x every time it repeats, in for loops the //loop structure allows this to be done in the structure
}
return 0;
}
This was another simple example, but it is longer than the above FOR loop. The easiest way to think of the loop is to think the code executes, when it reaches the brace at the end it goes all the way back up to while, which checks the boolean expression.
DO WHILE – DO WHILE loops are useful for only things that want to loop at least once. The structure is DO {THIS} WHILE (TRUE);
For example:
#include <iostream.h>
int main()
{
int x;
x=0;
do
{
cout<<« Hello world! »;
}while(x!=0);
return 0;
}
Keep in mind that you must include a trailing semi-colon after while in the above example. Notice that this loop will also execute once, because it automatically executes before checking the truth statement.

……….

Si le lien ne fonctionne pas correctement, veuillez nous contacter (mentionner le lien dans votre message)
Cours gratuit: Lesson language C++ (290 KO) (Cours PDF)
Lesson language C

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *