C++ 1. Introduction

What can you do with C++?

Games, embedded systems, web browsers, back end, enterprise software, operating systems, and the list goes on. C++ is a Turing complete language, which means that it can be used to create literally anything.

 

Tasting the language

Since this is an introduction and I don't want to break with tradition, I will go through the "Hello world!" program to cover some basics line by line. This program will simply print out Hello world! in the terminal.

// A simple program
#include <iostream>

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

Output:

Hello world!

 

// A simple program

The very first line (seen above) is a comment. Every character after // is a comment and is ignored by the compiler. Comments are just for you and all people who will read your code to help yourself and them to understand it, and will not be compiled. It's a good idea to make it a habit to comment your code in places where it may be hard to understand exactly what the code does. Imagine if you're on a big project and letting it rest for a couple of months, maybe even a year for whatever reason, when you come back to it you will most likely have forgotten most of it.

This line only comments out this single line, to comment out multiple lines you can use /* and end the comment with */. For example:

/*    This is a comment   on multiple lines*/

 

#include <iostream>

This is called a preprocessor directive. You can read more about preprocessing here. Basically, we are telling the computer to include a file, namely iostream. This particular file is needed for every program that inputs or outputs data on the screen. We will print out Hello world! so our program depends on it.

 

int main() {

Here is the start of every program ever written in C++. This is called a function, indicated by the parentheses after main. A function starts with a left brace { and ends with a right brace }. Therefore, if you look at our code, everything in this example will be done in the main function.

To write a function, you begin with what data type the function will return. Functions are typically doing some calculations, and we are interested in the result of these calculations. The main function will return an integer, written int. After that, the function needs a name so it can be referred to. The main function is simply named main. A function can also receive data, this is done by writing the data type and identifier inside the parentheses. I will explain functions in more depth in a later article.

 

	std::cout << "Hello world!" << std::endl;

If you guessed that std stands for Sexually Transmitted Disease, you're wrong, sadly. It stands for Standard Library. The entire line is called a statement, and outputs Hello world! on the screen. Statements end with a semicolon. Characters between two double quotation marks is called a string, "Hello world!" is a string.

Remember that we had to include the file iostream to output data on the screen. cout belongs to the namespace std. By typing std::cout, we are telling the computer that we want to use a name, cout, that belongs to std.

The << is called stream insertion operator. We insert the value to the right of this operator, the string "Hello world!", to the output stream. std::endl prints a new line.

	return 0;

As said before, the main function returns an integer. The return statement exits the function. Returning 0 indicates that the program has terminated successfully. This line can be omitted, because if the program come to this point it assumes that the program was terminated successfully anyway.

}

The last line, a right brace, is closing the function.

 

This was a brief introduction to the C++ language. If you don't understand everything completely yet, don't worry, you will soon.

Click here to read C++ 2. Escape Sequences

Enjoyed this article? Give the teacher an apple.

cookie

0

Author

authors profile photo

Articles with similar tags

Comments