Introduction

C ++ is a programming language designed in 1979 by Bjarne Stroustrup . The intention of its creation was to extend to the C programming language mechanisms that allow the manipulation of objects . In that sense, from the point of view of object-oriented languages , C ++ is a hybrid language.

Later, generic programming facilities were added , which were added to the structured programming and object-oriented programming paradigms . This is why it is often said that C ++ is a multiparadigm programming language .

There is now a standard, called ISO C ++, that most modern compiler manufacturers have adhered to. There are also some interpreters, such as ROOT.

The name "C ++" was proposed by Rick Mascitti in 1983, when the language was used for the first time outside of a scientific laboratory. Earlier the name "C with classes" had been used. In C ++, the term "C ++" means "increment of C" and refers to C ++ being an extension of C.

C++ features
  • Its syntax is inherited from the C language.
  • Object-oriented program (OOP).
  • Allows grouping of instructions.
  • Very didactic language, with this language you can learn many other languages ​​with great ease.
  • It is portable and has a large number of compilers on different platforms and operating systems.
  • It allows the separation of a program into modules that support independent compilation.
  • It is a high level language.
Hello world

The following is an example Hello World program written in C ++


#include
using namespace std;
int main()
{
cout << "Hola mundo" << endl;
return 0;
}

Using the directive #includetells the compiler to find and interpret all the elements defined in the file that accompanies the directive (in this case, iostream). To avoid overwriting the elements already defined by giving them the same name, the namespaces or namespacethe singular in English were created. In this case there is a namespace std, which is where the definitions of all the functions and classes that make up the included library standard C ++. By including the statement using namespace stdwe are telling the compiler that we will use the namespace stdso we will not have to include it when we use elements from this namespace, such as objects coutandcin, which represent the standard output stream (typically the screen or a text window) and the standard input stream (typically the keyboard).

The definition of functions is the same as in C, except for the characteristic that if you are mainnot going to collect arguments, we do not have to put them, unlike C, where you had to put them explicitly, even if they were not going to be used. It remains only to comment that the symbol "<<" is known as an insertion operator, and roughly it is sending coutwhat we want to show on the screen to paint it, in this case the string "Hola mundo". The same operator "<<" can be used several times in the same statement, so that thanks to this feature we can concatenate the object endlat the end, the result of which will be to print a line return.

Data types

C ++ has the following fundamental types :

  • Characters: char (is also an integer), wchar_t
  • Stamped: short, int, long,long long
  • Floating point numbers: float, double,long double
  • Booleans: bool
  • Empty: void
Principles

Every C ++ program must have the main function main()(unless another entry point is specified at compile time, which is actually the function that has the main())

int main() {}

The main function of the main source code must have one of the following prototypes:

int main ()

int main (int argc, char ** argv)

Although it is not standard some implementations allow int main (int argc, char ** argv, char ** env) The first is the default form of a program that takes no parameters or arguments. The second form has two parameters: argc , a number that describes the number of arguments in the program (including the name of the program itself), and argv , a pointer to an array of pointers, of argc elements, where the element argv [i] represents the i- th argument delivered to the program. In the third case, the possibility of being able to access the execution environment variables is added in the same way as the program's arguments, but reflected on the env variable . The return type of main is an int integer value . At the end of the function main, the return value must be included (for example, return 0 ;, although the standard foresees only two possible return values: EXIT_SUCCESS and EXIT_FAILURE, defined in the cstdlib file ), or exit through the exit function . Alternatively it can be left blank, in which case the compiler is responsible for adding the appropriate output.

The concept of class

Objects in C ++ are abstracted through a class. According to the paradigm of object-oriented programming an object consists of:

  • Identity, which differentiates it from other objects (Name that will take the class to which said object belongs).
  • Member functions or methods.
  • Attributes or member variables.

An example of a class that we can take is the dog class. Each dog shares characteristics (attributes). Its number of legs, the color of its coat or its size are some of its attributes. The functions that make him bark, change his behavior ... those are the functions of the class.
This is another example of a class:


