Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does C++ Abstract data Type mean?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what C++ abstract data type refers to, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

One of the concepts we often encounter when learning data structures is Abstract data types (Abstract Data Type), or ADT for short.

Wikipedia defines abstract data types as mathematical models of specific types of data structures with similar behavior in computer science, or data types of one or more programming languages with similar semantics.

Judging from this definition, it is very difficult to understand, in fact, we just need to grasp the core. The core is the separation of interface and implementation. When we use an ADT, we only need to interact with the interface without paying attention to the implementation details in the interface. Similarly, data is hidden and invisible and needs to be interacted through interfaces.

In other words, the interface is the only way to interact with data types. Otherwise, users have no access to ADT data and implementation details.

For example: take the stack as an example, if we do not design the stack as ADT, then when using the stack, users may need to create an array to store the data in the stack and implement the function of the stack by calling some methods. But it is necessary for the user to understand the principle of the stack and the details of data storage. ADT will make a good encapsulation. Users only need to know the function of each interface and call the corresponding interface to implement the logic they want.

Let's take a look at an example of a stack implemented in C++ Primer.

First of all, we need to know the total number of interfaces in the stack, such as the following:

Create an empty stack

Add data to the top of the stack

Data can be popped up from the top of the stack

You can check whether the stack is empty.

You can check whether the stack is full

Then we followed the object-oriented design idea of C++ and encapsulated it in a class.

First, let's define this class:

# ifndef STACK__H_#define STACK__H_typedef unsigned long Item;class Stack {private: enum {MAX=10}; Item items [MAX]; int top; public: Stack (); bool isempty () const; bool isfull () const; bool push (const Item & item); bool pop (Item & item);}; # endif

If we look at this definition, we will find that the data is set to private, that is, the user cannot access the data directly. You can only interact through the interface of public, and you don't have to care about the implementation details, so you can use it as a black box.

Finally, let's take a look at the implementation given in C++ Primer:

# include "stack.h" Stack::Stack () {top = 0;} bool Stack::isempty () const {return top = = 0;} bool Stack::isfull () const {return top = = MAX;} bool Stack::push (const Item & item) {if (top)

< MAX) { items[top++] = item; return true; } return false;}bool Stack::pop(Item &item) { if (top >

0) {item = items [--top]; return true;} return false;} the above is what C++ abstract data type refers to. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report