Simple understanding of data type encapsulation and data abstraction, data type encapsulation Abstraction

Source: Internet
Author: User

Simple understanding of data type encapsulation and data abstraction, data type encapsulation Abstraction

Pay special attention to programming technologies, rather than language features.

-- C ++ programming language Bjarne Stroustrup


This article is the Reading Notes of chapter 2 of Bjarne Stroustrup. The example is from the second chapter of this book.
In program design, we tend to organize the data structure (or data type) and a group of related processes for its operations. Logically, we can call it a module. At this time, the program is divided into some modules, including a set of operations on data, data is hidden in the module. The following uses the stack design as an example to describe data encapsulation and data abstraction in C and C ++.
For the C language, we can perform the following simple design for the stack data type:

typedef struct Stack {  int elem[MAX_SIZE];  int top;} Stack;Stack* createStack();void destroyStack(Stack*);void push(Stack*,int);int pop(Stack*);

Because the struct structure can be freely accessed from outside, data may be damaged. We can introduce a design similar to the handle used by win32 programming. That is:

typedef void* HStack;HStack createStack();void destroyStack(HStack hStack);void push(HStack hStatck,int) {     struct Stack* stack = (struct Stack*)hStack;     ........ }int pop(HStack hStack){     struct Stack* stack = (struct Stack*)hStack;    ........ }

Only when operations related to the data type are implemented can the internal structure of the Stack be known and the Stack can be forced to convert. Therefore, the user of this module does not need to know the structure of the struct Stack. changing the structure of the struct Stack does not affect the use of API users.

Client code:

void f(){    HStack h= createStack();    push(h,2);    int i = pop(h);    destroyStack(h);}

Therefore, when C language encapsulates similar data types, a group of operations usually include initialization and recycling operations, which usually requires the user to call them consciously. This makes the data type a bit "pseudo-type". The features of C language cannot enable the encapsulated data type to be automatically initialized and destroyed.

C ++ provides the user-defined class to support data type encapsulation. During program design, complete operations are provided for each type by determining which types are required. Stack definition:

class Stack{public:    Stack(int max_size);    ~Stack();    void push(int);    int push();private:    int* m_elem;    int m_top;    int m_max_size;};

The constructor Stack (int) is called when an object of this class is created to handle initialization issues. If an object of this class has its scope, call its destructor when cleaning up.

The definition of the Stack Type above can be called a specific type, involving specific implementation. This design method is sufficient when the type is not frequently changed or the type is used for local variables. In some cases, we want abstract types. Abstract types can isolate users from implementation details for better flexibility. At this time, the user is oriented to abstract type programming, rather than specific type programming. C ++ can achieve this through class inheritance and abstract classes. For example:

Class Stack {public: virtual void push (int) = 0; virtual int pop (int) = 0 ;}; // use an array to implement Stack class ArrayStack: public Stack {}; // use the linked list to implement Stack class ListStack: public Stack {}; // user-oriented abstract class programming: void f (Stack & s) {s. push (2); int I = s. pop ();}

By defining the Stack abstract class (in C ++, it can be understood as a class with pure virtual member functions. For example, virtual void push (int) = 0;) provides flexibility for different implementations of this abstract class. Users can program this abstract class (in C ++, it is usually a reference or pointer of this type of object to realize polymorphism ).

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.