C Programming and Data Structures (16 Blogs)

How To Implement Data Abstraction In C++

Last updated on Aug 08,2019 6.6K Views

How To Implement Data Abstraction In C++

Data Abstraction is showing essential information to the user but hiding the background details. In this article we would be understanding Data Abstraction in C++. Following pointers will be covered in this article,

So let us get started with this article,

Abstraction In C++

Consider an example

A person uses a mobile phone unless he is from an IT or ECE background he does not know anything other then what buttons to press. This is a proper example of Data Abstraction.

There are two ways of implementing Data Abstraction in C++:

Abstraction Using Classes

In classes, we use access specifiers to bring about data abstraction.

Abstraction using header files

We use a different function from different header files, but we do not know any of the implementation details.

Let us move on with this abstraction in C++ article

Abstraction Using Specifiers

We can implement Abstraction by using access specifiers. They give the programmer the control on what data or functions are to be made visible to the user and what is kept a secret. There are three main access specifiers,

Private: Abstraction In C++:

When data member or member functions are made private, it can only be accessed inside the class and no one outside the class can access it.

Public: Abstraction In C++:

When data member or member functions are made public, it can be accessed by everyone.

Protected: Abstraction In C++:

Protected Access Specifier is a special kind of access specifier. When data member or member functions are made protected, it works similarly to private and it can be accessed to members of the class. 

Let us move on with this abstraction in C++ article

Types Of Abstraction

There are 2 types of abstraction,

Data Abstraction

Hiding the details about the data is called data abstraction.

Control Abstraction

Hiding the details about the implementation is called control abstraction.

Advantages Of Abstraction

  • Only you can make changes to your data or function and no one else can.

  • Makes the application secure by not allowing anyone else to see the background details.

  • Increases reusability of the code.

  • Avoids duplication of your code.

Let us move on with this abstraction in C++ article

Sample Code


#include<iostream>
using namespace std;
class test
{
private:
int x;
public:
test(int a)
{
x =a;
}
int get()
{
return x;
}
};
int main()
{
test a(7);
cout<<"The Number is: "<<a.get();
return 0;
}

Output

output - abstraction in java - Edureka

Explanation

In the above program, we show the concept of abstraction. We have a private member x which cannot be accessed from the main function. The only way to access it is by creating an object of class test. There is a parameterized constructor, which assigns the value we get from main to x. We have a get method, that returns the value of x.

Inside the main function, we create an object of test class and assign a parameter. This moment the parameterized constructor is called, and it assigns the parameter value to x.

We have a count statement that calls the get function and the number is displayed. This is the basic concept of abstraction. We cannot directly use the private data member in the main function. If we decide to make the display function private and try to access it, we get an error. Encapsulation and Abstraction are the major features of Object-Oriented programming.

With this we come to the end of this article on ‘Abstraction In C++’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of  this article and we will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Implement Data Abstraction In C++

edureka.co