0% found this document useful (0 votes)
33 views

Encaspsulation - CPP HW

Uploaded by

shivani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Encaspsulation - CPP HW

Uploaded by

shivani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

// c++ program to explain

// Encapsulation
  
#include<iostream>
using namespace std;
  
class Encapsulation
{
    private:
        // data hidden from outside world
        int x;
          
    public:
        // function to set value of 
        // variable x
        void set(int a)
        {
            x =a;
        }
          
        // function to return value of
        // variable x
        int get()
        {
            return x;
        }
};
  
// main function
int main()
{
    Encapsulation obj;
      
    obj.set(5);
      
    cout<<obj.get();
    return 0;
}
EXPLAINATION : In the above program the variable x is made private. This variable can be accessed
and manipulated only using the functions get() and set() which are present inside the class. Thus we
can say that here, the variable x and the functions get() and set() are binded together which is
nothing but encapsulation.

You might also like