class Point
{
// by default, members are 'private' so they can only be modified from within the class itself.
private :
// Private member variable
int id ;
protected :
// Protected member variables
int x ;
int and ;
public :
// Constructor
Point ();
// Destroyer
~ Point ();
// Member functions or methods
int GetX ();
int GetY ();
};
Constructors

Constructors They are special methods that are automatically executed when creating an object of the class. Their declaration does not specify the type of data they return, and they have the same name as the class to which they belong. Like other methods, there can be multiple overloaded constructors, although there cannot be virtual constructors.
As a special feature when implementing a constructor, just after the declaration of the parameters, there is what is called "initializer list". Its objective is to call the constructors of the attributes that make up the object to be built.
It should be noted that it is not necessary to declare a constructor like a destructor, since the compiler can do it, although it is not the best way to program.
Taking the example of the Point Class, if we want that every time an object of this class is created the coordinates of the point are equal to zero we can add a constructor as shown below:


class Point
{
public :

float x ; // Coordinates of the
float point y ;

// Constructor
Point () : x ( 0 ), y ( 0 ) { // We initialize the variables "x" and "y"
}
};

// Main to demonstrate the operation of the class

# include // This allows us to use "cout"

using namespace std ;

int main () {
Point MyPoint ; // we create an element of the Point class called MyPoint

cout << "X coordinate:" << MyPoint . x << endl ; // we show the accumulated value in the variable x
cout << "Y coordinate:" << MyPoint . and << endl ; // we show the accumulated value in the variable and
getchar (); // we tell the program to wait for the input buffer (stop)
return 0 ;
}

Destroyers

Destructors are special member functions called automatically at program execution, and therefore do not have to be explicitly called by the programmer . Its main tasks are:

  • Release the computational resources that the object of that class has acquired at runtime upon expiration.
  • Remove the links that other resources or objects could have with it.

Destructors are invoked automatically when the flow of the program reaches the end of the scope in which the object is declared. The only case in which the destructor of an object must be explicitly invoked is when it was created using the new operator, that is, it lives in heap memory, and not in the program execution stack. Invocation of the destructor of an object that lives in heap is done through the delete or delete [] operator for arrays. Example:


int main () {
int * anInteger = new int ( 12 ); // we assign an integer in heap memory with the value 12
int * arrayOfIntegers = new int [ 25 ]; // we allocate memory for 25 integers (they are not initialized)
delete anInteger ; // we free the memory occupied by an Integer
delete [] arrayOfIntegers ; // we free the memory occupied by arrayDeEnteros
return 0 ;
}

If the delete and delete [] operators were not used in that case, the memory occupied by anInteger and arrayOfIntegers, respectively, would be meaninglessly occupied. When a portion of memory is occupied by a variable that is no longer used, and there is no way to access it, it is called a 'memory leak'. In large applications, if a lot of memory leaks occur, the program can end up taking up considerably more RAM than it should, which is not convenient at all. This is why heap memory management must be used consciously.
There are two types of destructors, they can be public or private, depending on whether they are declared:

  • If it is public, it is called from anywhere in the program to destroy the object.
  • If it is private, the destruction of the object by the user is not allowed.

The use of destructors is key in the concept of Acquiring Resources is Initializing .

Templates

Templates are the C ++ mechanism for implementing the generic programming paradigm . They allow a class or function to work with abstract data types, specifying later which ones you want to use. For example, it is possible to construct a generic vector that can contain any type of data structure. In this way it is possible to declare objects of the class of this vector that contain integers, floats, polygons, figures, personnel cards, etc.

The declaration of a template is done by prepending the declaration template to the declaration of the desired structure (class, structure or function).
For example:


template
T max(const T &x, const T &y) {
return (x > y) ? x : y; //si x > y, retorna x, sino retorna y
}
Reference

All the documentation in this page is taken from devdocs.io