Loading lesson path
C++
C++ Tutorial focused on C++ Introduction and related concepts.
C++ is a popular programming language.
C++ is a cross-platform language that can be used to create high-performance applications.
If you want to run C++ on your own computer, you need two things:
Let's break up the following code to understand it better:
A computer program is a list of "instructions" to be "executed" by a computer.
The cout object, together with the << operator, is used to output values and print text.
You can also use cout() to print numbers.
To insert a new line in your output, you can use the \n character:
Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Comments can be singled-lined or multi-lined.
Variables are containers for storing data values.
To declare more than one variable of the same type , use a comma-separated list:
All C++ variables must be identified with unique names .
When you do not want others (or yourself) to change existing variable values, use the const keyword (this will declare the variable as "constant", which means unchangeable and read-only ):
You have already learned that cout is used to output (print) values. Now we will use cin to get user input.
As explained in the Variables chapter, a variable in C++ must be a specified data type:
Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you need a floating point number (with decimals), like 9.99 or 3.14515.
A boolean data type is declared with the bool keyword and can only take the values true or false .
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':
The string type is used to store a sequence of characters (text). This is not a built-in type, but it behaves like one in its most basic usage. String values must be surrounded by double quotes:
The auto keyword automatically detects the type of a variable based on the value you assign to it.
Operators are used to perform operations on variables and values.
Arithmetic operators are used to perform common mathematical operations.
Assignment operators are used to assign values to variables.
Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.
As with comparison operators , you can also test for true ( 1 ) or false ( 0 ) values with logical operators .
When a calculation contains more than one operator, C++ follows order of operations rules to decide which part to calculate first.
Strings are used for storing text/characters.
The + operator can be used between strings to add them together to make a new string. This is called concatenation :
C++ uses the + operator for both addition and concatenation .
To get the length of a string, use the length() function:
You can access the characters in a string by referring to its index number inside square brackets [] .
Because strings must be written within quotes, C++ will misunderstand this string, and generate an error:
It is possible to use the extraction operator >> on cin to store a string entered by a user:
You might see some C++ programs that run without the standard namespace library. The using namespace std line can be omitted and replaced with the std keyword, followed by the :: operator for string…
C-style strings are created with the char type instead of string .
C++ has many functions that allows you to perform mathematical tasks on numbers.
Very often, in programming, you will need a data type that can only have one of two values, like:
A Boolean expression is a piece of code that compares values or variables and returns a boolean value: 1 (true) or 0 (false).
You already know that C++ supports familiar comparison conditions from mathematics, such as:
Use the else statement to specify a block of code to be executed if the condition is false .
Use the else if statement to specify a new condition to test if the first condition is false .
There is also a short-hand if...else , known as the ternary operator because it uses three operands.
You can also place an if statement inside another if . This is called a nested if statement.
You can combine or reverse conditions using logical operators . These work together with if , else , and else if to build more complex decisions.
Use the switch statement to select one of many code blocks to be executed.
Loops can execute a block of code as long as a specified condition is reached.
The do/while loop is a variant of the while loop. This loop will execute the code block once , before checking if the condition is true. Then it will repeat the loop as long as the condition is true.
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
It is also possible to place a loop inside another loop. This is called a nested loop .
There is also a " for-each loop" (also known as ranged-based for loop), which is used to loop through elements in an array (or other data structures ):
The break statement can also be used to jump out of a loop .
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
You can loop through the array elements with the for loop.
In C++, you don't have to specify the size of the array. The compiler is smart enough to determine the size of the array based on the number of inserted values:
To get the size of an array, you can use the sizeof() operator:
A multi-dimensional array is an array of arrays.
Structures (also called structs) are a way to group several related variables into one place.
An enum is a special type that represents a group of constants (unchangeable values).
In the example from the earlier example, the & operator was used to create a reference variable. But it can also be used to get the memory address of a variable; which is the location of where the va…
You learned from the previous chapter, that we can get the memory address of a variable by using the & operator:
In the example from the earlier example, we used the pointer variable to get the memory address of a variable (used together with the & reference operator). However, you can also use the pointer to g…
You can also change the pointer's value. But note that this will also change the value of the original variable:
Memory management is the process of controlling how much memory your program uses - and how it is used. This includes creating, using, and releasing memory when it's no longer needed.
The new keyword lets you manage memory yourself.