C++ Example Of Abstraction – Detailed FAQ

One of the features of object-oriented programming is abstraction, which allows you to show the user only the information that is relevant and hide the information that isn’t. For instance, when you click “send” to send someone an email, you only see the success message without knowing what actually happens or how the recipient’s data is sent over the network (because it is irrelevant to you).

Let’s look at how access specifiers can be used to accomplish this in a C++ program:

Abstract Illustration

#include <iostream>
using namespace std;
class AbstractionExample{
   /* By making these data members private, I have
    * hidden them from outside world.
    * These data members are not accessible outside
    * the class. The only way to set and get their
    * values is through the public functions.
   int num;
   char ch;

   void setMyValues(int n, char c) {
      num = n; ch = c;
   void getMyValues() {
      cout<<"Numbers is: "<<num<< endl;
      cout<<"Char is: "<<ch<<endl;
int main(){
   AbstractionExample obj;
   obj.setMyValues(100, 'X');
   return 0;


Numbers is: 100
Char is: X

Data abstraction has an advantage

The main benefit of using this feature is that you simply need to modify the high level class where you designated the members as private when the code changes and you need to make some changes. You do not need to alter the low level (user level) class code because no class directly accesses these data members.
Imagine if you had made these data members available to the whole public. If you ever wanted to change the code, you would have to update all the classes that directly access the members.Data abstraction also has the following benefits:

1) It secures the program by making data private and preventing user-level errors that could taint the data.
2) By doing so, code duplication is prevented, and code reuse is improved.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button