Encapsulation - OOPs concept in C++

How is Encapsulation used in OOP's concept of C++?
What is Data Abstraction?

Explanation

Encapsulation is the method of combining the data and functions inside a class. This hides the data from being accessed from outside a class directly, only through the functions inside the class is able to access the information.

This is also known as "Data Abstraction", as it gives a clear separation between properties of data type and the associated implementation details. There are two types, they are "function abstraction" and "data abstraction". Functions that can be used without knowing how its implemented is function abstraction. Data abstraction is using data without knowing how the data is stored.

Example :


#include <iostream.h> class Add
{
private:
int x,y,r;
public:
int Addition(int x, int y)
{
r= x+y;
return r;
}
void show( )
{ cout << "The sum is::" << r << "\n";}
}s;
void main()
{
Add s;
s.Addition(10, 4);
s.show();
}

Result :

The sum is:: 14

In the above encapsulation example the integer values "x,y,r" of the class "Add" can be accessed only through the function "Addition". These integer values are encapsulated inside the class "Add".

C++ Tutorial


Ask Questions

Ask Question