You are on page 1of 6

Encapsulation in C++ - GeeksforGeeks https://www.geeksforgeeks.org/encapsulation-in-c/?

ref=lbp

Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python

Encapsulation in C++
Difficulty Level : Easy ● Last Updated : 24 Nov, 2021

In normal terms Encapsulation is defined as wrapping up of data and


information under a single unit. In Object Oriented Programming,
Encapsulation is defined as binding together the data and the functions
that manipulates them.
Consider a real life example of encapsulation, in a company there are
different sections like the accounts section, finance section, sales
section etc. The finance section handles all the financial transactions
and keep records of all the data related to finance. Similarly the sales
section handles all the sales related activities and keep records of all
the sales. Now there may arise a situation when for some reason an
official from finance section needs all the data about sales in a
particular month. In this case, he is not allowed to directly access the
data of sales section. He will first have to contact some other officer in
the sales section and then request him to give the particular data. This is
what encapsulation is. Here the data of sales section and the employees
that can manipulate them are wrapped under a single name “sales
section”.

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

Encapsulation also lead to data abstraction or hiding. As using


encapsulation also hides the data. In the above example the data of any
of the section like sales, finance or accounts is hidden from any other
section.

In C++ encapsulation can be implemented using Class and access


modifiers. Look at the below program:

// 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
We use cookies toint
ensureget()
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
{
return x; Got It !
}

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.

Role of access specifiers in encapsulation

As we have seen in above example, access specifiers plays an important


role in implementing encapsulation in C++. The process of
implementing encapsulation can be sub-divided into two steps:

1. The data members should be labeled as private using the private


access specifiers
2. The member function which manipulates the data members should
be labeled as public using the public access specifier

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks


and would like to contribute, you can also write an article using
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
write.geeksforgeeks.org or mail your article to review-
Got It !

3 of 6 12-02-2022, 07:11 pm
Encapsulation in C++ - GeeksforGeeks https://www.geeksforgeeks.org/encapsulation-in-c/?ref=lbp

team@geeksforgeeks.org. See your article appearing on the


GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

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

Polymorphism in C++ Abstraction in C++

R ECO M M E N D E D A RT I C L E S Page : 1 2

01 Difference between Abstraction


and Encapsulation in C++
05 C++ Program to Find the Mth
element of the Array after K left
16, Oct 19 rotations
We use cookies to ensure you have the best browsing experience on our23, Jan 22By using our site, you acknowledge
website.
that you have read and understood our Cookie Policy & Privacy Policy

02 Packaged Task | Advanced Got


C++It !
06 C++ Program to Count of Array

4 of 6 12-02-2022, 07:11 pm
Encapsulation in C++ - GeeksforGeeks https://www.geeksforgeeks.org/encapsulation-in-c/?ref=lbp

(Multithreading & elements greater than all


Multiprocessing) elements on its left and at least
31, Jan 22 K elements on its right
21, Jan 22

03 C++ Program to Find Mth


element after K Right Rotations 07 C++ Program to Count of
of an Array rotations required to generate
25, Jan 22 a sorted array
20, Jan 22

04 Different ways to access


characters in a given String in
08 C++ Program to Print all
possible rotations of a given
C++ Array
24, Jan 22 20, Jan 22

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

Vote for di�culty


Current di�culty : Easy 5th Floor, A-118,
Sector-136, Noida, Uttar Pradesh - 201305

Easy Normal Medium Hard Expert


feedback@geeksforgeeks.org

Article Tags : cpp-class, C++

Practice Tags : CPP


Company Learn
About Us Algorithms

Careers Data Structures


Improve Article Report Issue
Privacy Policy Languages

Contact Us CS Subjects

Copyright Policy Video Tutorials

Web Development Contribute


Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Web Tutorials Write an Article
Load Comments
HTML Write Interview Experience

CSS Internships

JavaScript Videos

Bootstrap

@geeksforgeeks , Some rights reserved


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 !

6 of 6 12-02-2022, 07:11 pm

You might also like