I am writing this blog for me to come back at this and re-convince myself to continue learning C++.

C++ is a general-purpose programming language, invented as an extension of C by Bjarne Stroustrup in 1985. Since its birth, C++ has been utilized in various industries for developing desktop applications, video games, servers, and more. Its popularity seems to be steadily increasing, as it ranks 2nd in popularity on the TIOBE index, surpassing C. But why is it so popular, and why should we learn C++?
Object-Oriented Programming
C++ introduced the object-oriented programming (OOP) paradigm to C, which is a significant reason
for its popularity. In C, we could store relevant variables in a struct
but not the associated
functions. Recall how we define a linked list and its functions in C:
typedef struct {
int data;
struct Node *next;
} Node;
Node *head = NULL;
void printLinkedList (Node *head) {
Node *current = head;
while (current != NULL) {
printf("%d -> ", current -> data);
current = current -> next;
}
printf("\n");
return;
}
In C, we needed to define functions separately from the struct
, leading to function names like
printLinkedList
instead of just print
, to avoid confusion with printing other data types like
strings and arrays. On the other hand, in C++, with OOP, we can store relevant variables and
functions in an object, like LinkedList
, and call them as LinkedList.head
or LinkedList.print()
.
This OOP approach is a more intuitive way of organizing data than procedural programming, as it
better mirrors how the real world works. For example, if we want to write code for a dog to bark
at a person (differently for the homeowner and others), it is much more intuitive and less
redundant to express it as Dog.bark(person)
than void Dog_bark(dog, person)
. The former
reads like "the dog barked at a person," while the latter feels more like "There was a dog
and a person, and the dog's barking was heard," if that makes sense.
The former is more natural because the structure of the code clearly communicates that the dog is the one taking action on the person, just like a natural language does. The latter code fails to convey this as effectively and relies solely on the process's name to communicate that information. This is why OOP code can be easier to write and read.*
Moreover, functions in an object can access the variables and functions stored internally within the object, reducing the need for external parameters. This encapsulation helps hide the details of variable initialization and function implementation, facilitating abstraction and faster development.*
Criticism
There are numerous other advantages to OOP, such as inheritance and composition, and C++ continuously evolves with useful features added every three years. You can access all of these features while still having control over memory management, just like in C, for writing efficient code. It sounds like a dream language, doesn't it? However, despite the praise, C++ has also faced significant criticism.
The more features that are added, the more ways there are to achieve the same outcome. Unfortunately, this also means there are exponentially more ways to make mistakes. For example, with C++, you can abstract away dynamic memory allocation within an object, but this increases the risk of forgetting to free memory, leading to memory leaks. When you have many abstract features, it's tempting to use the ones you know can do the job without considering their underlying implementation, potentially resulting in inefficient and bug-prone code.
Additionally, with so many features and the constant arrival of new ones every three years, keeping up with the changes and maintaining C++ code can be a struggle. C, on the other hand, is leaner and can still implement virtually everything that C++ can, even object-oriented-like code through modularization and careful function naming. In fact, Linus Torvalds, the creator of Linux, has famously been critical of C++. He once called C++ "crap" for some of the reasons mentioned above (at least in the context of writing kernel programs). I recommend reading his original source for more context.
Some might argue that writing in C++ is faster due to its pre-built features, but C++ is infamous for its cryptic syntax, which can be hard to read and write, as well as the boilerplate required for defining an object. If development speed is your top priority, a higher-level language with a garbage collector, like Python, might be a better choice.
Why Learn C++?
C++ isn't perfect—no programming language is. While it's true that C++ isn't suitable for all problems, there are some challenges that C++ is uniquely well-suited to address, such as video games, which inherently involve many objects and their interactions and require highly efficient code.
Additionally, many other higher-level languages (Python, JavaScript, etc.) are object-oriented, so knowing C++ provides a strong foundation for learning them. It's also beneficial for implementing some data structures and algorithms that naturally align with object-oriented principles.
I might not recommend C++ as a starting language for beginners, as it is generally harder to write and read. However, since we've already covered the basics of C, learning C++, an extension of C, is a logical and beneficial next step. I'm excited to start learning C++ and to share my journey with you all.
Resources
- Dream of Code. 2023. Should you learn C++ in 2023?. YouTube.