You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 32 Next »

Whether you have had C++ before or not, this page is made to give you a brief introduction to it's most important features.

As always, do keep in mind that this is only one of many good tutorials on C++. We highly recommend you to check out other tutorials online as well, as they might explain things from a different point of view or include information that is not included here. Some of these tutorials are:

We will also use hyperlinks to relevant documentation when explaining various topics. We encourage you to get used to using these, as they will definitely help you out later.

Should you have a suggestions to any specific topic that you would like to see included here, please contact us by mail or through the forums.

Want to get better at programming, check out our page Tips and tricks for coding.

Page content

Getting started

After downloading Visual Studio (or another compiler, though they won't be explained here), it is time to make our first project!


Visual Studio 2019 is used in this tutorial.

  1. Open VS and click on Create a new project. Choose then an Empty Project (make sure it is a C++ project) and click Next.
  2. Proceed by giving your project a name (e.g. "MyProject" and a location. Click Create.
  3. Your project will then open. Here, you may edit the layout of VS as you want by dragging the different modules around.
  4. To write our code, we will need a new file in our project. To add a new file, right-click on your project name → Add → New Item. You may also use the shortcut Ctrl + Shift + A.
  5. Choose C++ File (.cpp), rename it if you want and click Add. The difference between a C++ File (.cpp) (often reffered to as only "cpp file" or "source file") and a Header File (.h) is very important and will be discussed later, but for now, just ignore it.
  6. You have now made your first project! Continue reading at this page to learn how C++ is written.

Now that we have made a working project, let's implement the well known Hello World program.

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.

Hello World
#include <iostream>
using namespace std;

int main() {
	cout << "Hello World!";
	return 0;
}

Be aware of the semicolons.


Alright, time to take a deeper look at the code. The first line is

#include <iostream>

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).

using namespace std;

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.

Hello World, but without using namespace std;
#include <iostream>

int main() {
	std::cout << "Hello World!";
	return 0;
}

Next up is

int main() {
	...
	return 0
}

What you see above is an example of a function in C++ (functions will be further covered later). 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.

Hello World, not using main function -> Error
#include <iostream>
using namespace std;

cout << "Hello World!";

// Lines starting with "//" is a comment
// This code will result in an error when trying to build.

Lastly, we have

cout << "Hello World!";
// or, as previously mentioned
std::cout << "Hello World!";

cout is the standard output stream (stands for console or choaracter output), and is what enables us to write to screen. cout is also a function, but since it is used so often, it has been given a special operator "<<", making the function call easier. We will describe this even further in a later section.

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++.

Variables

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 differently. Among other things, we need to declare what type our variable is before we can give it a value.

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

int myInteger					// e.g. -1, 2, 3 ...
unsigned int myPositiveInteger	// e.g. 0, 1, 2 ...
float myFloat					// e.g. -1.12, 2, 10e2 ...
double myDouble					// similar to float, but in general, float has 7 digits of precision while double has 15.
bool myBool						// 1, 0, true, false
char myChar						// 'a', 'b', 'v' ... One character
string myString					// "a", "Hello", "Hi again" ...			






  • No labels