Data Hiding and Encapsulation Using Access Specifiers in C++ - C++ Programming Concepts
Data Hiding and Encapsulation Using Access Specifiers in C++ - C++ Programming Concepts
Encapsulation De nition:
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 De nition:
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.
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.
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.
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
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”.
Encapsulation is used to reduce the complexity and improve reusability.
Encapsulation de nes the interface clearly which improves the readability and understandability.
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.
Classes in C++
4 Simple Class Example Program For Find Prime Number In C++ (/c-programs/c-c-class-example-programs/Simple-Class-
Example-Program-For-Find-Prime-Number-In-C)
10 Simple Program for Inline Function without Class Using C++ Programming (/c-programs/simple-program-for-inline-function-
using-c-programming)
15 Simple Example Program for Inline Function Using C++ Programming (/c-programs/c-function-example-programs/simple-
example-program-for-inline-function-using-c-programming)
20 Simple Program for Static Data and Member Function Using C++ Programming (/c-programs/simple-program-for-static-data-
and-member-function-using-c-programming)
21 Simple Program for Unary Operator Overloading Using C++ Programming (/c-programs/simple-program-for-unary-operator-
overloading-using-c-programming)
23 Simple Program for Binary Operator Overloading Using C++ Programming (/c-programs/simple-program-for-binary-operator-
overloading-using-c-programming)
Queries
02Uoph5AAc3rq9ZzZjVWaNScw/viewform)
Contact us
[email protected] (mailto:[email protected])
Android Apps
PostgreSQL (https://play.google.com/store/apps/details?id=com.thiyagaraaj.postgresql)
Perl (https://play.google.com/store/apps/details?id=com.thiyagaraaj.perl)
Android Apps
© 2007-2020 All Rights are reserved. | WWW.LITTLEDROPS.NET (http://www.littledrops.net) and WWW.THIYAGARAAJ.COM (http://www.thiyagaraaj.com)