All That You Need To Know About Abstraction In C++.
In this C++ series of tutorials, we will learn all the major concepts of Object Oriented programming in C++ which are known as the four pillars of OOP.
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
In this tutorial, we will explore all about data abstraction in C++.
=> Click Here For The Absolute C++ Training Series.
Table of Contents:
What Is Abstraction?
Abstraction is a technique of object-oriented programming using which we hide the details of implementation from the user and only expose an interface that is required.
We can take a real-life example of an Air conditioner (AC). We have a remote control in order to control the various AC functions like start, stop, increase/decrease the temperature, control humidity, etc. We can control these functions just with a clock of a button but internally there is a complex logic that is implemented to perform these functions.
However, as an end user, what we exposed to is only the remote interface and not the implementation details of all these functions.
Abstraction is one of the four pillars of object-oriented programming and almost all the OOP solutions are based on the abstraction principles i.e. separation of interface and implementation details in the program.
In the above diagram, we have shown an object and its contents as a pictorial representation. We can see that the innermost layer of this object is its core functionality followed by the implementation details.
In OOP, these two layers (though in most of the cases this is just one layer) are not exposed to the outside world. The outermost layer, the interface, is the layer that is provided for the end user in order to access the functionality of the object.
As a result, any changes made to the innermost layers of the object are not evident to an end user as long as the interface that the user is exposed to remains the same.
Implementation Of Abstraction In C++
C++ supports abstraction at a great depth. In C++ even the library functions that we use can be considered as an example of abstraction.
We can represent the implementation of abstraction in C++ as follows:
As shown in the above figure, we can implement abstraction in C++ in two ways:
#1) Using Classes and Access Specifiers
A class implemented in C++ with access specifiers public, private and protected, can be considered as an implementation of abstraction.
As we already know, the use of access specifiers allows us to control the access given to the class members. We can make some members private so that they are inaccessible outside the class. We can have some members as protected so that they are accessible only to the derived classes. Lastly, we can make some members public so that these members are accessible outside the class.
Using this notion, we can implement abstraction in such a way that the implementation details are hidden from the outside world by using private access specifier while the interface can be exposed to the outside world using the public specifier.
Hence, we can implement abstraction in C++, using a class to bundle data and functions into a single unit and using access specifiers to control access to these data and functions.
Let us implement the following Example to demonstrate this.
#include <iostream> #include <string> using namespace std; class sample { int num1,num2; void readNum(){ cout<<"Enter num1 : "; cin>>num1; cout<<"\nEnter num2 : "; cin>>num2; } public: void displaySum() { readNum(); cout<<"\nSum of the two numbers = "<<num1+num2<<endl; } }; int main() { sample s; s.displaySum(); }
Output:
Enter num1 : 10
Enter num2 : 20
Sum of the two numbers = 30
In the above program, we have a sample class which has two integer variables, num1 and num2. It also has two functions readNum and displaySum. The data members’ num1 and num2, as well as the function readNum, are private to the class.
The function displaySum is public to the class. In the main function, we create an object of class sample and call displaySum that reads the two numbers as well as prints their sum.
This is the abstraction implementation. We expose only one function to the public while keeping the other data members and functions under wraps. Though this is just an example to demonstrate abstraction, while implementing real-life problems, we can have many levels of abstraction in C++.
#2) Using Header File Implementation
We use header files in C++ program to import and use predefined functions. For this, we use the #include directive to include header files in our program.
For Example, in the above program, we have used the functions cin and cout. As far as these functions are concerned, we only know how to use them and what are the parameters they take.
We do not know what goes on in the background when these functions are called or how they are implemented in the iostream header file. This is another way of abstraction provided by C++.
We do not know the details of the implementation of all the functions that we import from the header files.
Here is another Example to demonstrate Abstraction.
#include <iostream> #include <string> using namespace std; class employee{ int empId; string name; double salary,basic,allowances; double calculateSalary(int empId){ salary = basic+allowances; return salary; } public: employee(int empId, string name,double basic,double allowances): empId(empId),name(name),basic(basic),allowances(allowances){ calculateSalary(empId); } void display(){ cout<<"EmpId = "<<empId<<"\tName = "<<name<<endl; cout<<"Employee Salary = "<<salary; } }; int main() { employee emp(1,"Ved",15000,3245.43); emp.display(); }
Output:
EmpId = 1 Name = Ved
Employee Salary = 18245.4
In this example, we have defined a class employee which has private details like empId, name, salary details like basic and allowances. We also define a private function “calculateSalary” which calculates the salary using basic and allowances.
We have a constructor to initialize all the data for a particular employee object. We also call the function “calculateSalary” from the constructor to calculate the salary of the current employee.
Next, we have a “display” function that displays empId, name, and salary. In the main function, we create an object of class employee and call the display function.
We can clearly see the level of abstraction that we have provided in this program. We have hidden all the employee details as well as calculateSalary function from the user by making them private.
We have exposed only one function display to the user that gives all information about employee object to the user and at the same time, it also hides details like private data and how we calculate the salary of the employee.
By doing this, in the future, if we want to add more details and change the way in which the salary is calculated, we don’t have to change the display function. The user will be unaware of these changes.
Advantages Of Abstraction
Enlisted below are some of the advantages of abstraction.
- The programmer need not write low-level code.
- Abstraction protects the internal implementation from malicious use and errors.
- Abstraction can prevent code duplication and thus the programmer needs to perform the same tasks over and over again.
- Abstraction promotes code reuse and properly classifies the class data members.
- The Programmer can change the internal details of the class implementation without the knowledge of end-user thereby without affecting the outer layer operations.
Conclusion
Abstraction is one of the most important concepts in OOP and is implemented at a great depth in C++. Using abstraction, we can keep the implementation details of the program under wraps and only expose the details that we want to the outside world.
By using the abstraction concept, we can design abstract data types and classes that act as a skeleton to the programming solution on top of which an entire solution is built. As we progress through the OOP topics, we will learn more about these types and classes.
In our upcoming tutorials, we will learn about another pillar of OOP i.e. Encapsulation. We will also weigh abstraction and encapsulation together there.
=> Look For The Entire C++ Training Series Here.