Abstraction in C++ Programming Language
One of the most fundamental and significant components of C++ object-oriented programming is data abstraction. Abstraction refers to hiding the intricacies and presenting simply the most important facts. Data abstraction is the process of exposing to the outside world only the information that is absolutely necessary while concealing implementation or background information.
Take a man operating a vehicle in the actual world as an example. The man only knows that pressing the accelerator will make the car go faster and that applying the brakes will make the car stop, but he has no idea how pressing the accelerator actually increases the speed of the car. He also has no idea how the accelerator, brakes, etc. are used in the vehicle. Abstraction is exactly what it is.
Abstraction in C++ |
Various Abstraction Types:
⦁ Data abstraction: This type hides the extraneous data and only displays the relevant information about the data.
⦁ Control Abstraction: This type hides extraneous information and only displays the information about the implementation that is necessary.
Utilizing Classes for Abstraction
Classes can be used to implement abstraction in C++. Using the available access specifiers, the class enables us to group data members and member methods. Which data member is visible to the public and which is not can be determined by a class.
Header file abstraction
Header files are yet another sort of abstraction in C++. Take the pow() method from the math.h header file as an illustration. Without understanding the underlying algorithm used by the function to compute the power of numbers, we simply call the function pow() found in the math.h header file whenever we need to determine the power of a number.
Utilizing Access Specifiers for Abstraction
The cornerstone of C++'s implementation of abstraction is access specifiers. Access specifiers can be used to impose limitations on class members. For instance:
Anywhere in the program can access members of a class that have been designated as public.
Only from within the class may one access members that have been designated as private. They can't be accessible from any other parts of the code than the class.
The two qualities mentioned above that access specifiers offer allow us to construct abstraction with ease. Let's say that a class has the ability to mark the elements that specify the internal implementation as private. Additionally, critical information that must be disclosed to the public can be tagged as such. And these public members can access the private members as they are inside the class.
Benefits of Abstracting Data
⦁ Aids in preventing the user from writing low-level code
⦁ Reduces code duplication and improves reuse.
⦁ It can independently alter the class's internal Implementation without affecting users.
⦁ Increases the security of a software or application by only giving users access to crucial information.
⦁ It improves the readability of the code by reducing its complexity and redundancy.
Encapsulation in C++ Programming Language
The wrapping up of data and information into a single unit is referred to as encapsulation in the C++ language. Encapsulation is the bringing together of the data and the functions that manipulate them in object-oriented programming.
Consider a practical illustration of encapsulation: at a company, there are various divisions, such as the accounts division, the finance division, the sales division, etc. Now,
The finance department manages all financial transactions and maintains records of all financial data.
In a similar vein, the sales section manages all actions linked to sales and keeps track of all sales.
Now a scenario could occur when a finance official requires all the sales information for a specific month for some reason.
In this instance, he is not permitted to view the sales section's data directly. To request specific information, he must first speak with another officer in the sales department.
Encapsulation is what it is. Here, the term "sales section" refers to both the data of the sales section and the people who can change them.
Encapsulation in C++ |
Two key characteristics of Encapsulation
Data Protection: By keeping the data members of an object private, encapsulation safeguards the internal state of the object. These data members can only be accessed and modified using the public methods of the class, ensuring safe and regulated data manipulation.
Information concealment: Encapsulation shields from outside code the internal implementation specifics of a class. The class's usage is made more straightforward and abstract by restricting access to its public interface; this also enables internal implementation changes to be made independently of how they affect external code.
Implications of Encapsulation
The Implications of encapsulation are listed below:
⦁ No function from the class can be accessed directly. To access the function that uses the class member variables, we require an object.
⦁ Encapsulation refers to a method that we create inside a class that uses only member variables.
⦁ Encapsulation is not what we name it if there isn't a function created inside the class that uses a member variable.
⦁ An improvement in data security
⦁ It makes it easier to manage how our data members are modified.
Data abstraction is a byproduct of encapsulation. Encapsulation can also be used to hide data, as in the example given above, where information from areas like sales, finance, or accounting is kept secret from other sections.
Access specifiers' function in encapsulation
Access specifiers make Data Hiding easier in C++ programs by limiting access to the functions and data members of the class. In C++, there are three different kinds of access specifiers:
Protected Private Public
The compiler makes all of a class's data members and member functions private by default.
Things to Think About
As demonstrated in the aforementioned example, access specifiers are crucial in the implementation of encapsulation in C++. Encapsulation implementation can be broken down into two steps:
⦁ Putting all the information and methods into a class as a single entity.
⦁ Utilizing access specifiers to conceal pertinent information.
Difference between Abstraction and Encapsulation:
⦁ The method or process of obtaining the information is called abstraction. While encapsulation is the procedure or approach used to keep the information contained.
⦁ In abstraction, issues are resolved at the level of the design or interface. Problems are resolved at the implementation level when encapsulation is used.
⦁ The technique of masking the undesired information is an abstraction. Encapsulation, on the other hand, is a technique for securing data from the outside world while also concealing it within a single entity or unit.
⦁ Abstract classes and interfaces are tools we can use to build abstraction. Encapsulation, on the other hand, can be implemented using an access modifier, such as private, protected, or public.
⦁ Implementation problems are masked in abstraction by employing abstract classes and interfaces. While the data is concealed when in encapsulation utilizing getter and setter methods.
0 Comments