In this blog post tutorial, you will learn about encapsulation in C++ with the help of programming examples. I will describe each small point related to encapsulation because encapsulation is one of the key features of object-oriented programming. This blog post on encapsulation will be helpful for both beginners and experienced. So let’s start this blog post with a question “what is encapsulation in C++”?
What is encapsulation in C++?
Encapsulation is one of the four basic techniques of OOP (Object Oriented Programming) that are Inheritance, Abstraction, Polymorphism, and Encapsulation. Encapsulation prevents unauthorized access to some piece of information or functionality.
In object-oriented programming (OOP), encapsulation refers to the bundling of data with the functions (methods) that operate on that data. In C++, we can bundle data members and functions that operate together inside a single class.
If someone hands us a class, we do not need to know how it actually works to use it; all we need to know about is its interface ( public functions/attributes).
This is often compared to the vending machine: where you don’t need to know the internal working of the vending machine to operate with it. You just need to care about the interfaces (buttons) provided by the vending machine to accomplish your goal without knowing the implementation details.
Let’s see an example where I am describing how to bundle data and function together using the class.
#include<iostream> using namespace std; class Test { // data hidden from outside world int x; public: // Get value of x int getX() { return x; } //Set value of x void setX(int data) { x = data; } }; int main() { Test obj; //changing data safely //with allowed interface obj.setX(50); cout<<obj.getX(); return 0; }
Output:
50
In the above program, the method getX() returns the value of x, and method setX() sets the value to the variable x. And the data member x and methods getX() and setX() are kept together in the Test class.
You can see how the class Test
realize the concept of encapsulation, because it presents to the outside only the outer shell in form of the operations (getX
and setX
methods), while the internal data and internal implementation remain encapsulated inside the shell.
In C++ for a single class, the volatile part is normally encapsulated using the private and/or protected keywords. These are access specifiers. C++ supports three access specifiers that you can use to define the visibility of classes, methods, and attributes.
private:
A member (either data member or member function) declared in a private section of a class can only be accessed by member functions and friends of that class.
class Test { private: // Access only by member functions //and friends of that class int data; };
protected:
A member (either data member or member function) declared in a protected section of a class can only be accessed by member functions and friends of that class, and by member functions and friends of derived classes.
class Test { protected: //Access by member functions and friends of that class, //and by member functions and friends of derived classes. int data; };
public:
A member (either data member or member function) declared in a public section of a class can be accessed by anyone.
class Test { public: //Access by anyone int data; };
Why encapsulation is an important feature of OOPs?
We have already described encapsulation with some real-life examples but here we will discuss the importance of encapsulation. I believe what you have understood is that encapsulation is a way to achieve “information hiding”.
Encapsulation is important because it helps to separate the implementation details from the behavior exposed to clients of a class (other classes/functions that are using this class), and gives you more control over coupling in your code.
For example, you don’t need to understand the internal working of the mobile phone to operate with it. You have interfaces to control the behavior of the mobile without knowing its implementation details. These interfaces restrict the unwanted configuration changes and protect from undefined behavior.
In C language, we can achieve encapsulation by making things static in a compilation unit or module. This prevented another module from accessing the static stuff. We will talk about it later in detail.
Recommended Articles for you:
- C++ Programming Courses And Tutorials.
- Polymorphism in C++ for example.
- Compile Time Polymorphism with Templates in C++.
- Operator Overloading in C++ with some FAQ.
- Introduction of reference in C++.
- Use of mutable keywords in C++.
- Best electronic kits for programmers.
- References and const in C++ with example programs.
- C++ Interview Questions with Answers.
- List of some Best C++ Books, you must see.