C++ Data Abstraction
Data abstraction means providing only key information to the outside world and hiding the underlying implementation details—that is, presenting only necessary information without showing the details.
Data abstraction is a programming (design) technique that relies on the separation of interface and implementation.
Let's take a real-life example, such as a television. You can turn it on and off, switch channels, adjust the volume, and add external components (such as speakers, a VCR, or a DVD player), but you don't know its internal implementation details. In other words, you don't know how it receives signals through cables, how it converts those signals, and finally displays them on the screen.
Therefore, we can say that a television separates its internal implementation from its external interface. You do not need to know how it works internally; you can operate the TV directly through its external interfaces, such as the power button, remote control, and volume control.
Now, let's get back to the point. In terms of C++ programming, C++ classesData Abstractionmake this possible. They provide a large number of public methods to the outside world for manipulating object data, meaning that the outside world does not actually know the internal implementation of the class.
For example, your program can callsort()functions, without needing to know the algorithm used in the function to sort data. In fact, the underlying implementation of the function sorting may vary depending on the library version; as long as the interface remains unchanged, the function call can continue to work normally.
In C++, we useClassto define our own abstract data types (ADTs). You can use classesiostreamofcoutobjects to output data to standard output, as shown below:
Example
Here, you don't need to understandcouthow text is displayed on the user's screen. You only need to know the public interface; the underlying implementation of cout can be freely changed.
Access labels enforce abstraction
In C++, we use access labels to define the abstract interface of a class. A class can contain zero or more access labels:
- Members defined with the public label are accessible to all parts of the program. The data abstraction view of a type is defined by its public members.
- Members defined with the private label are not accessible to code that uses the class. The private section hides implementation details from the code that uses the type.
There is no limit on how often access labels may appear. Each access label specifies the access level of the member definitions immediately following it. The specified access level remains in effect until the next access label is encountered or until the closing right brace of the class body is reached.
Benefits of data abstraction
Data abstraction has two important advantages:
- The internal state of a class is protected from damage caused by unintentional user-level errors.
- The class implementation may change over time to respond to changing requirements, or to respond to bug reports that require no changes to user-level code.
If data members are defined only in the private section of a class, the author of the class can change the data at will. If the implementation changes, only the class code needs to be examined to see what impact the change causes. If the data is public, any function that directly accesses data members of the old representation may be affected.
Examples of data abstraction
In a C++ program, any class with public and private members can serve as an instance of data abstraction. See the following example:
Example
When the above code is compiled and executed, it produces the following results:
Total 60
The class above adds numbers and returns the sum. The public membersaddNumandgetTotalare the external interface; users need to know them in order to use the class. The private memberstotalare things users do not need to know, but which are necessary for the class to work correctly.
Design Strategy
Abstraction separates code into interface and implementation. Therefore, when designing components, the interface must be kept independent of the implementation, so that if the underlying implementation changes, the interface remains unchanged.
In this case, no matter what program uses the interface, the interface will not be affected; only the latest implementation needs to be recompiled.
other extensions