Encapsulation in C++.

Encapsulation: Encapsulation is process of wrapping of data and method in single unit. 

Encapsulation provides abstraction between an object and its clients. It helps to protect an object from unwanted access by clients.

Encapsulation is a powerful feature that leads to information hiding,abstract data type and friend function.

 

Note: Encapsulation is one of the way to achieve abstraction.

Let’s write a program of encapsulation to add two number.

//Code

 

 

#include<iostream.h>

#include<conio.h>

class bca

{

long int a,b,sum;

public:

void add()

{

cout<<”enter first number:”;

cin>>a;

cout<<”enter second number:”;

cin>>b;

sum=a+b;

cout<<” \n sum of two numbers is:”<<sum;

}

};

void main()

{

clrscr();

bca t;

t.add();

getch();

}

 

 

 

Run the program and see output.

 

 

 

Output:

 

 

 

OUTPUT

 

 

Happy coding.

 

 

 

Tags: , , ,