How does object-oriented programming differ from procedural programming in C++, and how is it implemented?

In this blog post, we will examine how object-oriented programming differs from procedural programming and explore how this concept is implemented in C++.

 

In the early days of programming, after computers were developed, procedural programming was the dominant concept. Procedural programming involves executing code in a specific order as input by the user. As code became more complex and difficult to understand over time, the concept of object-oriented programming emerged. Unlike procedural programming, object-oriented programming involves creating multiple small units called objects and combining them to execute a program. C++ was created by incorporating the object-oriented programming concept into the existing procedural language C.
There are various other object-oriented languages, but the way they implement the object-oriented concept differs. C++ implements object-oriented programming through the concept of “classes.” In C++, when using variables, you must explicitly tell the computer what type the variable is. For example, when using a variable named “a,” you must clearly specify whether “a” is an integer or a floating-point number. A class is not a basic type like an integer or a floating-point number, but rather a type defined by the programmer. Within a user-defined class, you can create various variables and functions. For example, if you create a class named ‘animal,’ you can define a floating-point variable ‘weight’ to represent weight and a function to make a sound. A class merely provides the structure, and actual use is possible by creating a variable of the ‘animal’ class type. This concept is often compared to a bread mold and bread. You create a bread mold called a class and use it to make variables like bread. Variables created using a class are called “objects.”
The main components of object-oriented concepts are inheritance, encapsulation, and polymorphism. C++ implements these elements in classes. Inheritance is the concept of one class inheriting the functions of another class. For example, suppose there are classes called “animal,” “lion,” and “rabbit.” Since lions and rabbits are animals, a well-designed program would have the “lion” and ‘rabbit’ classes inherit most of the functions of the “animal” class. In this case, instead of rewriting the same code, you can simply specify that ‘lion’ and ‘rabbit’ inherit from the ‘animal’ class, making the code more concise. Here, the ‘animal’ class that provides inheritance is called the parent class, and the ‘lion’ and ‘rabbit’ classes that inherit are called child classes.
Encapsulation is the control of access to a class’s internal information. If there is important data inside a class, it can be dangerous to allow indiscriminate access to it. However, it is not possible to make the entire class private in order to protect specific data. Therefore, we use a few keywords to distinguish between information that should be made public and information that should be hidden.
Polymorphism means that the same name can behave differently depending on the situation. In C++, when defining a function, you must specify the function name, return type, and the type and number of variables that will be passed to the function. For example, when creating a function to add two numbers, a function that adds integers and a function that adds floating-point numbers perform different operations, but if you must give them different names even though they have similar functionality, remembering and using them becomes very cumbersome. However, in C++, if the return type, input variable types, or number of variables differ, functions with the same name are recognized as distinct functions. Additionally, child classes do not need to perform all the functions of their parent class. For example, if there is a function that produces an animal’s sound, since each animal makes a different sound, the function can be overridden (redefined) in the child class.
C++ is a language created by adding additional syntax to C, so it can use most of C’s features. Therefore, pointers, one of C’s main features, are also used in C++. In a computer’s memory, each storage area has a unique address. A pointer points to this address. When a function needs to receive a variable to execute, the computer copies the variable to a new space and executes the function using that variable. When the function execution is complete, the copied variable disappears. For example, when executing the ‘swap’ function that exchanges the values of two variables, the values of the copied variables change, but the original variables remain unchanged. However, if the ‘swap’ function receives a pointer to a variable and changes its value, the pointer points to the original address, so the actual value of the variable is changed. C++ also provides a reference feature that allows you to pass the variable itself instead of a pointer, which can be useful in certain situations.
Another key feature is support for generic programming. Generic programming is a concept designed to enhance code reusability and programmer convenience. The primary feature in C++ that enables this is templates. As mentioned earlier, while it is possible to create multiple functions with the same name, creating multiple simple addition functions would be inefficient. With templates, the computer automatically determines the type of the function’s argument and performs the appropriate action, regardless of whether the type is an integer or a floating-point number. This reduces code writing time, simplifies the code, and improves readability.
Another element of generic programming is the Standard Template Library (STL). Data utilization is very important in programming. Algorithms for handling data are mathematically organized, and data structures are implementations of these algorithms. However, creating data structures from scratch every time is not only a waste of time but also does not guarantee that the algorithm is optimized. C++ provides data structures optimized by experts at the language level, which is the STL. Programmers can simply select and use the data structures they need.
As such, C++ provides many features at the language level for user convenience. Since it was developed as an extension of C, the language itself has some complex aspects. While learning and understanding all these concepts may be challenging, C++ remains widely used due to its powerful performance. The standard syntax of C++ continues to evolve over time, and various features will likely be added in the future to further enhance programmer convenience.

 

About the author

Writer

I'm a "Cat Detective" I help reunite lost cats with their families.
I recharge over a cup of café latte, enjoy walking and traveling, and expand my thoughts through writing. By observing the world closely and following my intellectual curiosity as a blog writer, I hope my words can offer help and comfort to others.