Home / Blog /
The fundamentals of C++
//

The fundamentals of C++

//
Tabnine Team /
6 minutes /
March 24, 2021

C++ has been around a long time — since the 70s to be more precise. C++ is especially popular in the game industry because it gives the developer high levels of control over performance, memory, and resource allocations.

The C++ language is part of the C-language family, which means that if you learn C++, there’s a high chance of transferring that knowledge over to niche programming projects that uses C.

This article will give you an introduction to the fundamentals of C++ to help you on your learning journey. If you already know a programming language, most of the concepts introduced here will be familiar. This is because C++ is a partial object-oriented language. So if you’re already familiar with Java and C#? C++ should be a breeze.

STM32F4 – Object-oriented Programming with Embedded Systems (C++ /w STL) |  Iztok's HW/SW Sandbox

Writing C++ Statements

A statement is the foundation and building block of all programming. It’s like a code-based sentence that has a specific instruction that causes the program to perform an action.

In C++, a statement often ends in a semicolon. Here is an example of a statement:

#include <iostream> ​ int main()
{
std::cout << "Meow! Meow!";
return 0;
}

Lines 5 and 6 are statements. If you miss out the ;, you will get a syntax error message. syntax can be viewed as the grammar rules for programming. If your C++ syntax doesn’t follow the established rules, it will be considered invalid.

main() function acts as an entry point for our application. Every C++ program will have one main() function. Programs work in a linear manner. It is a set of instructions executed in turn. If there are multiple main(), you will get a console error because the program doesn’t know which main() is the true starting point.

Commenting In C++

Commenting in C++ is the same as other major programming languages. // represents a single-line comment. This includes text that may wrap but is technically on the same line because it hasn’t been escaped.

For multi-lined comments are anything that sits in between the /* and */ pair.

For example:

std::cout << "Hello there!n" //std::count is part of the iostream library
​
std::cout << "Just printing another line.n"
​
/* This is a
multi-line
comment. */
​
/* For formatting purposes,
  * it is convention to have a space and * preceding
  * to give your multi-line text alignment
  */

Using multi-line comments inside another multi-line comment won’t work and will cause syntax errors because how the * and * are paired up.

The general reason behind using a comment is usually to describe what the library, function, or program does. The purpose of this is to act as a guide to new developers dealing with the code. It is a way to help document your code and make it easier to understand its purpose without needing to go diving into the actual code.

Writing a good comment should inform the developer what the code after it is supposed to represent. This means writing description rich comments.

For example, here is a bad comment:

// Set height range to 0
height = 0;

This doesn’t tell the developer much except what he can already see. Here is an example of a better description.

// The block's height is set to 0 because the game has just started.
height = 0;

In the above example, the comment is description rich and gives the developer context.

Variables, Assignments and Initialization

In C++, we can access saved variables in memory via an object. A named object is called a variable and the name of that variable is called an identifier. Declaring a variable for the first time (i.e. when you create the variable) is called writing a definition. Once the definition is written, it is called instantiation — that is the moment when the object is first created and assigned a memory space.

Here is an example of a variable definition being instantiated.

int x;

In the above example, the data type is int. There are 7 primary or primitive data types in C++. They are:

  • int integer
  • char character
  • bool boolean
  • float floating point
  • double double floating point
  • void valueless or void
  • wchar_t wide character

You can also declare multiple variables at once using ,. For example:

int x, y;

Is the same as writing the following:

int x;
int y;

The declared values need to have the same type, or else you’d end up with a syntax error.

To assign a value to a variable, you can do so using the = operator. Once you’ve already declared and initialized the value with a type, you don’t need to do it again.

Here is an example of a variable assignment:

int x;
x = 5;
​
int y = 7; //this will also work

Do note that = is an assignment while == is an equality operator used for testing values. This method of using = to assign value is called a copy initialization.

An alternative to this is to perform a direct initialization using a pair of parenthesis ().

int x(5); //example of direct initialization

To initialize multiple variables at once, you can follow the , pattern with assignments. For example:

int x = 5, y = 7;
int a(1), b(2);

Uninitialized variables will not be set as empty but will take on whatever garbage value that previously existed in the memory allocated spot. For example, if you print out an uninitialized value, you’ll get some strange return.

#include <iostream>
​
int main()
{
  int x;
  std::cout << x;
  return 0;
}

The output will be whatever used to exist where x is currently allocated. No value returned will be the same.

iostream: cout, cin, and endl

In the first example for this piece, we encountered #include <iostream>. This is the C++ way of importing libraries for you to access their preconfigured functionalities, settings, and methods. In this case, the iostream refers to the input/output library — or io library.

Inside the iostream library, there are several predefined variables that we can use. std::cout is something that you can use to send data to the console as a text output. cout, as strange as it initially looks, actually stands for “character output”.

Here is an example of std::cout in action:

#include <iostream>
​
int main()
{
    std::cout << "How now brown cow!";
    return 0;
}

<< is called an insertion operator and allows you to insert your value into the std::cout function.

std::cout can take several printed and calculated parameters, in addition to variables. For example:

#include <iostream>
​
int main()
{
    int x('hello');
    std::cout << x;
    return 0;
}

hello will be printed in the console as a result. You can have more than one thing printed on the same line. Just add another insertion operator << to create the link. For example:

#include <iostream>
​
int main()
{
    int x('hello');
    int y(' brown cow');
    std::cout << x << y;
    return 0;
}

This will result in the output hello brown cow.

It is good to note that if you have several std::cout, it will still print everything on the same line. std::endl acts as a line break for your console output. Here is how you can use it:

#include <iostream>
​
int main()
{
    int x('hello');
    int y(' brown cow');
    std::cout << x << y << std::endl;
    std::cout << x << y << std::endl;
    return 0;
}

Without std::endl, you’d end up with hello brown cowhello brown cow as a single line.

Lastly for this section, there is std::cin — which stands for character input. This lets your console become a little bit more interactive by allowing you to assign user input to a variable. For example:

#include <iostream>
​
int main()
{
  std::cout << "What's your name? ";
    int name{ };
    std::cin >> name;
    std::cout << "Hello, " << name << 'n';
    return 0;
}

Note the direction of >> for std::cin. This is because >> is an extraction operator and the input is stored in the variable referenced. n is also another way to create a line break in the console output.

Where To From Here?

That’s the initial basic syntax for C++. If you already know a programming language like Java, moving forward works on almost the same theoretical foundations. The only major difference that you might need to explore further is how advanced syntax is structured.

If you’re new to programming in general and have a foundational understanding? The next steps from here would be exploring how arrays, functions and files work, object scope, conversions, bit manipulation, and control flows.

Just a list of good programming memes