C++Data Abstraction
Data abstraction refers to providing only critical information to the outside world and hiding the implementation details of its background, that is, presenting only the necessary information without presenting the details.
Data abstraction is a programming (design) technology that relies on interfaces and implements separation.
Let's give a real-life example, such as a TV set, where you can turn on and off, switch channels, adjust the volume, add external components (such as speakers, VCRs, DVD players), but you don't know its internal implementation details, which means that you don't know how it is receiving signals from a cable, How to convert the signal and finally display it on the screen.
Therefore, we can say that television has its internal implementation and external interface separated, you do not need to know its internal implementation principle, directly through its external interface (such as power button, remote control, sound volume controller) can control the TV.
Now, let's get down to it, for C + + programming, C + + classes provide the possibility for data abstraction . They provide the outside world with a lot of public methods for manipulating object data, which means that the internal implementation of the class is not really clear to the outside world.
For example, your program can call the sort () function without needing to know the algorithm used to sort the data in the function. In fact, the underlying implementation of a function sort differs depending on the version of the library, and as long as the interface is not changed, the function call can work as usual.
C++Data Encapsulation
All C + + programs have the following two basic elements:
- program Statements (code): This is the part of the program that executes the action, which is called a function.
- Program Data: The data is the information of the program, it is affected by the program function.
Encapsulation is the concept of binding data and manipulating data in object-oriented programming, so as to avoid being disturbed and misused by the outside world, thus ensuring security. Data encapsulation introduces another important OOP concept, namely data hiding .
Data Encapsulation is a mechanism by which data and operational data are bundled together, and data abstraction is a mechanism that exposes only the interface to the user and hides the specific implementation details.
C + + supports encapsulation and data hiding (public, protected, private) by creating classes . As we already know, the class contains private members, protected members (protected), and members of public members. By default, all items defined in a class are private.
In a C + + program, any class with public and private members can be used as an instance of data encapsulation and data abstraction.
C + + data encapsulation and abstraction