Advertisement

Encapsulation in C++ - Code with abuzar

 

In this we will learn about Encapsulation in C++

 

Encapsulation in C++


Encapsulation is a method that is used in object oriented programming . Encapsulation is

 defined as wrapping of data and function in single unit is known as encapsulation .

For real based example

 

   Consider a real life example of encapsulation in C++ , in a company there are different

 type of section like sales section , account section and manufacture section similarly the

 sales section handle all the sales related activities and hold all the record . account section

 handle the accounts of the company and manufacture handle when product is is

 manufactured . Now there may arise a situation when for some reason and official from

 manufacture section needs all the data about sales section in a particular month . In this

 case , he is not allowed to direct access the data of sales section . He will have to contact

 first for the some official officers from the sales section and request him to give the

 particular data  . Encapsulation is used in this process .

Encapsulation is also used for data hiding . In this above example data of any section like ,

 sales , account , manufacture hidden form any other section .


Example of Encapsulation

#include<iostream>
Using namespace std ;

Class encapsulation{
    Private :

// data hidden from outside the world !
     Int x ; 
Public :
Void setint x  ){
x = a ;
}
Int get ()
{
Return x ;
}
}; 
Int main( ){
// Object Created 
Encapsulation obj ;
Obj.set(5);
Cout <<obj.get();
Return 0 ;
}

Output

5


Please ! Write comment if you find anything incorrect in this website

This article is contributed by Mohd Abuzar . If you like code with abuzar or want to contribute with us so mail at - abuzaar.abuzar.mohd325@gmail.com 

 

 

 

Post a Comment

0 Comments