Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The data abstraction of C+


May 11, 2021 C++


Table of contents


The data abstraction of C+

Data abstraction refers to providing only critical information to the outside world and hiding the implementation details behind the scenes, i.e. showing only the necessary information without presenting the details.

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

Let's take a real-life example, like a TV, where you can turn a TV on and off, switch channels, adjust volume, add external components (such as speakers, video recorders, DVD players), but you don't know the details of its internal implementation, that is, you don't know how it receives signals over cables, how to convert signals, and ultimately display them on the screen.

Therefore, we can say that the TV splits its internal implementation from the external interface, you do not need to know its internal implementation principle, directly through its external interface (such as power button, remote control, sound controller) can control the TV.

Now, let's put it all in the right way, and in terms of C++ programming, the C++ class provides the possibility for data abstraction. They provide the outside world with a large number of common methods for working with object data, that is, the outside world is not actually aware of the internal implementation of the class.

For example, your program can call a sort() function without knowing the algorithm used to sort the data in the function. In fact, the underlying implementation of function sorting varies depending on the version of the library, and function calls work as long as the interface remains the same.

In C+, we use the class to define our own abstract data type (ADT). You can use the cout object of class ostream to output data to the 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. All you need to know is that the underlying implementation of cout is free to change.

Access labels enforce abstraction

In C+, we use access labels to define the abstract interface of the class. A class can contain zero or more access labels:

  • Members defined using public labels have access to all parts of the program. A type of data abstraction view is defined by its public members.
  • Members defined using private labels cannot access code that uses classes. The private section hides implementation details about the type of code used.

There is no limit to how often access labels can appear. E ach access label specifies the level of access defined by the member immediately following it. The specified access level remains valid until the next access label is encountered or the closing 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 inadvertent user-level errors that damage the object's state.
  • Class implementations can change over time to respond to changing requirements, or to error reports that require no change in user-level code.

If you define a data member only in the private part of the class, the author of the class is free to change the data. I f the implementation changes, you only need to examine 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 form of expression may be affected.

An instance of data abstraction

Any class with public and private members can be used as an instance of data abstraction in a C++ program. Take a look at the following example:

#include <iostream>
using namespace std;

class Adder{
   public:
      // 构造函数
      Adder(int i = 0)
      {
        total = i;
      }
      // 对外的接口
      void addNum(int number)
      {
          total += number;
      }
      // 对外的接口
      int getTotal()
      {
          return total;
      };
   private:
      // 对外隐藏的数据
      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 class above adds the numbers and returns the sum. P ublic members addNum and getTotal are external interfaces that users need to know in order to use classes. Private member total is not required by the user to understand, but is necessary for the class to work properly.

Design the strategy

Abstraction separates code into interfaces and implementations. Therefore, when designing components, the interface must be kept independent of the implementation, so that if the underlying implementation is changed, the interface will remain the same.

In this case, the interface is not affected regardless of whether any program uses the interface, just recompiling the latest implementation.