I thought it’s a good idea went back to the basics of C++ and demonstrate some basics concepts of this programming language.

What’s C++?

C++ is a general purpose multi paradigm programming language. Also, C++ can be referred as a compiled language, this means that a typical C++ program is composed by a bunch of source files. Those files have to be processed by a compiler, producing objects file, which are combined by a linker yielding an executable program. The next figure demonstrates the process to create a executable file by C++ [1].

There two kind of entities in C++:

  1. Core language features, such as built in types, loops, conditional statements, basics keywords.
  2. Standard Library Components, such as STL containers, algorithms and I/O operations, basically everything you add using #include.

Practical Introduction.

For instance, the next snippet shows how to declare a function, use the standard output stream (cout) and the basic adoption of the main function- which triggers the executable program.

#include <iostream>

using namespace std;

// return functionName(args){ //Function Body }

//Create a square function
int sqrt(int num){
 return num*num;

//Another Function
void printSqrt(int num){
 //cout : Print at console.
 cout<<"The square of: "<<num<<" is"<<sqrt(num)<<endl;

int main(){
 //Call local function
 //Nonzero return from main means a error.
 return 0;

Type, Variables & Arithmetic

Some of the C++ fundamental types are: bool(true or false),char (character, for example, ‘a’,’f’, and ‘&’) ,int (1,5 or 1245), and double (floating point number, for example 3.1415). The size of the type vary among different machines and can be obtained by the sizeofoperator.

For example, the typical size of a char is 8-bits, and the size of the others variables are quoted in multiples of the size of a char; if a char is 8-bits, an int is 32-bits, and a doubleshall be 64-bits. The following image illustrate this concept [1]:

Universal Initializer

With the apear of C++11, the syntax choices for object initialization grow, but braced initialization is the most recommended usable initialization syntax. This type of initialization prevents narrowing conversions, and inadequate calls. The next snippet shows what I mean with this last point:

class Widget{
    void operator()(int i){}

int main(){
    int a{1};//Ok
    int b(2);//Ok
    Widget w;//Build in type
    w(3); //Error, calling operator() instead
    return 0;

Points and Arrays

Pointers are the nightmare of inexpert C++ developers, because it’s easy to screw up everything with them. In my C++ programs, a huge percentage of errors are related with pointers . For example, check the following code, which explains a few operations, and at the end it accesses prohibited memory:

int main(){
    int v[4]={1,2,3,4}; //Array
    int* p=v; //p->address of v

    //Loop array
    for(auto i=0;i<4;++i){
        cout<<"Value of v["<<i<<"]"<<
    cout<<"Address of p: "<<&p<<endl;
    cout<<"Value of p: "<<p<<endl;
    cout<<"Contents of p: "<<*p<<endl;

    //Let's treat a pointer as array
    cout<<"Contents of p[3]: "<<*(p+3)<<endl;
    //Access violation
    cout<<"Contents of p[255]: "<<*(p+255)<<endl;

It’s important to notice that the prefix unary * means “contents of”, and the prefix unary &means “address of”. Moreover, when we need to represent the notion of “no object available”, we should give the pointer the value nullptr.


[1] Stroustrup, B. (2000). The C++ programming language. Boston: Addison-Wesley.

Leave a Reply

Your email address will not be published.