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

H

Object-oriented programming emerged to overcome the limitations of procedural programming and uses the concepts of inheritance, encapsulation, and polymorphism to make code more reusable and secure. C++ implements these concepts through classes, templates, and more, and is both powerful and complex.

 

In the early days of programming after the development of computers, procedural programming was the dominant concept. Procedural programming is the idea of executing user-input code in a set order. Over time, as code became more complex and difficult to understand, the concept of object-oriented programming emerged. Unlike procedural programming, object-oriented programming involves creating multiple small objects and combining them to execute a program. C++ was created by combining the concept of object-oriented programming with C, an existing procedural language.
There are many other object-oriented languages, but they all implement the object-oriented concept in different ways. C++ implements object orientation through the concept of classes. In C++, when you use a variable, you must tell the computer what type it is. For example, if you use the variable ‘a’, you must tell it whether ‘a’ is an integer or a real number. A class is a new type defined by the programmer, rather than a default type like integer or real. Inside a user-defined class, you can create a variety of variables and functions. For example, if you create a class named ‘animal’, you can create a real variable ‘weight’ inside it that represents weight, and you can also define a function that makes a cry. The class simply provides the structure; the actual use is accomplished by creating variables of the ‘animal’ class type. A common analogy for this concept is a bread mold and bread. You create a bread mold called a class and use it to bake variables like bread. Variables created using classes are called “objects”.
The main components of the object-oriented concept are inheritance, encapsulation, and polymorphism. C++ implements these elements in classes. Inheritance is the idea that one class inherits functionality from another class. For example, suppose we have a class ‘animal’ and classes ‘lion’ and ‘rabbit’. Since lions and rabbits are animals, in a well-designed program, the ‘lion’ and ‘rabbit’ classes would inherit most of the functionality of the ‘animal’ class. Instead of retyping the same code, we can simplify the code by simply stating that ‘lion’ and ‘rabbit’ inherit from class ‘animal’. The ‘animal’ class that does the inheritance is called the parent class, and the ‘lion’ and ‘rabbit’ classes that inherit from it are called the child classes.
Encapsulation is about controlling access to information inside a class. If you have sensitive data inside a class, it’s dangerous if it can be accessed indiscriminately. However, you can’t make an entire class private to protect specific data. Therefore, we use a few keywords to distinguish between what information is public and what is hidden.
Polymorphism means that the same name can behave differently in different contexts. In C++, when you define a function, you must specify the name of the function, the return type, and the type and number of variables that go into the function. For example, when creating a function to add two numbers, it would be very cumbersome to remember and use if you had to name the functions differently, even though they perform different behaviors, such as adding integers and adding real numbers. However, C++ recognizes different functions with the same name if the return type of the function, the type of variables that are input, or the number of variables are different. Also, a child class does not have to perform all of the same functions as its parent class. For example, if you have a function that makes an animal cry, you can override that function in a child class because different animals have different cries.
Because C++ is a language that adds additional syntax to C, it can use most of the features of C. Therefore, one of the main features of C, pointers, is also used in C++. In a computer’s memory, each storage area has a unique address. Pointers are responsible for pointing to this address. When a function receives a variable to execute, the computer copies the variable, stores it in a new space, and uses it to execute the function. When the function finishes running, the copied variable disappears. For example, when you run a ‘swap’ function that swaps the values of two variables, the value of the copied variable changes, but the original variable remains the same. However, if the ‘swap’ function takes a pointer to a variable and swaps its value, the pointer points to a unique address, so the value of the variable actually changes. C++ also provides a reference function that allows you to pass the variable itself instead of a pointer, which can be useful.
Another feature is that it supports generic programming. Generic programming is a concept that aims to make code more reusable and easier for programmers. The main feature of C++ that makes this possible is Templates. As mentioned earlier, it is possible to create multiple functions with the same name, but it is wasteful to create multiple simple addition functions. With templates, it doesn’t matter whether the type of the function is an integer or a real number, the computer will determine the type and perform the behavior. This reduces the time it takes to write code and makes it more concise and readable.
Another element of generic programming is the Standard Template Library (STL). The use of data is very important in programming. Algorithms for dealing with data are mathematically organized and implemented in structures, but it’s a waste of time for programmers to reinvent the wheel every time, and there’s no guarantee that the algorithms are optimized. In C++, expertly optimized data structures are provided at the language level, called STLs. As a programmer, you can choose the one you need and use it.
As you can see, C++ provides a lot of features at the language level for user convenience, and the language itself is complex because it was created as an extension of C. It can be difficult to learn and understand all of these concepts, but C++ is still used by many programmers because of its power. The standard of C++ syntax continues to change over time, and more features will be added to make life easier for programmers in the future.

 

About the author

Blogger

Hello! Welcome to Polyglottist. This blog is for anyone who loves Korean culture, whether it's K-pop, Korean movies, dramas, travel, or anything else. Let's explore and enjoy Korean culture together!

About the blog owner

Hello! Welcome to Polyglottist. This blog is for anyone who loves Korean culture, whether it’s K-pop, Korean movies, dramas, travel, or anything else. Let’s explore and enjoy Korean culture together!