What is Abstraction in C++? With Real Life Example and Its Implementation
By Rohan Vats
Updated on Nov 17, 2022 | 6 min read | 7.8k views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Nov 17, 2022 | 6 min read | 7.8k views
Share:
Table of Contents
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.
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.
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.
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
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?
C++ is an object-oriented programming approach. The data abstraction in C++ is implemented in two ways:
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.
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
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.
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.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources