Site icon DataFlair

Data Abstraction in C++ | A Never Diminishing Concept to Dig

Data Abstraction in C++ Tutorial

Free C++ course with real-time projects Start Now!!

Data abstraction in C++ is an integral concept of object-oriented programming. If we ask newbies about data abstraction, then they would probably say that this concept is not their cup of tea. A question mark can be seen clearly on their faces. There’s a reason behind this, they’re not aware of the basics of this concept and they try to handle its major operations.

Data Abstraction in C++

As discussed earlier, data abstraction is a concept of object-oriented programming that helps us to represent only the important features of the program without including its background details or explanation.

Let us consider a real-life situation to better understand the concept of data abstraction.

Example of Data Abstraction

Suppose you are operating a mobile phone. You know that the important feature of a phone is the memory card, SIM, battery life, design and build, and processor power.

But, while operating the phone you do not get into the operational details of the phone such as how the CPU of the phone allocates memory for the various media on your phone or other intricate architectural details of the phone. All these details aren’t visible to cell-phone users from a non-technical background.

This is an example of abstraction where you get to know only about the important features without going into the depth of its working. You can use only certain commands and buttons on your phone without getting to know what is internally happening inside your phone.

Implementation of Data Abstraction in C++

We can implement the concept of data abstraction in 2 main ways:

1. Abstraction using classes and objects

With the help of classes, we can decide if we want a particular data member or member function to be accessible outside the class or not with the help of access specifiers (public, private or protected)

2. Abstraction through header files

Header files indicate the inclusion of several inbuilt library functions associated with that header file. For instance, if we include the header file #include<string.h>, functions such as strlen(), strcpy and many more are accessible to the programmer without getting to know the internal logic behind these inbuilt functions.

Example-

Let us consider a program where we take 2 numbers as input and obtain their sum. This program will illustrate how to find their sum without being able to separately access the 2 numbers.

The most common way to implement data abstraction is through classes and objects with the help of access specifiers. Let us consider a simple program in C++ to better understand it with respect to the above-stated problem.

Here is a code that illustrates the use of data abstraction in C++:

#include <iostream> 
using namespace std; 

class Abstraction 
{
// Private by default 
int number1, number2; 

public: 
// Function to access private members

void input(int n1, int n2) 
{ 
number1 = n1;
number2 = n2;
} 

void sum() 
{ 
cout<<"Their sum is:" << number1+number2 << endl; 
} 
}; 

int main() 
{

cout<<"Welcome to DataFlair tutorials!"<<endl;

Abstraction a; 
a.input(17, 3); 
a.sum(); 
return 0; 
}

Output-

Advantages of Data Abstraction in C++

Let us discuss some of its benefits in order to acknowledge its significance. The benefits of data abstraction are:

1. Data abstraction increases the reusability of the code by avoiding any chances of redundancy.

2. It increases the readability of the code as it eliminates the possibility of displaying the complex working of the code.

3. With the implementation of classes and objects, comes enhanced security. Since data abstraction is a method of implementing classes and objects any denying access to other classes of accessing the data members and member functions of the base class.

4. It helps the user to write a high-level code.

5. It separates the entire program into code and implementation making it more comprehensible.

Quiz on Abstraction in C++

Time limit: 0

Quiz Summary

0 of 15 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

      

    class A

    {

        private:

            int x;

        public:

            A(int a){x=a;}

            void show()

            {

                cout<<x;

            }

    };

      

    int main() 

    {

        A obj(1);

        obj.show();

        return 0;

    }

    Correct
    Incorrect
  2. Question 2 of 15
    2. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

      

    class A

    {

        private:

            int x=1;

            void show()

            {

                cout<<x;

            }

     

    };

    int main() 

    {

        return 0;

    }

    Correct
    Incorrect
  3. Question 3 of 15
    3. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

      

    class A

    {

        private:

            int x, y;

        public:

            void val(int a, int b)

            {

                x=a;

                y=b;

            }

            void show()

            {

                cout<<x<<y;

            }

    };

      

    int main() 

    {

        A obj;

        obj.val(1, 2);

        obj.show();

        return 0;

    }

    Correct
    Incorrect
  4. Question 4 of 15
    4. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class Add {

       public:

          void add(int number) {

             result += number;

          }

          int sum() {

             return result;

          };

          

       private:

          int result=0;

    };

     

    int main() {

       Add a;

       

       a.add(1);

       a.add(2);

       a.add(3);

     

       cout << a.sum();

       return 0;

    }

    Correct
    Incorrect
  5. Question 5 of 15
    5. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class Add {

       public:

          void add(int number) {

             result += number;

          }

          int sum() {

             return result;

          };

          

       private:

          int result=0;

    };

     

    int main() {

       Add a;

       

       a.add(10);

       a.add(-2);

       a.add(-3);

     

       cout << a.sum();

       return 0;

    }

    Correct
    Incorrect
  6. Question 6 of 15
    6. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class Add {

       public:

          Add(int number) {

             result += number;

          }

          int sum() {

             return result;

          };

          

       private:

          int result=0;

    };

     

    int main() {

       Add a1(1),a2(8);

       cout << a1.sum()+a2.sum();

       return 0;

    }

    Correct
    Incorrect
  7. Question 7 of 15
    7. Question

    What is the output of following program?

    #include <iostream>

    #include <math.h>

    using namespace std;

     

    int main() {

       int a=64;

       int i=sqrt(a);

       cout<<i;

       return 0;

    }

    Correct
    Incorrect
  8. Question 8 of 15
    8. Question

    What is the output of following program?

    #include <iostream>

    #include <math.h>

    using namespace std;

     

    int main() {

       int a=3,b=2;

       int i=pow(a,b);

       cout<<i;

       return 0;

    }

    Correct
    Incorrect
  9. Question 9 of 15
    9. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

    class A{

    private:

       int x;

       char y;

     

    public:

       void set(int a, char b) {

          x=a; y=b;

       }

       void get() {

          cout<<x<<y;

       }

    };

    int main(){

       A obj;

       obj.set(7, ‘X’);

       obj.get();

       return 0;

    }

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

    class A{

    private:

       int x;

       char y;

       void set(int a, char b) {

          x=a; y=b;

       }

       void get() {

          cout<<x<<y;

       }

    };

    int main(){

       A obj;

       obj.set(7, ‘X’);

       obj.get();

       return 0;

    }

    Correct
    Incorrect
  11. Question 11 of 15
    11. Question

    Mechanism of hiding implementation details and exposing only interface

    Correct
    Incorrect
  12. Question 12 of 15
    12. Question

    What among the following is not an advantage of data abstraction?

    Correct
    Incorrect
  13. Question 13 of 15
    13. Question

    what are the ways to achieve abstraction?

    Correct
    Incorrect
  14. Question 14 of 15
    14. Question

    What are the main access specifiers to achieve abstraction?

    Correct
    Incorrect
  15. Question 15 of 15
    15. Question

    Which statement is not true with respect to data abstraction?

    Correct
    Incorrect

Summary

Now, we’ve reached towards the end of the tutorial. After completing this article, we got to know about data abstraction in C++, we also inferred its significance and importance. Thereafter, we studied the implementation techniques of data abstraction. It is safe to say that, it wouldn’t be very difficult for a beginner with little or no knowledge to grasp this concept.

If you have any queries, then do let us know in the comment section below.

Exit mobile version