As you may know, data types are of two types, primitive and non-primitive data types in C++. But what about abstract data types in C++?? ADT comprises a collection of data and a set of operations on particular data. We use a Data Structure to store and organize data in a computer so that it can be used efficiently, which is why ADT comes into the picture.
This article talks about only abstract data types in C++ in detail.
What are Abstract Data Types?
To understand abstract datatype in C++, we must first understand what abstract is. In the data structure, an abstraction means hiding the details and showing only the main product.
A class with a defined set of operations and values is known as an abstract data type (or ADT).
An abstract data type is a data type whose behavior is defined by the qualities and functions within a class. Or we use structure to use an object of the class to have the specific abstract data types.
ADT consist of three parts:
- Data that describes the structure of data used in ADT.
- Operations which describes the valid operations for the ADT.
- Error that describes how to deal with the errors that on occur.
You can also do a free certification of Basics of C++.
Abstract Data Types
According to a mathematical model, data objects comprise a data type and the functions that operate on the objects, and it is defined as an abstract data type.
There is a broad distinction between "imperative" (or "operational") and "functional" (or "axiomatic") definition styles.
A developer uses an imperative approach to write code that specifies the computer's steps to achieve the goal. This is also known as algorithmic programming. On the other hand, a functional approach includes composing the problem as a set of functions to be executed.
In a particular program, states are the values that have an identity. Let's see this with an example below.
Var sum = 0; Var a = 2; Var b = 4; Sum = a+b; Print sum;
Here we saw the program adds two variables together and assigns them to the sum variable.
The state of the sum variable changed from 0 at the start of the program to 6 before the print function.
Functionality is a method of programming that attempts to bind everything in the style of pure mathematical functions. It's a declarative programming style. Its primary focus is on "what to solve" instead of an imperative style, which focuses on "how to solve."
An example of the functional style is an abstract stack (functional) which we have discussed later in this article.
Features of ADT:
- Abstraction: It is the key concept in object-oriented programming. An ADT hides the implementation details of the data structure and its operation from the user, providing only the necessary things. This allows the user to focus on the data structure's functionality without having to worry about how it is stored or implemented.
- Encapsulation: ADT encapsulates its data and operations, which means that the data and operations are not accessible from outside the ADT. This protects the data from unauthorized access and modifications
- Polymorphism: ADT can be used with different data types, which makes data structure more flexible and reusable.
- Inheritance: ADT allows inheritance which allows it to create more complex data structures as it can inherit other ADT.
Advantages of ADT
The advantages of abstract data type(ADT) are mentioned below:
Abstract data types in data structure types support code reusability, allowing a programmer to write shorter code.
- Localization of change
Disadvantages of ADT
Any changes to the data type must be implemented by all programs that inherit it, even if they are not required.
- ADTs can be less efficient than using low-level data structures directly.
- ADTs can hide important implementation details, making it harder to optimize code.
- Designing and implementing ADTs can require more upfront time and effort.
Typical Operations In ADT
Some of the operations that are frequently specified for ADTs (possibly under different names) are:
|hash(s)||It uses the instance's state to compute some standard hash function;|
|print(s) or show(s)||It obtains a human-readable illustration of the instance's state.|
|compare(s, t)||It determines whether the states of two instances are equivalent in some way.|
|clone(t)||It performs s ←create(), copy(s,t), and returns s|
|free(s) or destroy(s)||It reclaims the memory and other resources used by s.|
Examples of Abstract Data Type
Following are the examples of Abstract Data types:
- Priority queue
- Double-ended queue
- Double-ended priority queue
Stack Data Type Program
A stack is a collection of elements that is an abstract data structure. The stack uses the LIFO, meaning the element at the end is popped out first. Some of the stack's primary operations are as follows:
- Push- It pushes the value to the top of the stack.
- Pop - Removes the data value at the top of the stack.
- Peek - This method returns the stack's top data value.
You can try by yourself with the help of online c++ compiler.
Many programming languages, like C++ and Java, come with standard libraries that implement many common ADTs.
These ADTs are often called -
- container classes
Commonly used ADTs are -
- List (or sequence or vector)
- Multi-set (or bag)
- Stack and Queue
- Map (or dictionary)
Frequently Asked Questions
1. What are abstract data type functions?
- Abstract Data Type functions are toolkits for specific data structures.
- They give easy-to-use actions like adding, finding, removing, or looking through data.
- They keep things neat and reusable by separating how data works from how it's built.
- These functions hide complicated details and make it simple to work with data.
- For example, toolkits for stacks, lists, and trees, each with its unique set of actions.
2. What are the 3 types of abstraction?
The three types of abstraction are:
- Data abstraction - hiding implementation details of data structures
- Procedural abstraction - hiding implementation details of functions and procedures
- Control abstraction - hiding implementation details of control flow mechanisms like loops and conditionals.
3. What is example of abstract class in C++?
An example of an abstract class in C++ is the "Shape" class, which provides a common interface for different geometric shapes but does not implement the "draw" method, which must be defined by the subclasses inherited from it.
We have discussed Abstract Data Types in C++ in this article. We have covered abstract data type, two ways to define abstract data type in C++, advantages, and disadvantages, etc. We hope this article helps you enhance your knowledge of abstract data types in C++.
We encourage you to check out our other articles to learn more about C++, like the abstract data types in C++
To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course.