Abstraction
Data abstraction is one of the most essential and important features of object-oriented programming in C++. Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.
Advantages of Abstraction
i) Helps the user to avoid writing the low-level code
ii) Avoids code duplication and increases reusability.
iii) Can change the internal implementation of the class independently without affecting the user.
iv) Helps to increase the security of an application or program as only important details are provided to the user.
v) It reduces the complexity as well as the redundancy of the code, therefore increasing the readability.
Some Ways of achieving Abstraction
1) Abstraction using Classes
We can implement Abstraction in C++ using classes. The 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 the outside world and which is not.
2) Abstraction in Header files
One more type of abstraction in C++ can be header files. For example, consider the POW () method present in math.h header file. Whenever we need to calculate the power of a number, we simply call the function pow() present in the math.h header file and pass the numbers as arguments without knowing the underlying algorithm according to which the function is actually calculating the power of numbers.
3) Abstraction using Access Specifiers
Access specifiers are the main pillar of implementing abstraction in C++. We can use access specifiers to enforce restrictions on class members.
For example: 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.
We can easily implement abstraction using the above two features provided by access specifiers.
Say, the members that define the internal implementation can be marked as private in a class.
And the important information needed to be given to the outside world can be marked as public. And these public members can access the private members as they are inside the class.
Example: For achieving Abstraction
#include <iostream>
using namespace std;
class implementAbstraction {
private:
int a, b;
public:
// method to set values of
// private members
void 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;
}
Encapsulation
Encapsulation in C++ is defined as the wrapping up of data and information in a single unit. In Object Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them.
Features of Encapsulation
1) We cannot access any function from the class directly. We need an object to access that function that is using the member variables of that class.
2) The function which we are making inside the class must use only member variables, only then it is called encapsulation.
3) If we don’t make a function inside the class which is using the member variable of the class then we don’t call it encapsulation.
4) Encapsulation improves readability, maintainability, and security by grouping data and methods together.
5) It helps to control the modification of data members.
Two Important property of Encapsulation
1) Data Protection: Encapsulation protects the internal state of an object by keeping its data members private. Access to and modification of these data members is restricted to the class’s public methods, ensuring controlled and secure data manipulation.
2)Information Hiding: Encapsulation hides the internal implementation details of a class from external code. Only the public interface of the class is accessible, providing abstraction and simplifying the usage of the class while allowing the internal implementation to be modified without impacting external code.
Role of Access Specifiers in Encapsulation
Access specifiers facilitate Data Hiding in C++ programs by restricting access to the class member functions and data members.
There are three types of access specifiers in C++:
1) Private: Private access specifier means that the member function or data member can only be accessed by other member functions of the same class.
2) Protected: A protected access specifier means that the member function or data member can be accessed by other member functions of the same class or by derived classes.
3) Public: Public access specifier means that the member function or data member can be accessed by any code.
Note: By default, all data members and member functions of a class are made private by the compiler.
Example 1: For Encapsulation
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(string name, int age) {
this->name = name;
this->age = age;
}
void setName(string name) {
this->name = name;
}
string getName() {
return name;
}
void setAge(int age) {
this->age = age;
}
int getAge() {
return age;
}
};
int main() {
Person person(“John Doe”, 30);
cout << “Name: ” << person.getName() << endl;
cout << “Age: ” << person.getAge() << endl;
person.setName(“Jane Doe”);
person.setAge(32);
cout << “Name: ” << person.getName() << endl;
cout << “Age: ” << person.getAge() << endl;
return 0;
}
Example 2: For Encapsulation
#include <iostream>
using namespace std;
class Encapsulation {
private:
// Data hidden from outside world
int x;
public:
// Function to set value of
// variable x
void set(int a) { x = a;
}
// Function to return value of
// variable x
int get()
{
return x;
}
};
// Driver code
int main()
{
Encapsulation obj;
obj.set(5);
cout << obj.get();
return 0;
}