At the university, I had a place where I used to eat a fat Brazilian street hamburger while reading my precious book “C++ How to Program” third edition from Deitel & Deitel. I cannot tell what was heavier, the code in my mind, or the hamburger in my stomach. I was young, and at the time I believed my body could take it all, now I guess I was wrong about the hamburgers.

I read that book because, as I said in my first story, I believed C++ was the holy grail of the programming languages. I also had the hope of using this programming language in big projects. What did only happen a few years after I finished the book, in the Brazilian military, where I was part of the development team of their command and control system. Yeah, the system which could tell a person on the other side to move the tank a little bit to the north using a military radio and a drag-and-drop action with the mouse, like “Command & Conquer.” 😃

Talking about C++ is hard, maybe because there are people who believe C++ is C on steroids, remember the “fear”?

It shall rain fire upon those who are not worthy of C++ and its pointers to class instances.

Also, maybe because if you talk to two different C++ experts, you will notice: first, you don’t know much about C++; and second, you see that both of them completely different excellent programming styles on C++ and of course, they will not agree with each other.

Don’t Mix Mangoes And Milk?!

There is an old saying in Brazil that Mango and Milk together can cause stomach ache, my grandma used to say that. Long story short, this saying exists since slavery to create a sort of fear among slaves so they would not drink milk because they used to eat lots of mangoes, in the end, a well-done blend of mango and milk is very tasteful, but do not add the hamburger here just to be sure. And what about C++?

So, I would say C and C++ are like mangoes and milk. First of all, they are not the same. There is not such a programming language called C/C++ as there is not fruit or drink called “Milgo.” C is one programming language, and C++ is another. Still, like mango and milk, mixing them could cause a little adverse reaction to those “religious” developers, which I am not. But, in the end, a good mixture of C and C++ can work wonders.

C++, in my opinion, is a language as powerful and fast as C is. On top of that, it has the beautiful advantage of being Object-Oriented, which makes everything more readable and maintainable in my point of view. C++ was used to develop KDE, the V8 Javascript engine, and MySQL, for example.

Do The Evolution

C++ evolved a lot with all its standards, bringing more and more features to the developers. Back in the day, it was “just” an object-oriented programming language where I needed to pay attention and follow all my memory allocations and make sure to delete my instances. Powerful nevertheless, no questions asked here. But if someone would like to hide those costs of memory management, or use more high-level features such as lambda expression, for example, there was no way to go despite using some big libraries, such as Qt or Boost. Those helped the developers in many different directions but with the loss of performance.

Once upon a time, I had the feeling I was programming Java, the code was like Java and the performance as well…

Many features are being brought inside in C++ standards and lots of improvements as well. In my opinion, C++11 was the most significant release so far. Among lots of features, lambda functions became available, now from “just” an object-oriented programming language, suddenly some functional programming concepts were more comfortable to be implemented. Type inference is also something that came along and helped a lot.

C++14 and C++17 brought improvements in performance and also some new features in the standard libraries, which helps the code to be more readable and more maintainable. This year still, C++20 is coming, with some needed changes.

Long story short, C++ is evolving fast and not stuck in history, as many people tend to believe.

Seek and Destroy

Coding in C++ can confuse many newcomers and also some experienced developers who are not used to work with this programming language. I am not saying C++ is complicated, but rather that some learning is needed like all programming languages.

Usually, the first question-mark-face I see is when a newcomer sees a destructor or dtor. Yes, all classes have a constructor and a destructor. The destructor is the last breath an instance takes, and it is typically used to clean up the mess your class created. Free memory or reset states, for example. C++ does not have a garbage collector, that means, the destructor will be called precisely at the moment your instance’s lifecycle finds its end, be it by delete instance , when we are talking about dynamic allocation in the heap, or when the instance’s scope is gone when it is static allocation on the stack.

