Skip to content

Latest commit

 

History

History
72 lines (56 loc) · 3.31 KB

08. Abstraction.md

File metadata and controls

72 lines (56 loc) · 3.31 KB

ABSTRACTION

Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.

Abstraction means displaying only essential information and hiding the details

Through abstraction, we focus on what an object can do (its interface) rather than how it accomplishes those tasks (its implementation).

Example

Imagine using a TV remote. You don't need to know how the remote works internally; you only need to know the buttons and their functions.

TYPES OF ABSTRACTION

  1. Data Abstraction
  2. Control Abstraction
|----------------------|---------------------------------------------------|------------------------------------------------------|
|                      |                                                   |                                                      |
| Aspect               | Data Abstraction                                  | Control Abstraction                                  |
|                      |                                                   |                                                      |
|----------------------|---------------------------------------------------|------------------------------------------------------|
| Focus                | Exposing essential data while hiding details      | Managing program execution flow and code structure   |
|----------------------|---------------------------------------------------|------------------------------------------------------|
| Purpose              | Simplify data access and manipulation             | Enhance code organization and reusability            |
|----------------------|---------------------------------------------------|------------------------------------------------------|
| Achieved by          | Encapsulating data in classes and objects         | Creating functions, methods, or procedures           |
|----------------------|---------------------------------------------------|------------------------------------------------------|
| Access               | Controls data visibility and modification         | Defines the sequence of operations                   |
|----------------------|---------------------------------------------------|------------------------------------------------------|
| Key Benefit          | Enhances data security and integrity              | Makes code modular and easy to understand            |
|----------------------|---------------------------------------------------|------------------------------------------------------|
| Example              | Class with private attributes and public methods  | Creating functions to perform specific operations    |
|----------------------|---------------------------------------------------|------------------------------------------------------|

PROGRAM

#include<iostream>
using namespace std;

class Solution
{
    int a, b;
    
public:
    void setData(int a, int b) 
    {
        this->a = a;
        this->b = b;
    }
    
    int getData() 
    {
        return (a + b);
    }
};

int main()
{
    Solution obj;
    
    obj.setData(2, 3);
    
    cout << obj.getData(); // Outputs the sum of a and b
    
    return 0;
}

DIFFERENCE BETWEEN ENCAPSULATION AND ABSTRACTION

https://www.geeksforgeeks.org/difference-between-abstraction-and-encapsulation-in-c/