Professional Documents
Culture Documents
ref=lbp
Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python
Encapsulation in C++
Difficulty Level : Easy ● Last Updated : 24 Nov, 2021
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
Got It !
1 of 6 12-02-2022, 07:11 pm
Encapsulation in C++ - GeeksforGeeks https://www.geeksforgeeks.org/encapsulation-in-c/?ref=lbp
#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;
}
2 of 6 12-02-2022, 07:11 pm
Encapsulation in C++ - GeeksforGeeks https://www.geeksforgeeks.org/encapsulation-in-c/?ref=lbp
};
// main function
int main()
{
Encapsulation obj;
obj.set(5);
cout<<obj.get();
return 0;
}
output:
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.
3 of 6 12-02-2022, 07:11 pm
Encapsulation in C++ - GeeksforGeeks https://www.geeksforgeeks.org/encapsulation-in-c/?ref=lbp
Want to learn from the best curated videos and practice problems,
check out the C++ Foundation Course for Basic to Advanced C++ and
C++ STL Course for foundation plus STL. To complete your preparation
from learning a language to DS Algo and many more, please refer
Complete Interview Preparation Course.
Like 221
Previous Next
R ECO M M E N D E D A RT I C L E S Page : 1 2
4 of 6 12-02-2022, 07:11 pm
Encapsulation in C++ - GeeksforGeeks https://www.geeksforgeeks.org/encapsulation-in-c/?ref=lbp
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
Got It !
5 of 6 12-02-2022, 07:11 pm
Encapsulation in C++ - GeeksforGeeks https://www.geeksforgeeks.org/encapsulation-in-c/?ref=lbp
Article Contributed By :
GeeksforGeeks
Contact Us CS Subjects
CSS Internships
JavaScript Videos
Bootstrap
Got It !
6 of 6 12-02-2022, 07:11 pm