beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Abstraction in C++ with example

By Chaitanya Singh | Filed Under: Learn C++

Abstraction is one of the feature of Object Oriented Programming, where you show only relevant details to the user and hide irrelevant details. For example, when you send an email to someone you just click send and you get the success message, what actually happens when you click send, how data is transmitted over network to the recipient is hidden from you (because it is irrelevant to you).

Let’s see how this can be achieved in a C++ program using access specifiers:

Abstraction Example

#include <iostream>
using namespace std;
class AbstractionExample{
private:
   /* 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;

public:
   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');
   obj.getMyValues();
   return 0;
}

Output:

Numbers is: 100
Char is: X

Advantage of data abstraction

The major advantage of using this feature is that when the code evolves and you need to make some adjustments in the code then you only need to modify the high level class where you have declared the members as private. Since none class is accessing these data members directly, you do not need to change the low level(user level) class code.
Imagine if you had made these data members public, if at some point you want to change the code, you would have to make the necessary adjustments to all the classes that are accessing the members directly.

Other advantages of data abstraction are:
1) Makes the application secure by making data private and avoiding the user level error that may corrupt the data.
2) This avoids code duplication and increases the code reusability.

❮ PreviousNext ❯

Leave a Reply Cancel reply

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

C++ Tutorial

  • C++ Tutorial
  • First C++ Program
  • C++ Variables
  • C++ Data types
  • C++ Operators

Flow Control

  • C++ If-else
  • C++ Switch Case
  • C++ for loop
  • C++ while loop
  • C++ do-while loop
  • C++ Continue
  • C++ break
  • C++ goto

Functions

  • C++ Functions
  • Default Argument in Function
  • C++ Recursion

Arrays

  • C++ Arrays
  • Multidimensional Arrays
  • C++ Array to Function
  • C++ Strings

Pointers

  • C++ Pointers
  • "this" Pointer

OOPs Concepts

  • C++ OOPs
  • C++ Constructor
  • C++ Destructor
  • C++ Structure
  • Struct and Function
  • C++ Enum
  • C++ Inheritance
  • C++ Polymorphism
  • C++ Function Overloading
  • C++ Function Overriding
  • Function Overloading vs Overriding
  • C++ Virtual Function
  • Encapsulation
  • Abstraction
  • C++ Interfaces
  • Friend Class and Function

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap