C ++ Advanced Learning Series: data abstraction, encapsulation and interfaces

Data Abstraction

Data abstraction refers only to provide critical information to the outside world, and hiding the implementation details of their background, that is, only to demonstrate the necessary information without presenting details. Data abstraction is a dependent on the programming interface and implementation separation (design) techniques.

Let's take a real example of real life, such as a TV, you can turn on and off, switch channels, adjust the volume, adding external components (such as speakers, VCR, DVD player), but you do not know its internal implementation details, that is, you do not know how it is receiving a signal through cable, how to convert signal, and finally displayed on the screen. Therefore, we can say the TV to its internal implementation and external interfaces separated, and you do not know its internal implementation principle, directly through its external interface (such as a power button, a remote control, sound volume controller) can operate a TV.

C ++ class for data abstraction possible. They provide a common method for operating a large number of data objects to the outside world, that is, in fact, the outside world did not know the internal class implementation.

For example, your program can call the  sort ()  function, without the need to know the algorithm used to sort data function. In fact, the underlying function implementation will vary depending on the sort of repository vary, as long as the same interface, function calls can work as usual.

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

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

Here, without understanding  cout  how text is displayed on the user's screen. Only need to know the public interface can be, cout underlying implementation can be freely changed.

Access tab mandatory abstract

In C ++, we use the Access tab (public private protect) to define a class of abstract interface . A class can contain zero or more access tags :

  • Members of the public label definitions have access to all parts of the program. Abstract view of a data type it is defined a common member.
  • Members to use private label definition can not have access to the code that uses the class. Private use of part of the type of code hides the implementation details.

Frequency Access tab appears there is no limit. Each tag specifies the access level of access immediately followed by the member definition. Specify the access level will remain in effect until the next encounter Access tab or close the right body type encountered in parentheses.

The benefits of data abstraction

Data abstraction has two important advantages:

  • Inner classes are protected, user-level error will not cause unintended damage to the state of the object.
  • Class may realize over time and change, in order to respond to changing needs, or meet those requirements do not change the user level code error reporting.

If the private section defines only data members of the class (the data is private), the authors write that class can freely change the data, if the implementation changes, you only need to type the code checks to see what impact this change will result. And if the data is public, any direct access to old data expressed as a function of the form members are likely to be affected.

Examples of data abstraction

C ++ program, any of the classes with a member of the public and private data abstraction can be used as examples. Consider the following examples:

#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;
}

Code output:

Total 60

The above class numbers together, and returns the sum. Public Member  addNum  and  getTotal  is the external interface, users need to know in order to use their class. Private data members  total  users do not need to know, but the class is normally necessary for the work.

Published 161 original articles · won praise 90 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_42415326/article/details/104056698