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.
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.
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.
Program to Demonstrate Abstraction in C++
// data abstraction
private: int x,y,z;
cout<<“Enter two numbers: “;
cout<<“Sum of numbers: “<<z;
Enter two numbers:
Sum of numbers: 45
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.
Implementation of Data Abstraction in C++
C++ is an object-oriented programming approach. The data abstraction in C++ is implemented in two ways:
- Using classes and objects
- Using header files
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.
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.
Let us have a look at a simple C++ program to understand abstraction using classes, objects, and header files:
using namespace std;
int num1, num2;
// To access private members
void input(int n1, int n2)
num1 = n1;
num2 = n2;
cout<<” Sum is:” << num1+num2 << endl;
cout<<“Data Abstraction in C++“<<endl;
Data Abstraction in C++
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
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.
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.