We are finally heading to the conclusion of this small preface. However, before starting to dig inside Bitcoin code we have to get used to three important concepts, those of variables, functions and classes.
Variables
Simply put, variables can be seen as containers that store data. The kind of data that a variable stores is defined by its type, ranging from integer numbers to the most complicated custom-made types. In C++ the type must be explicitly stated when declaring a variable.
Here is a simple example of an integer variable:
int var;
var = 5;
In the above code snippet, an int
variable var
is declared; once the variable is declared, a value can be assigned to it. The value that we assign to our variable must be coherent with its type; being 5
an integer number, the assignment operation is successful. The value can be assigned at the same time of the variable declaration:
int var = 5;
This code is exactly the same as the previous one.
There are many different types of variables, each with its own characteristics:
Kind of variables: there can be characters (i.e. letters), integer numbers, floating point numbers and so on.
Number of bits: specifies how many combination of 0s and 1s a variable can store, calculated as 2^bit (i.e. for a
char
we have 2^8 = 256 possible values).Signed/unsigned: specifies if you can have both positive and negative numbers.
char varChar = 'a' // 8 bit character
unsigned int varUnsignedInt = 1; // Positive 32 bit integer number
short varShort = 3748; // Signed 16 bit integer number
float varFloat = 0.56; // 32 bit floating point number
double varDouble = 0.7366722; // 64 bit floating point number
bool varBool = true; // Binary variable that can be true or false
Functions
A function is a block of code that only runs when it’s called. A function can take some data as parameters, which are called inputs, and can return a certain value, called output.
There are some pre-defined functions in C++. The most important of them is the main()
function, which is the entry point of every C++ program; there can only be one main in a project, not less and not more.
However, usually functions are declared by the user, with the main purpose to make the code reusable in different parts of the project: define the code once, and use it many times.
Here’s an example of a simple function, taking as input two int
values and as output another int
, which is the sum of the inputs:
int sumFunction(int value1, int value2){
int sum = value1 + value2;
return sum;
}
The keyword return
indicates that the function is done processing and that the output can be returned.
Once a function is declared, it is possible to call it from our code:
int main() {
// Declare variable 1
int var1 = 5;
// Declare variable 2
int var2 = 4;
// Call sumFunction
int result = sumFunction(var1, var2);
return 0;
}
Classes
Being C++ an Object Oriented Programming (OOP) language, it’s important to understand the concept of class. A class is basically an user-defined container that stores different variables, called attributes, and functions to interact with them, called methods.
A class can be thought of as a template for objects, which are the instantiations of a particular class. To define a class the keyword class
is used.
Here is an example:
class Car{
public:
Car();
~Car();
void accelerate();
void brake();
string brand;
string model;
private:
float speed;
}
The keywords public
and private
define who can access the variable; the former let’s the user access the variable directly from outside the class, the latter allows the user to access it only inside the class itself. To access and attibute or a member the .
operator is called.
Two special functions, the so-called constructor and destructor, let the user instantiate and destroy an object; when an object is declared the constructor is called, when it is destroied the destructor is.
Here’s an example of an object instantiation:
int main() {
Car bmw;
bmw.brand = "BMW";
bmw.accelerate();
bmw.break();
return 0;
}
Enough theory for now!
In the next article we will start diving into Bitcoin Core.
“Brace yourself, Bitcoin code is coming!”
See you soon guys!
Tuma