Abstract Data Type in C++
What is Abstract Data Type?
An object's behaviour is determined by an assortment of variables and a collection of actions, and they are referred to as the abstract data type (ADT) or class. The ADT specification specifies the tasks must be completed; it does not state how they will be carried out. It is unclear which algorithms will be applied to the computations and how the information will be arranged in memory. Since it provides an implementation-independent view, it is called "abstract."
Data types regarded as built-in, including int, float, double, long, etc., allow us to perform fundamental operations like addition, subtraction, division, and multiplication. We may now encounter a scenario requiring particular operations for our customized information type. Only when and as needed can these operations be defined. Therefore, we can design data structures and associated operations to simplify solving issues. These non-built data structures are called Abstract Data Types (ADT).
![Abstract Data Type in C++](https://static.tutorialandexample.com/cpp/abstract-data-type-in-cpp1.png)
Users of data types don't always need to understand how those kinds are implemented. For instance, we've been utilizing basic numbers like int, float, and char data types, merely knowing that they may be used and operated upon without knowing how they're constructed.
Abstract Data type in C++:
In C++, a collection of actions that can be carried out on a specific data structure are described at an upper level without going into specifics about how those operations are carried out. This is known as an Abstract Data Type (ADT). Put another way; an ADT does not define how the information is saved, or the actions are carried out; instead, it specifies which actions can be carried out on a data structure and how those actions should behave.
There are several ways to create abstract data types in C++. Typical methods include some of the following:
Objects and Classes:
Classes are vital in the object-orientated programming paradigm, which C++ supports. By defining a class, you can create an object design by encapsulating operations and data. Constructed entities from these categories indicate examples of abstract datatypes.
Example Code:
class Stack { Public: void push(int element); int pop(); bool isEmpty(); Private: int data[100]; int top; }; void Stack::push(int element) { } int Stack::pop() { } bool Stack::isEmpty() { }
Output:
![Abstract Data Type in C++](https://static.tutorialandexample.com/cpp/abstract-data-type-in-cpp2.png)
Templates:
Generic classes and methods that handle various data types can be created using C++ templates. You can use this to develop abstract data types that work with different kinds of information.
Example Code:
templateclass Stack { Public: void push(Template element); Template pop(); bool isEmpty(); private: Template data[100]; int top; }; template void Stack::push(Template element) { } template Template Stack::pop() { } template bool Stack::isEmpty() { } Output:
Standard Template Library (STL):
A wide range of template classes and functions that implement popular abstract data types are available in the C++ Standard Library. For instance, dynamic arrays, connected lists, and queues are implemented by std::vector, std::list, and std::queue, in that order.
Thus, the user is not required to understand how a particular data type will be implemented—only what it can accomplish. Consider ADT a "black box" that conceals the data type's internal organization and architecture.
We will now define the List ADT, Stack ADT, and Queue ADT.
1. Compile ADT:
- A list with a head structure comprising a measure, points, and an address of the comparison function—necessary to compare the data in the list—is typically used to keep the data in an essential series.
- A self-referential pointer that points to the following node in the list and an entry point to the data structure are both contained in the information node.
2. ADT Stack:
- The reference to data is saved in each node of the Stack ADT Representation rather than actual data.
- The application allows memory for the data, and its address is sent to the stack ADT.
- The ADT encapsulates both the head and information nodes. Only the pointer to the stack is visible to the calling program.
- The stack head structure also includes an indication of the present amount of elements in the stack and a pointer to the top.
3. ADT queue:
Every node has a link pointer that points to the following item in the queue and a void pointer that points to the data. The application must set aside space for storing the information.
The fundamental layout of the stack abstract data type is followed by the queue abstracted data type (ADT).