- 'Encapsulation' as the term suggests Encapsulating
things(functions and data) inside a class(entity)
- Encapsulation is an OOPs(object oriented programming) concept which is used not just in C++ but in almost every modern programming languages
- Encapsulation
is like welding of code and data together into objects
-
Encapsulation secures the data and prevents direct access of data in the class
from members outside the class
Code Example
#include <iostream.h>
class Plus
{
private: int a,b,c;
public: int
Addition(int a, int b)
{
c= a+b;
return c;
}
void show( )
{
cout << "The sum
is::" << c << "\n";
}
}d;
void
main()
{
Plus d;
d.Addition(10,
4);
d.show();
}
Result: The sum is:: 14
In the
above encapsulation example the integer values "a,b,c" of the class "Plus"
can be accessed only through the function "Addition". These integer values are encapsulated inside the class "Plus".
No comments:
Post a Comment