Login    New User    Forgot Password    Help      

Encapsulation

GO TO INDEX

Encapsulation

Encapsulation is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. Data is only accessible through the functions existing inside the class. Data encapsulation led to the important concept of data hiding. Data hiding is the implementation details of a class that are hidden from the user. The concept of restricted access led programmers to write specialized functions or methods for performing the operations on hidden members of the class. Attention must be paid to ensure that the class is designed properly.

Neither too much access nor too much control must be placed on the operations in order to make the class user friendly. Hiding the implementation details and providing restrictive access leads to the concept of abstract data type. Encapsulation leads to the concept of data hiding, but the concept of encapsulation must not be restricted to information hiding. Encapsulation clearly represents the ability to bundle related data and functionality within a single, autonomous entity called a class.

Example

Sample Code
class Exforsys
{
public:
int sample();
int example(char *se) 
int endfunc();
...
...  //Other member functions
 
private:
int x;
float sq; 
... 
...  //Other data members
};



 

Access functions

An access function is a short public function whose job is to return the value of a private member variable.

 

Example

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}

Output

The result is 8

GO TO INDEX