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.
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.
Imperative Style
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.
Functional Style
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.
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.
Encapsulation
Localization of change
Flexibility
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:
OPERATION
FUNCTION
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.
Following are the examples of Abstract Data types:
Collection
Container
List
String
Set
Multiset
Map
Multimap
Graph
Tree
Stack
Queue
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.
Program
C++
C++
#include <iostream>
using namespace std;
#define MAX 10
// Creating a stack. struct stack { int stackItems[MAX]; int stackTop; };
typedef struct stack st;
void makeEmptyStack(st *s) { s->stackTop = -1; }
// Check if the stack is full. bool isFull(st *s) { return s->stackTop == MAX - 1; }
// Check if the stack is empty. bool isEmpty(st *s) { return s->stackTop == -1; }
// Add elements into the stack. void push(st *s, int newitem) { if (isFull(s)) { cout << "STACK FULL" << endl; } else { s->stackItems[++s->stackTop] = newitem; } }
// Pop elements from the stack. void pop(st *s) { if (isEmpty(s)) { cout << "STACK EMPTY" << endl; } else { cout << "Popped Stack Item = " << s->stackItems[s->stackTop--] << endl; } }
// Display Stack elements. void displayTheStack(st *s) { cout << "The Stack: "; for (int i = 0; i <= s->stackTop; i++) { cout << s->stackItems[i] << " "; } cout << endl; }
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.
Conclusion
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++
We hope you have gained a better understanding of these topics now! Are you planning to ace the interviews with reputed product-based companies like Amazon, Google, Microsoft, and more?
To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course.
Happy Coding!
Live masterclass
Learn Advanced Excel: Ace Data Analytics Interview at Amazon
by Megna Roy, Data Analyst @ Amazon
04 Oct, 2024
01:30 PM
Ace Flipkart's Data Analytics interview by decoding its data strategy
by Ashwin Goyal, Product @ HRS Group, Ex - Udaan, OYO
29 Sep, 2024
06:30 AM
Stay irreplaceable: Master these essential AI tools for developers
by Pranav Malik, SDE 3 @ Oracle, Ex- Microsoft
03 Oct, 2024
01:30 PM
Learn Advanced Excel: Ace Data Analytics Interview at Amazon
by Megna Roy, Data Analyst @ Amazon
04 Oct, 2024
01:30 PM
Ace Flipkart's Data Analytics interview by decoding its data strategy
by Ashwin Goyal, Product @ HRS Group, Ex - Udaan, OYO