Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Abstract Data Types?
3.
Abstract Data Types
3.1.
Imperative Style 
3.2.
Functional Style
4.
Features of ADT:
5.
Advantages of ADT
6.
Disadvantages of ADT
7.
Typical Operations In ADT 
8.
Examples of Abstract Data Type
9.
Stack Data Type Program
9.1.
Program
9.2.
C++
9.3.
Output
10.
ADT libraries
10.1.
Example
10.2.
C++
10.3.
Output
11.
Frequently Asked Questions
11.1.
1. What are abstract data type functions?
11.2.
2. What are the 3 types of abstraction?
11.3.
3. What is example of abstract class in C++?
12.
Conclusion
Last Updated: Mar 27, 2024
Medium

Abstract Data Types in C++

Author Kanak Rana
0 upvote

Introduction

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.

abstract data types in c++

This article talks about only abstract data types in C++ in detail. 

Also, see Literals in C.Fibonacci Series in C++ 

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).

ADT in C++

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.

Check this out, Characteristics of OOPS

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.

  • 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.

Must Read Lower Bound in C++, hash function in data structure.

Examples of Abstract Data Type

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;
}

int main() {
st s;

makeEmptyStack(&s);

push(&s, 7);
push(&s, 8);
push(&s, 9);
push(&s, 10);
push(&s, 11);
push(&s, 12);

displayTheStack(&s);

pop(&s);

cout << "\nAfter Popping:\n";
displayTheStack(&s);

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

output

You can try by yourself with the help of online c++ compiler.

ADT libraries

Many programming languages, like C++ and Java, come with standard libraries that implement many common ADTs.

These ADTs are often called -

  • containers 
  • collections 
  • container classes

Commonly used ADTs are -

  • List (or sequence or vector)  
  • Set
  • Multi-set (or bag) 
  • Stack and Queue
  • Tree 
  • Map (or dictionary)

Example

  • C++

C++

#include <iostream>
#include <set>
using namespace std;
int main()
{
std::set<int> x;
for(int i = 1; i<=10; i++)
{
x.insert(i);
}
for(auto& i: x)
{
cout << i << ' ';
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

output

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:

  1. Data abstraction - hiding implementation details of data structures
  2. Procedural abstraction - hiding implementation details of functions and procedures
  3. 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 AmazonGoogleMicrosoft, and more? 

To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course.

Happy Coding!

Live masterclass