Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconWhat is Abstraction in C++? With Real Life Example and Its Implementation

What is Abstraction in C++? With Real Life Example and Its Implementation

Last updated:
19th May, 2021
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
What is Abstraction in C++? With Real Life Example and Its Implementation

Object-Oriented Programming (OOP) is the most popular paradigm of programming and is the standard way to code for programmers. OOP works on the concept of classes and objects. It structures a software program into simple, reusable pieces of code called classes for creating individual objects. C++ is an object-oriented programming language, and abstraction in C++ is one of the most important C++ features and the highlight of this article.

Object-oriented programming is meant to enhance the flexibility of the program. In this article, you will learn about how data abstraction is carried out in the C++ program along with the benefits of doing it. 

Check out our free courses to get an edge over the competition. 

Data Abstraction in C++ 

Abstraction is derived from two Latin words, ‘abs’, which means ‘away from’ and ‘traction’ meaning ‘to draw’, refers to representing required essential features without including the whole details.

Ads of upGrad blog

C++ classes perform abstraction with the list of abstract attributes such as functions. They put all the essential properties of an object to be created. The attributes are called data members, and the functions that operate on them are called member functions. C++ classes using the data abstraction concept are termed abstract data types.

 

Source

While using a class, data members and member functions are represented in the code. But, the built-in data types and the members in the class get ignored while using an object which is known as data abstraction. 

Check out upGrad’s Java Bootcamp

Program to Demonstrate Abstraction in C++

#include<iostream.h>

#include<conio.h>

class sum

{

// data abstraction

private: int x,y,z;

public:

void add()

{

clrscr();

cout<<“Enter two numbers: “;

cin>>x>>y;

z=x+y;

cout<<“Sum of numbers: “<<z;

}

};

void main()

{

sum s;

s.add();

getch();

}

Output

Enter two numbers:

22

23

Sum of numbers: 45

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)

Real-Life Examples Demonstrating Abstraction

The abstraction is a feature of Object-Oriented Programming, where only relevant details are shown to the user and irrelevant details are hidden.

You can understand Abstraction in C++ with these examples,

When an email is sent, you just click send and you get the sent receipt. What is abstracted here is the data transferred to the recipient.

The important features of a phone are the memory card, SIM, battery life, design, and processor. While operating the phone you do not get into the operational details of the phone such as CPU memory allocation for the various media that are invisible to you. You can use only certain commands and buttons on your phone without knowing what is happening inside your phone.

A man driving a car knows that pressing the accelerators will increase the speed of the car but he does not know-how, which is nothing but abstraction.

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

 

Implementation of Data Abstraction in C++

C++ is an object-oriented programming approach. The data abstraction in C++ is implemented in two ways:

  1. Using classes and objects
  2. Using header files

 

Source

1. Using Classes and Objects

We can choose to abstract a specific data member or member function to be accessible outside the class or not using public, private, or protected access specifiers with the help of classes.

Explore our Popular Software Engineering Courses

2. Using Header Files

Header files include inbuilt library functions linked to that header file. For the header file #include<string.h>, programmers can access the functions strcpy() and strlen() and many more.

Program:

Let us have a look at a simple C++ program to understand abstraction using classes, objects, and header files:

#include <iostream>

using namespace std;

class Abstraction

{

// Private

int num1, num2;

public:

// To access private members

void input(int n1, int n2)

{

num1 = n1;

num2 = n2;

}

void sum()

{

cout<<” Sum is:” << num1+num2 << endl;

}

};

int main()

{

cout<<“Data Abstraction in C++“<<endl;

Abstraction a;

a.input(11, 18);

a.sum();

return 0;

}

Output

Data Abstraction in C++

Sum is:29

In-Demand Software Development Skills

Benefits of Abstraction in C++

There is no denying the fact that abstraction in C++ is a beneficial programming concept. Let us explicitly discuss some of the benefits it offers:

It enhances code reusability and class partitioning by avoiding any redundancy.

It augments code readability by eliminating the complex working of the code making it comprehensible.

The internals of the class get protected from accidental user-level errors

Abstraction in C++ is a practice to write high-level code.

