Abstraction
Anonymous contributor
Anonymous contributor2 total contributions
Anonymous contributor
Published Oct 4, 2023
Contribute to Docs
Abstraction involves displaying only essential information while concealing underlying details.
Data abstraction refers to providing only data-related information to the user, hiding additional information that is not needed.
Access Specifiers
Abstraction can be implemented by using access specifiers to control the accessibility of members within a class:
- 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 the code outside the class.
// C++ Program to demonstrate abstraction#include <iostream>using namespace std;class implementAbstraction {private:int a, b;public:// Method to set values of// private membersvoid set(int x, int y) {a = x;b = y;}void display() {cout << "a = " << a << endl;cout << "b = " << b << endl;}};int main() {implementAbstraction obj;obj.set(10, 20);obj.display();return 0;}
The output will be:
a = 10b = 20
Advantages of Data Abstraction
Data abstraction has several advantages including:
- Simplicity: Helps the user to avoid writing the low-level code.
- Reusability: Avoids code duplication and increases reusability.
- Modularity: Allows for changes to the internal implementation of the class independently without affecting the user.
- Security: Enhances the security of an application or program by only providing essential details to the user and keeping sensitive information hidden.
- Readability: Reduces the complexity as well as the redundancy of the code, therefore increasing the readability.
All contributors
- Anonymous contributorAnonymous contributor2 total contributions
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.