Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Abstraction

In this tutorial, we will learn about abstraction in C++ with the help of examples.

Abstraction

Data abstraction is one of the most essential and important feature of object oriented programming in C++. Abstraction means displaying only essential information and hiding the details.

Data Abstraction is a programming technique that depends on the seperation of the interface and implementation details of the program.

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).


Ways to achieve Abstraction

Data Abstraction can be achieved in two ways:

  1. Abstraction using classes : We can implement Abstraction in C++ using classes. Class helps us to group data members and member functions using available access specifiers. A Class can decide which data member will be visible to outside world and which is not.
  2. Abstraction in header files : An another type of abstraction is header file.
    For example:- pow() function available is used to calculate the power of a number without actually knowing which algorithm function uses to calculate the power. Thus, we can say that header files hides all the implementation details from the user.

Abstraction using Access modifiers

Access modifiers are the main pillar of implementing abstraction in C++. We can use access specifiers to enforce restrictions on class members.

  • Members declared as public in a class, can be accessed from anywhere in the program.
  • Members declared as private in a class, can be accessed only from within the class. They are not allowed to be accessed from any part of code outside the class.

Example 1: C++ Program to demonstrate Abstraction

Example of data abstraction using classes.

#include <iostream>
using namespace std;
class abstraction {
  public:
    int a, b;

  public:
    // method to set value of private members
    void value(int x, int y) {
        a = x;
        b = y;
    }
    void display() {
        cout << "The value of a = " << a << endl;
        cout << "The value of b = " << b << endl;
    }
};

int main() {
    abstraction obj;
    obj.value(2, 4);
    obj.display();

  return 0;
}

Output

The value of a = 2
The value of b = 4

In the above example, we are not allowed to access the variables a and b directly, however one can call the function value() to set the values in a and b and the function display() to display the values of a and b.


Why Abstraction?

  • Data abstraction means hiding of data.
  • Abstraction is implemented automatically while writing the code in the form of class and object.
  • It shows only important things to the user and hides the internal details.

Example 2: C++ Program to calculate the power of a number.

Example of data abstraction in header files.

#include <iostream>
#include <math.h>
using namespace std;

int main() {

    int n = 2;
    int power = 3;
    
    // pow(n, power) is the power function
    int result = pow(n, power);

    cout << "Cube of n = " << result;

    return 0;
}

Output

Cube of n = 8

In the above example, pow() function is used to calculate 4 raised to the power 3. The pow() function is present in the math.h header file in which all the implementation details of the pow() function is hidden.


Advantages of Abstraction

  • It avoids duplication of your code..
  • It increases reusability of the code.
  • 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.
Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!