C++ Abstraction

Data abstraction means that only the key information is provided to the outside world, and the implementation details of its background are hidden, that is, only the necessary information is presented without presenting details.

Data abstraction is a programming (design) technique that relies on the separation of interfaces and implementations.

Let's give a real-life example, such as a TV, you can turn on and off, switch channels, adjust volume, add external components (such as speakers, VCRs, DVD players), but you don't know Its internal implementation details, that is, you don't know how it receives signals over the cable, how to convert the signal, and ultimately display it on the screen.

So, we can say that TV separates its internal implementation from the external interface, you don't need to know its internal implementation principle, you can directly through its external interface (such as power button, remote control, volume controller) Control the TV.

Now, let's get down to business. For C++ programming, C++ classes offer the possibility of data abstraction. They provide a large number of public methods for manipulating object data to the outside world, that is, the outside world is actually not aware of the internal implementation of the class.

For example, your program can call the sort() function without knowing the algorithm used to sort the data in the function. In fact, the underlying implementation of function sorting will vary depending on the version of the library. As long as the interface is unchanged, the function call can work as usual.

In C++, we use classes to define our own abstract data types (ADTs). You can use the cout object of class iostream to output data to standard output as follows:


#include <iostream> using namespace std; int main( ) { cout << "Hello C++" <<endl; return 0; }

Here, you don't need to understand how cout displays text on the user's screen. You only need to know the public interface, and the underlying implementation of cout can be freely changed.

Access Tag Enforcement Abstraction

In C++, we use access tags to define abstract interfaces for classes. A class can contain zero or more access tags:

  • All members of the program can be accessed by members defined using the public tag. A type of data abstract view is defined by its public members.
  • A member defined with a private tag cannot access the code that uses the class. The private part hides the implementation details from the code that uses the type.

The frequency of access tags is unlimited. Each access tag specifies the access level defined by the member immediately following it. The specified access level will remain in effect until the next access tag is encountered or the closing right parenthesis of the class body is encountered.

The benefits of data abstraction

Data abstraction has two important advantages:

  • The inside of the class is protected from object state corruption due to unintentional user-level errors.
  • Class implementations may change over time to respond to changing needs or to respond to error reports that require no changes to user-level code.

If you only define data members in the private part of the class, the author who wrote the class can change the data at will. If the implementation changes, you only need to check the code of the class to see what effect this change will have. If the data is public, any function that directly accesses the data members of the old representation may be affected.

Example of data abstraction

In a C++ program, any class with public and private members can be used as an instance of data abstraction. Please see the example below:


#include <iostream> using namespace std; class Adder{ public: // Constructor Adder(int i = 0) { total = i; } // External interface void addNum(int number) { total += number; } // External interface int getTotal() { return total; }; private: // Externally hidden data int total; }; int main( ) { Adder a; a.addNum(10); a.addNum(20); a.addNum(30); cout << "Total " << a.getTotal() <<endl; return 0; }

When the above code is compiled and executed, it produces the following results:

Total 60

The above class adds the numbers and returns the sum. Public members addNum and getTotal are external interfaces that users need to know to use classes. The private member total is something the user doesn't need to know, but it is necessary for the class to work properly.

Design Strategy

Abstract separates code into interfaces and implementations. So when designing a component, you must keep the interface independent of the implementation, so that if you change the underlying implementation, the interface will remain the same.

In this case, no matter what program uses the interface, the interface will not be affected, just recompile the latest implementation.