It avoids code duplication and the programmer does not have to go over common tasks every time for similar operations

It can alter internal class implementation independently without affecting the user.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Explore Our Software Development Free Courses

Conclusion

Ads of upGrad blog

Data abstraction in C++ is a method to provide data security from unauthorized methods. In this article, we tried to explain one of the most significant C++ concepts used widely along with its implementation and real-life examples for better clarity. It wouldn’t be difficult for a beginner with little or no knowledge to have a hang of the concepts with some practice.

If you are considering upGrad’s Master of Science in Computer Science course, then the learning process will be smoother for you.

We hope you will have an excellent learning opportunity in executing these C++ projects. If you are interested to learn more and need mentorship from industry experts, check out upGrad & IIIT Banglore’s Executive PG in  Full-Stack Software Development.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1What is abstraction in C++?

Abstraction is the process of hiding the details relevant to a specific purpose. This is the central idea of object-oriented programming. It allows programmers to write code that is more generic than the actual problem being solved. This helps in reducing the size of the code and simplifying the code. Abstraction also allows you to modify the code in the future, without having to change the code to fit the new needs.

2What are the important pillars of object-oriented programming?

Abstraction: We can think of abstraction as the process of using various layers of separation between the details of a complex operation and the code that performs this operation. Polymorphism: This basically means that the same operation can be represented differently in different contexts. Inheritance: Inheritance is all about code reuse. Interfaces: Interfaces are high level descriptions of classes. They are basically a contract specifying the methods that a class must have. Composition: Composition is all about combining objects in order to achieve more complex functionality.

3How to achieve abstraction in C++?

An abstraction is an implementation of a concept. It’s what you do when you use a bunch of classes and functions to separate an idea from the code that implements it. In C++, an abstraction is a class, a template, or a function. In C++, abstraction is achieved in many ways, for example, C++ provides multiple ways to achieve data abstraction, means to hide implementation details and let client code work with high level interfaces.

Explore Free Courses

Suggested Blogs

How to Rename Column Name in SQL
46742
Introduction We are surrounded by Data. We used to store information on paper in enormous file organizers. But eventually, we have come to store it o
Read More

by Rohan Vats

04 Mar 2024

Android Developer Salary in India in 2024 [For Freshers &#038; Experienced]
901109
Wondering what is the range of Android Developer Salary in India? Software engineering is one of the most sought after courses in India. It is a reno
Read More

by Rohan Vats

04 Mar 2024

7 Top Django Projects on Github [For Beginners &amp; Experienced]
51275
One of the best ways to learn a skill is to use it, and what better way to do this than to work on projects? So in this article, we’re sharing t
Read More

by Rohan Vats

04 Mar 2024

Salesforce Developer Salary in India in 2024 [For Freshers &#038; Experienced]
908634
Wondering what is the range of salesforce salary in India? Businesses thrive because of customers. It does not matter whether the operations are B2B
Read More

by Rohan Vats

04 Mar 2024

15 Must-Know Spring MVC Interview Questions
34571
Spring has become one of the most used Java frameworks for the development of web-applications. All the new Java applications are by default using Spr
Read More

by Arjun Mathur

04 Mar 2024

Front End Developer Salary in India in 2023 [For Freshers &#038; Experienced]
902261
Wondering what is the range of front end developer salary in India? Do you know what front end developers do and the salary they earn? Do you know wh
Read More

by Rohan Vats

04 Mar 2024

Method Overloading in Java [With Examples]
25802
Java is a versatile language that follows the concepts of Object-Oriented Programming. Many features of object-oriented programming make the code modu
Read More

by Rohan Vats

27 Feb 2024

50 Most Asked Javascript Interview Questions &#038; Answers [2024]
3987
Javascript Interview Question and Answers In this article, we have compiled the most frequently asked JavaScript Interview Questions. These questions
Read More

by Kechit Goyal

26 Feb 2024

OOP Concepts and Examples That Every Programmer Should Know
25126
In this article, we will cover the basic concepts around Object-Oriented Programming and discuss the commonly used terms: Abstraction, Encapsulation,
Read More

by Rohan Vats

26 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon