Getting started
After downloading Visual Studio, it is time to make our first project!
Expand | ||
---|---|---|
| ||
|
Now that we have made a working project, let's implement the well known Hello World program.
Expand | |||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||||||||||||||||||||||||||||||
To get a better understanding of how the code works, we will first implement the complete program and afterwards we will go through it step by step. To run the code, go to Debug → Start debugging/Start Without Debugging.
Alright, time to take a deeper look at the code. The first line is
In C++, much like in Python, we have to include libraries (headers) to perform certian actions, e.g. writing to file or solving math equations. iostream is the standard input/output streams library that enables us to print to screen (or e.g. a file).
In this example we have chosen to include the line above to make getting into C++ easier. What using namespace std does is to silently insert std:: (std = standard) as a prefix to a lot of our standard functions. In the example above, cout will therefore be interpreted as std::cout. The short answer to why this is necessary is to specifiy where (which library) the function comes from in the case we have multiple functions with the same name. An analogy to this is how two students with the same name "Tom" are differentiated by adding e.g. the first letter of their last name; "Tom H." and "Tom B.". By specifying the standard namespace for our entire program, we will avoid having to write std:: before many of our functions, which is benefitial when first learning C++. However, note that this shortcut comes with a cost. To learn more about these negatives, read this post on Stack Overflow explaining why it is considered a bad practice.
Next up is
What you see above is an example of a function in C++ (functions will be further covered later). The brackets " { " and " } " are in C++ used to group a block of statements, in this case those within the function main. Other examples where they are used are in e.g. for loops or if else statements. The equivilant to this in MATLAB is then end statement, and in Python it is the whitespace indent. The special thing about the main function is that in C++, a program shall contain a global function named main, which is the designated start of the program (the only function called when running). This is very different from both Python and MATLAB, where programs can be written without using any functions at all. To us, it means that every action we want to execute has to be described from within the main function, although you're of course allowed to call on other functions.
Lastly, we have
cout is the standard output stream (stands for character output), and is what enables us to write to screen. cout is a statement, where "<<" is the insertion operator indicating what follows is inserted into cout. We will describe this even further in a later section. Also, notice that every statement is terminated with a semicolon ( ; ). Forgetting this is a very common source of error. Congratulations, we have now not just made a simple program, but we have also dove deeper into the meaning of our code, thus already touching multiple important features in C++. For even more details on the Hello World example, visit this page. |
Variables and operators
You are probably familiar from Python or MATLAB that variables can be e.g. integers, floating-points or strings. In C++, variables are handled a bit more carefully. Among other things, we need to declare what type our variable is before we can give it a value.
Expand | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||
As mentioned, variables can not be given any value without first beeing told what kind of data will be stored there. A full list of all variables and their size/precision can be found at the variable documentation, but some of the most used are
In the example above, some variables are a bit different.
When initializating variables in C++, three methods are used:
When working with different variables, keep in mind how the variable operates. An example of this is the int, which will always round down to the lowest integer.
As shown, static_cast< new_type >( expression ) can temporarily convert a variable to another type in order to bypass the limits set by the original type. To read more about other type conversions, visit this page. |
Expand | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||||||||||||||
C++ has a lot of operators which when used correctly can make your code very efficient. Considering the scope of these operators, we will only cover the basics here, and leave you to visit further documentation to learn more about other operators, as well as their precedence. Arithmetic operators C++ supports five arithmetic operators, shown the the following table:
Note that compared to MATLAB or Python, we are in fact missing an operator, power. In C++, this operator is instead included as the function pow( expression, exponent ).
Relational and comparisonal operators When comparing expressions, the following operators are used:
Logical operators When evaluating logical operators, C++ only evaluates what is necessary to compute a result from left to right.
|
Statements and flow control
We will now have a look at the selection statments if else and switch case and the iteration statments for loops and while loops, as well as some other statements included in these. Please note that we will assume that you understand how the different statements work in general (maybe not switch case, as this is not included in Python), thus enabling us to focus more on the syntax, as well some useful tips and tricks. However, if you're completely new to this, or simply want a more detailed walk-through, have a look at the tutorials linked at the top of this page for an even more detailed introductionlinks above.
Expand | |||||
---|---|---|---|---|---|
| |||||
The if else statement is used to execute a block if and only if a condition is fulfilled.
|
Expand | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||
The while loop will execute a block of statments as long as an expression is evaluated as true. In C++, the syntax is as following.
Another type of while loop available in C++ is the do-while loop. It behaves very similar to a regular while loop, but instead of evaluating the expression in the beginning, a condition is evaluated at the end. This introduces a functionality to the while loop that may in some cases be very helpful, it makes it execute the block of statements at least one time. To illustrate a scenario like this, imagine that you want to make a program that asks for your real name until you write something else than "Smith".
If we were to do the same task with a regular while loop, we would have to do something like this:
|
BibTeX Display Table |
---|