Menu Share

Introduction to C++

by | Published on
category C / C++

Today a lot of people are learning programming and the bar for learning is usually set pretty low with most languages. This is not exactly the case with C++ which is getting more and more complicated with every revision. It is a pretty old language but one that is really good for system programming, game programming or – if we continue in that manner – any other performance critical case.

Table of Contents

The roots of programming

When teaching and learning programming a lot of people neglect to look at the roots. The case is that the use of programming came from science and scientists and to truly understand how it works and why one needs to understand subjects such as mathematics and sometimes physics. A lot of developers neglect this when working with high-level languages because the programming language makes it easy. But the nature of this blog is game programming and let me spoil something for you – games require math and physics anyway.

I learned math after I learned programming and actually taught myself mathematics through programming but for you – my readers I will start by explaining some of the programming concepts with math as this should be more familiar with you.

Function in mathematics

You would probably already be familiar with the function notation in mathematics. It doesn’t matter how complex the function is at this point so we will examine some of the most simple ones:

y=f(x)=x+xy = f(x) = x + x

Lets analyze this notation and see what it tells us:

  • We have y that equals a function of x – This means that there is some calculation that we always do to x that will produce y. This is usually replaced by some real value of x for example:

y=f(13) or y=f(42)y = f(13) \text{ or } y = f(42)

  • The function of x has some kind of an equation that when replaced with a real number would produce the result y. In our case this equation is x + x . If we take the numbers from the previous example in a case where we replace x with 42 then in that case y would become equal to 42 + 42 or eventually 84.
  • This function is not a constant as there is no single value for y. What the value of y is will be determined only when we replace the x with some value that we can do actual calculations with.

I also want to expand on that last statement. A constant function would always return the same value for y no matter how x changes. A function that is constant would easily be:

y=f(x)=x−xy = f(x) = x – x

Because in this case y will always be 0 no matter how x changes.

Types of values

One more dry lesson before the cool part of actually programming. In mathematics we rarely consider size of numbers but the computers can only operate with certain types and sizes. Everything in the computer is numbers because the only information the computer understands is 0 and 1 (also known as boolean state). Computers don’t exactly understand text – they have numbers for every corresponding letter and we humans have created tables that would translate those numbers to the actual letters. Only one 0 or 1 is too small of a size to define things so we usually work in packs of bits by 8 (also called bytes).

1B = 8b

1KB = 1024B

1MB = 1024KB

And so on…

This does in fact limit the computer as to what it can calculate. In programming we have types that define different sizes of data. C++ lowest pack of data to work with is 1B. There main types that the C++ programming language supports are:

  • Integer – plain number without a decimal point. Usually comes in the size of 4B.
  • Boolean – Only one bit is considered for the boolean but the size is 1B. This usually wastes 7 of the bits.
  • Character – Usually describes a single character from text. Size is 1B and the type can be used for small numbers instead of text.
  • Real number (Float/Double) – describes a real number. In the case of the float it is 4B in size but some those bits are used for the real part and some for the integer. Double is double the size as the name suggests meaning 8B.
  • Void – used to describe the no size values or nothingness.

There are more but these are enough to explain the most basic concepts.

Functions in C++

So to sum up in programming we would have the type limitation for numbers. As you will see shortly that means that we would have to declare what types are the values we are operating with. Also in programming we have three different cases: we can declare that there exists an f(x) function. We can also declare the formula or body of that f(x) function. Third we can actually use the f(x) function by providing x with an actual value.

Our program also starts from somewhere. The outside world will always seek and execute a function called main within our program. So lets now write the first example of C++ code using the mathematical principles:

int sum(int x);

void main() {
    sum(42);
}
 
int sum(int x) { 
    return x + x; 
}

This represents an example program in C++. Our main function that doesn’t return anything will be called from the outside world without any arguments or unknowns. Then our program will sum 42 by itself and the program will stop.

Writing code is strict. Computers cannot understand or infer our intention so we must be very clear in our wording and ordering of statements. A statement ends in a semi-column (;) much like English sentence ends with a dot (.) . If we examine the statements of the program we can see that the first statement says that a function exists that returns an integer (int) and takes an integer. Then our main function starts. The { and } define the start and end of the calculations that this main function will produce. Main function has only one statement that executes the function defined earlier. Keep in mind that this will not work if the above function is not also implemented so after the main we have again the sum function but this time it also has a body (defined by { & }) like main has. In the body we have two operators: “return” – which instructs the program to return whatever follows the return operator; and “+” which will sum up two numbers.

Output of a program

After we defined some terminology – a program wouldn’t be much without actually making something useful – that would be interacting with the system, showing a message on screen, playing a game, playing a sound or some other kind of output.

The easiest that one can get is output to the terminal. If you don’t know what a terminal is you can find out more in this post about the basics of the command line. In C++ code resides withing a file with extension of “.cpp”. This file will produce real executable translation units when compiled. On windows to start an application you would usually double-click a file with the extension of “.exe”. To produce the executable file the translation units need to be linked.

Translation units or .cpp files can share code by using simple text files usually with the extension of .h or .hpp and called header files. These files themselves are not compiled. They can be included in a .cpp file and what will happen is that the place where you include them will be replaced with the text inside the header file. You can include a header file more than once in a .cpp and in more than one .cpp files.

There are some common header files that exist for the C++ language that have the code that will allow you to write to the terminal for example. The most used one would be “iostream” (short for input and output stream). The syntax to include this file would be:

#include <iostream>

At this point you don’t need to worry about all the text that this file has. We will use only a single thing from it and that is “std::cout” (short for console output). To write something to the console you would use the operator << which would usually mean put whatever is on the right side into the left side. So to give a simple example it would look like:

std::cout << 84;

When the program gets to this part of the code it would output 84 in the terminal to be seen. I also want to mention here that by “When the program gets to this part of the code” I mean that the program will always execute top to bottom starting from the main function’s body. It will not execute anything outside the main body so you cannot write valid code just outside of a function.

A real example

You can check out the repl bellow and also click run. The black window bellow would display the message “Result: 84”. You can notice some things that we haven’t talked before – quotes would define a text value (that is actually a sequence of characters). You can also notice that we chain the << operators. And the last thing is the std::endl (short for end of line) – this comes from the “iostream” header and tells the console to start a new line (like pressing enter).

You can play around with this repl I would even encourage you to examine the syntax and test what can be done and what doesn’t work.

Conclusion

We discussed some important points of how the language works. You can explore more on other sites like Learn C++ in Y minutes or Learn C++. I also encourage you to read more on how the compiler and linker work in the C++ language.

Leave a comment

Your email address will not be published. Required fields are marked *