AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Learn C++ easily: C++ tutorial

PARTAGEZ

The C++ programming language has become essential in the field of system programming. With a C++ tutorial in hand, even beginners can quickly learn the basics of the programming language.

Application areas of C++

C++ is a object-oriented programming language thus suitable for many usage scenarios. This language is nevertheless mainly used in the field of systems programming because of its efficiency. The popular database management system MongoDB or Apple OS X are thus designed in C++. C++ is the ideal language if available resources are scarce.

However, the programming language is also suitable for programming of various applicationssuch as creating user interfaces or even games.

Differences between C, C++ and C#

If you are interested in C++, you may have already heard of the languages C and C#. The first is the foundation of C++ and C#. The imperative and procedural programming language C contains fewer elements than C++ or C#. Since the 1970s, C has been used primarily for system programming and hardware programming. The main reason is the high portability of the programming language.

C++ and C# are extensions of C which primarily support object-oriented programming. C# can be considered an extension of C++ in turn. This is why C# is also called C++++. Although C++ and C# are both object-oriented programming languages ​​based on C, they are not identical: C++ offers programmers greater freedom and portability than C#.

What you need to get started programming in C++

To learn C++, you need a way write and compile your own C++ programs. In principle, all you have to do is opt for a text editor and write your code there. You can then translate your .cpp files into executable programs using a compiler. Depending on the operating system you are working on, you can either download such a compiler or use it directly in the terminal.

You can also use a integrated development environment or EDI (IDE in English). The advantages of such an IDE are undeniable: in addition to syntax highlighting, you can test and compile your program directly in the development environment. Development environments are also very useful for debugging the programs you have written. There are many different IDEs that support programming in C++, for example VisualStudio Or Clion.

If you are taking your first steps in programming, it may also be useful to use external sources to illustrate basic C++ concepts. For example, you can find many educational materials on YouTube that will help you learn C++. For example, the “Programming with Mosh” channel has the video “C++ Tutorial for Beginners – Learn C++ in 1 Hour.”

Besides YouTube, the Microsoft C++ Documentation is an excellent source of information.

The syntax of the most important control structures

The C++ syntax is very similar to that of C. Thus, most C++ operators are also found in C. So if you already know how to program in C, the C++ tutorial will probably be easy for you to approach.

As in any programming language, a distinction is made between the various control structures in C++, all of which must be formulated syntactically correctly so that your code can compile without errors. Other elements, such as comments, are subject to specific syntactical rules.

The rule of thumb for programming in C++ is that the language is case sensitive, i.e. a distinction is made between upper and lower case letters. In addition, all instruction blocks are surrounded by braces. An indentation, as is called in other programming languages ​​such as Python, is not required due to parentheses, but greatly improves code readability. Each statement ends in C++ with a semicolon.

Are you also interested in other programming languages? Our beginner-friendly tutorials make your first steps in programming easier:

Comments

If you want to write notes about your code that the compiler should ignore, you can use comments. You have several ways to create them in C++:

// commentaire d’une ligne
/* commentaire qui s’étend
sur plusieurs lignes */

C++

Data Types

Different types of data are distinguished in C++, like other programming languages. These are initiated with specific keywords. If you want to declare a variable, assign it a data type directly in C++:

// nombre entier
int nombre = 5;
// nombre décimal
float nombre_decimal = 0,5f;
// chaîne de caractères
string chaine_caracteres = "Bonjour !";
// caractères ou char
char lettre="D";
// valeur logique
bool valeur_logique = true;

C++

The list of data types is not comprehensive and only shows the most common and frequently used data types.

if-else statement

As in most programming languages, an if-else statement allows certain parts of the code to be executed only when a condition is met. filled. The syntax you use in C++ is already familiar to C or Java programmers.

if (condition) {
	// code exécuté lorsque la condition est remplie
} else {
	// code exécuté lorsque la condition n’est pas remplie
}

C++

for and while loops

THE loops are also a basic concept of programming found in C++. Use a While Loop if you want to repeat a specific part of your code until a specific condition no longer applies:

int i = 0;
while (i <= 5) {
	cout << i << "\n";
i++;
}

C++

An integer variable is declared in the above code. It allows you to store integers. The variable named i initially gets the value 0. The While Loop is then executed until the number stored in i exceeds the value 5. As each loop is executed, the current value of the variable is output and then incremented by 1 ( incremented).

The same behavior can be reproduced in code with a “C++ for loop”, also called a “count loop”. The highlight: a counter variable is declared in the for loop and automatically modified on each loop execution:

for (int i = 0; i <= 5; i++) {
	cout << i << "\n";
}

C++

switch-statement

C++ provides an elegant way to distinguish between different conditions and execute code based on them, using switch statement control structures.

int input;
cin >> input;
switch (input) {
	case 0:
		cout << « Un 0 a été saisi »;
		break;
case 1:
		cout << « Un 1 a été saisi »;
		break;
	case 2:
		cout << « Un 2 a été saisi »;
		break;
	default:
		cout << « Un autre chiffre a été saisi »;
		break;
}

C++

The code above prompts the person running the program to enter an integer which is populated in the variable named “input”. In the following switch statement, the value of this variable is used to distinguish the different parts of the code: this number is displayed when a 0, 1 or 2 is entered. Otherwise, the statement initiated by the default keyword is executed.

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact