Latest web development tutorials

C ++ data abstraction

Data abstraction refers only to provide critical information to the outside world, and hide the implementation details of their background, that the performance of only the necessary information without presenting details.

Data abstraction is a dependency on the programming interface and implementation separation (design) technology.

Let's take a real-life real examples, such as a TV, you can turn on and off, change 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 received by a cable signal, how to convert signal and eventually 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 principle, directly through its external interface (such as the power button on the remote control, sound volume controller) can control the TV.

Now, let's get down to business, it is in terms of C ++ programming, C ++ classdata abstraction possible.Them to the outside world provides a common method for operating a large number of data objects, that is, in fact, the outside world did not know the internal class implementation.

For example, your program can call thesort () function, without the need to know the function of the algorithms used to sort the data.In fact, the underlying function of the realization of the sort due to different repository vary, as long as the same interface, function call as usual.

In C ++, we use our ownclass to define abstract data type (ADT).You can use the classostream coutobject to output data to standard output, as follows:

#include <iostream>
using namespace std;

int main( )
{
   cout << "Hello C++" <<endl;
   return 0;
}

Here, you do not understand how thecout text displayed on the user's screen.You only need to know the public interface to, cout underlying implementation can be freely changed.

Access tab mandatory Abstract

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

  • Tag defines the use of public members can access all parts of the program. Abstract view of a type of data is public by its members defined.
  • The use of private label defined using the class members can not have access to the code. Private part of the code using the type of hide implementation details.

Access frequency tag appears there is no limit. Each tag specifies the access followed by members of the defined access levels. Assigned access level will remain in effect until the next encounter Access tab or close the right body type encountered in parenthesis.

Data abstraction Benefits

Data abstraction has two important advantages:

  • Inner class is protected from inadvertent user-level error caused damage to the state of the object.
  • With the class implementation may change over time, in order to respond to changing needs, or meet those requirements do not change the user level code error report.

Private section defines the data members of the class, if only in the preparation of such authors can freely change the data. If implemented changes, you only need to check the code for the class to see what impact this change may cause. If the data is public, any direct access to the old form of data representation function members are likely to be affected.

Abstract instance data

C ++ program, any class with members 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;
}

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

Total 60

The class above the numbers together and returns the sum. Public MemberaddNum and getTotalis external interface, users need to know in order to use their class. Private memberstotal users do not need to know, but the class can work necessary.

Design Strategy

Abstract to separate code into interface and implementation. So in the design of components, the interface must remain independent of implementation, so that if you change the underlying implementation, the interface will remain unchanged.

In this case, regardless of any program using the interface, the interface will not be affected, only to realize the latest recompilation.