C++ is a programming language developed in 1983 by Bjarne Stroustrup, who wanted to "increment" the C language (hence the name, c++ meaning "add 1 to C"). This language brings new functionalities to C: object-oriented programming, classes, templates, etc., all of which will be explained in detail in this article.
It is one of the most widely used languages, particularly in software development (e.g. IPod interface, web browsers) or video game programming (e.g. PS3, PS4 operating systems), thanks to its performance and versatility.
Indeed, although C++ is difficult to master, it enables you to write optimized, extremely fast code, unlike other languages such as Python, which is easier to master but comes at a cost: lower efficiency and speed.
What are the principles of object-oriented programming?
Object-oriented programming (OOP) is based on a set of principles:
- The class (private or public: structure): a class is a set of data (integers, an array…) called attributes, and methods acting on this data, such as display, write, read or operators: addition, multiplication…
- Data encapsulation: the methods and data of a class are separated into two parts: a private part (reserved for the class developer) and a public part (accessible to all users). For example, the elements of an array can be read and modified, but not its size.
- This division avoids many bugs caused by programmer errors, and facilitates the programmer’s work: he doesn’t need to know how the class is constructed to be able to manipulate it.
- In C++, classes have specific methods: the constructor is used to initialize the class (idem in Python) and the destructor is used to release memory resources once the object created is no longer needed. The memory management provided by the destructor, which is not present in Python, is a source of many errors for beginners.
- Inheritance: a general class can be broken down into specific subclasses, allowing you to factorize your code and save on the number of lines, as in Python.
Procedural Programming
An essential distinction in C++ is that between functions that do not modify their inputs (they may eventually create and return a new object) and functions that modify inputs but return nothing. The latter are called procedures.
This language paradigm enables the establishment of routines and the factorization of code, making it easier to read and more concise.
Programming definition Generic
Generic programming is based on the following principle: the same operators or functions are used for different types (double, float, char, int…), thanks to the notion of templates. This makes the language more concise and easier to maintain.
Class and function templates are general code templates that allow you to create abstract classes or functions with generic types (typename) that you don’t need to fill in.
Why is this useful? It saves you having to write a function for each particular data type, as the compiler will automatically create them when the abstract function/class is called.
For example, you can create an abstract function maximum: templatemax(), then call it to find the maximum element of an array of integers, floats, vectors, etc., then functions max(int), max(float) and max(vector) will be produced.
Is C++ a Compiled Language?
C++ is a compiled language: the source code, closer to machine language, is directly transformed into an executable file by the compiler, unlike Python, which needs an interpreter to first translate the source code. This difference largely explains the speed of C++.
What are the limits of the language?
While C++ has many qualities – speed, conciseness and versatility – it also has its drawbacks. On the one hand, it requires us to manage memory “by hand” (via destructors in particular), which leads to numerous errors, unlike Python’s garbage collector, which allocates memory for us.
On the other hand, learning C++ is more complex, as it calls on fundamental computer science notions.
Conclusion
C++ is widely used, even in Data Science (without realizing it), as the development environment for AI models is implemented in C++, with Python serving as the link between the input script and this environment.
Did you like this article? Would you like a sneak preview of our articles? Join our Newsletter now.