- 0shares
- Facebook0
- Twitter0
- Google+0
- Pinterest0
- LinkedIn0
Encapsulation:
Encapsulation is an object oriented programming concept that hides the personal data from outside world using the access specifiers; private and protected. Encapsulation basically means to protect the data in a class using private and protected. Data encapsulation is also called data hiding.
Data hiding is where we do not want the user to have the access of the back end details or the details of the background of the program. To demonstrate the concept of data hiding consider the following example:
class rectangle
{
private:
int length;
int width;
public:
void set ();
void print ();
int area ();
};
In the above example, the fragment of the program is divided into two parts that is data members and the member functions. The data members are kept private, this is so because we do not want the user to interfere or access this data. However, the member functions are public.
This means that the member functions can be accessed or used by the user. In parent classes the data members are protected. Consider the following example:
class shape
{
protected:
int l;
int w;
public:
shape ();
shape (int x, int y);
void print ();
};
Difference between private and protected access specifiers
The protected data members or methods are only visible to child classes or subclasses that are implemented inheritance whereas private methods are only visible to the class variables.
Note that the class variables are also known as instance variables.
Data encapsulation is used where the implementation of the class changes but the interface remains the same. This is simplified using the stack class in which we use push() and pop() member functions. Stack is simply an undo function. It is as follows:
# include <iostream>
using namespace std;
# include <conio.h>
class stack
{
private:
int array [5];
int top;
public:
stack ();
void push (int d);
int pop ();
};
stack :: stack ()
{
array [top] =0;
}
void stack :: push (int d)
{
array [top] =d;
top++;
}
int stack :: pop ()
{
–top;
return array [top];
}
void main ()
{
stack s;
- push (1);
- push (2);
- push (3);
cout << s.pop ();
cout << s.pop ();
getche ();
}
Working of the above program:
The above program follows the concept of undo that is first user enters data and then pops out in back direction. There are three member functions; stack () is the constructor used to initialize the values of data members to zero to avoid garbage values.
The function void push (int d) is used to enter the data. Now take a look on the main () function the line s. push (1) means that the function is called using the object and 1 is passed. 1 is stored in int d. the top of array or array [top] is at zero location, when 1 is passed, the zero location of array has 1 value. After this the value of top is incremented, it means that now the location of array [top] is 1.
Again through main function (2) is passed, and this process is repeated thrice. Now the three locations of arrays have three values 1, 2 and 3 respectively.
After the push function, pop () function is called in the cout statement. The control is transferred to pop () it decrements the value of top and returns the value of array[top] which has the value 3. This value (3) is printed on the screen and the process is continued.
This concept is called FILO. FILO stands for First in Last out.