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:
- 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.
- 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.
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Abstraction in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Templates
.