That leads me to the second point, dynamic and static allocations. Usually, when instances are allocated in other programming languages, they are always dynamically allocated in the heap, and you have a pointer, even if you don’t see it. Take Java, for example; every new instruction returns a reference to the newly created instance; in other words, a pointer. C++ is not different here; the new operator does precisely the same. Differently from Java, C++ can have instances being statically allocated. For example:

class A { 
~A() { ... }
};
int main() {
A aStatic;
A *aDynamic = new A();delete aDynamic; // Destructor call for aDynamic} // Destructor call for a

Here the first reaction is: What does A aStatic means? In my experience, most people who ask me this question tend to answer their question with:

This is just a variable declaration, or?

It is a variable declaration, but not just. It allocates memory and initializes an instance of A in the stack of your program, and you can use it without any explicit assignment. You should also notice when the destructors are being called, explicitly for aDynamic , and implicitly for aStatic at the end of its scope.

That Is My Mother, And That Is My Father

Last but not least, one of the most impacting features C++ has is multiple inheritances. Different from most programming languages that I know, C++ is one that allows a class to have more than one parent class. This feature enables us to implement many different Object-oriented-programming concepts, such as interfaces, even if C++ does not have it as a language concept as in Java.

It also can be tricky. A C++ class can have two parents, but, different from you and me, its parents could have the same parent class; in other words, a unique grandparent. What?! Does that mean that its father and its mother were born from the same person, its grandma? Yes, but we are just talking about C++ and not real life.

This setup is called the Diamond Problem. This problem happens when multiple paths through the inheritance tree lead to the same origin or base class. For the compiler, it is hard to understand if the base class is different or equal, even if it has the same name. The developer should tell the compiler such a thing, and it is done through virtual inheritance.

Closing

C++ is a beautiful language, fast and full of features. Despite most people believe, it is not a complicated programming language to learn nor to put into action. I enjoy it a lot when I can use it in a project, be it big, be it personal. I also enjoy the speed it is evolving and the cool tricks you can use to solve your algorithmic problems. The possibility to interface with C and its proximity to the machine puts C++ in the same usage area as C. Still, in my opinion, with a significant readability and maintainability improvement.

Quoting my colleague, Johannes Floeckner, talking about C:

… programming C was like writing a symphony… sometimes complex, sometimes troublesome … but once it was done, simple beautiful!

And now if I may hijack his quote, I would say:

… programming C++ is like writing a symphony with Metallica… sometimes complex, sometimes troublesome, lots of rock’n roll … but once it was done, it is breath taking!

Thanks

Thanks, Balram Chavan, for the review.

Thanks, Johannes Floeckner, for the quote.

Feel free to reach out to me at LinkedIn https://www.linkedin.com/in/hugo-wruck-schneider/

About the writer: Dr. Hugo Wruck Schneider

After quite some years of experience in the Industry and in the Academy, technology and programming languages became Hugo’s second tongue. Entrepreneurship was also part of this journey, among successful and also unsuccessful startups. Today, Hugo is entirely focused and driven by challenging software and data engineering problems. Starting with training complex machine learning models and making them production ready, going through the implementation of high available data engineering pipelines, designing and creating high performant algorithms in GPUs for Bioinformatics, and ending with exploring the applications of quantum computing in the bioinformatics field, Hugo is always up to the challenge. Do not hesitate to connect with Hugo on LinkedIn: https://www.linkedin.com/in/hugo-wruck-schneider/ 

Legg igjen en kommentar

Fyll inn i feltene under, eller klikk på et ikon for å logge inn:

WordPress.com-logo

Du kommenterer med bruk av din WordPress.com konto. Logg ut /  Endre )

Google-bilde

Du kommenterer med bruk av din Google konto. Logg ut /  Endre )

Twitter-bilde

Du kommenterer med bruk av din Twitter konto. Logg ut /  Endre )

Facebookbilde

Du kommenterer med bruk av din Facebook konto. Logg ut /  Endre )

Kobler til %s