In this blog post, we’ll examine how object-oriented programming differs from procedural programming and explore how this concept is implemented in C++.
In the early days of programming, shortly after computers were developed, procedural programming was the mainstream concept. Procedural programming is the concept of executing user-input code in a predetermined sequence. As time passed and code became increasingly complex and difficult to understand, 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 born by integrating this object-oriented programming concept into the existing procedural language C.
While various other object-oriented languages exist besides C++, the way they implement object-oriented concepts differs. C++ implements object-oriented programming through the concept of ‘Class’. In C++, when using a variable, you must explicitly tell the computer what type that variable is. For example, when using a variable named ‘a’, you must clearly specify whether ‘a’ is an integer, a floating-point number, etc. A class is not a basic type like an integer or a floating-point number; it is a type newly 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’ inside it to represent weight, and you can also define a function to make sounds. A class merely provides the structure; actual usage is possible by creating variables of the ‘animal’ class type. This concept is often likened to a bread mold and bread. You create a bread mold called a class, and then use this mold to shape variables like bread. Variables created using a class are called ‘objects’.
The main components of object-oriented concepts are broadly divided into inheritance, encapsulation, and polymorphism. C++ implements these elements within classes. Inheritance is the concept where one class inherits functionality from another class. For example, suppose there are an ‘animal’ class and ‘lion’ and ‘rabbit’ classes. Since lions and rabbits are animals, a well-designed program would have the ‘lion’ and ‘rabbit’ classes inherit most of the functionality from the ‘animal’ class. This allows for concise code by simply specifying that ‘lion’ and ‘rabbit’ inherit from the ‘animal’ class, without rewriting the same code. Here, the ‘animal’ class providing inheritance is called the parent class, while the ‘lion’ and ‘rabbit’ classes receiving inheritance are called child classes.
Encapsulation controls access to a class’s internal information. If important data resides within a class, allowing indiscriminate access to it is risky. However, you cannot make the entire class private just to protect specific data. Therefore, keywords are used to distinguish which information is exposed and which is hidden.
Polymorphism means that the same name can behave differently depending on the context. In C++, when defining a function, you must explicitly specify the function name, return type, and the types and number of variables passed to the function. For example, when creating a function to add two numbers, a function that adds integers and one that adds floating-point numbers perform different actions. Yet, even though they are similar functions, if you must give them different names, 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. Furthermore, a child class does not need to replicate all the functionality of its parent class. For example, if there is a function that makes an animal sound, since each animal makes a different sound, the child class can override (redefine) this function to use its own version.
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 key features, are also used directly in C++. Each storage area in computer memory has a unique address. A pointer serves to point to this address. When a function needs to receive a variable to execute, the computer copies this variable, stores it in a new space, and then executes the function using that variable. Once the function execution finishes, the copied variable disappears. For example, when executing a ‘swap’ function that exchanges the values of two variables, the copied variable’s value changes, but the original variable remains unchanged. However, if the ‘swap’ function receives a pointer to the variable and changes its value, the pointer points to the unique address, so the variable’s value is actually changed. C++ also provides the reference feature, which passes the variable itself instead of a pointer, making it useful.
Another key feature is support for generic programming. Generic programming is a concept designed to enhance code reusability and programmer convenience. C++’s primary feature for achieving this is templates. As mentioned earlier, while it’s possible to create multiple functions with the same name, creating multiple simple addition functions is wasteful. Using templates, the computer automatically determines the type (whether integer or floating-point) that the function receives and executes the operation accordingly. This reduces coding time, makes the code more concise, and improves readability.
Another element of generic programming is the STL (Standard Template Library). Data utilization is crucial in programming. Algorithms for handling data are mathematically organized, and data structures are their implementations. However, programmers recreating data structures from scratch each time is not only time-consuming but also offers no guarantee that the algorithm is optimized. C++ provides expert-optimized data structures at the language level, which is the STL. Programmers simply select and use the required data structure.
In this way, C++ provides many features at the language level for user convenience. Because it was created as an extension of C, the language itself has complex aspects. Learning and understanding all these concepts can be challenging, but thanks to C++’s powerful capabilities, many programmers still use it. The standard for C++ syntax continues to evolve over time, and various features will continue to be added in the future for the convenience of programmers.