Data Hiding and Encapsulation using Access Specifiers in C++
Encapsulation Definition:
Encapsulation is a process of capsulation of data and methods into a combined single unit. In C++, encapsulation is used along with the classes concept.
Bundling of data and methods (functions) as a single unit is known as encapsulation.
Encapsulation represents the information of variables (attributes) in terms of data and, methods (functions) and its operations in terms of purpose.
- Encapsulation is the process of combining data and function into a single unit called class.
- Encapsulation is a powerful feature that leads to information hiding and abstract data type.
- They encapsulate all the essential properties of the object that are to be created.
- Using the method of encapsulation the programmer cannot access the class directly.
Data Hiding Definition:
Data hiding is a technique especially practised in object-oriented programming (OOP).Data hiding is hiding the details of internal data members of an object.
- Data hiding is also known as Information hiding.
- Sometimes Data Hiding includes Encapsulation. Thus Data Hiding is heavily related to Abstraction and Encapsulation.
- Data Hiding is the one most important OOP mechanism. Which is hide the details of the class from outside of the class.
- The Class used by only a limited set of variables and functions, others are hidden by the class.
Access Specifiers in C++
Access specifiers define how a member's variables and member's functions of a class can be accessed from outside the class. However, all members of a class can be accessed from within the class without any restriction.
Class members can be declared as public, protected or private access specifiers, which is used to build the encapsulation capabilities of the class.
There are three access specifiers.
- Private members: These can be accessed only from within the members of the same class.
- Protected members: These can be accessed only from within other members of the same class and its derived classes.
- Public members: These can be accessed from anywhere where the object is accessible.
By declaring the member variables and functions as a private in a class, the members are hidden from outside the class.Those private members and data cannot be accessed by the object directly.
Encapsulation General Form:
class class_name {
private:
datatype member_variables;
datatype member_functions;
public:
datatype member_variables;
datatype member_functions;
};
main() {
class_name objectname1, objectname;
}
Encapsulation Example:
class Square {
private:
int Num;
public:
void Get() {
cout << "Enter Number:";
cin>>Num;
}
void Display() {
cout << "Square Is:" << Num*Num;
}
};
void main() {
Square Obj;
Obj.Get();
Obj.Display();
getch()
}
In the above example, the variable “Num” is private. Hence this variable can be accessed only by the members of the same class and is not accessible anywhere else. Hence outside the classes will be unable to access this variable Which is called data hiding.
At the same time, “Square” class contains two other methods namely “Get” and “Display” which has public members. Here “Get” method just prints the value while “Display” method prints the square of the value in the variable “Num”. Here the class “Square” implements Data Encapsulation concept by capsulation of the value in the variable “Num” and thus allowing the user only to perform a restricted set of operations on the hidden variable “Num”.
Features and Advantages of Data Encapsulation:
- Encapsulation is used to reduce the complexity and improve reusability.
- Encapsulation defines the interface clearly which improves the readability and understandability.
- Encapsulation helps to hide important data and ensures enhanced Security.
- The member variables and members are bundled into a single unit as a class which makes the maintenance easy.
- The encapsulated classes are straightforward and are easy to manage and improves the future development of the application.
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming