Encapsulation in C++

Encapsulation in C++. It indicates the process of wrapping up the data and functions in a single capsule. It also safeguards the data from other classes by limiting access. Primarily it protects the data. If we take a real-world example of the university, we have different departments like Physics, Chemistry, Biology, etc. A situation may arise wherein, the Head of the Physics department needs some information from the Chemistry department, he can’t access the data from that department directly. First, he should contact the Head of the Chemistry department, then request him to give the data. This is how encapsulation works.

How to Use Encapsulation in C++?

In order to obtain this we have to follow the below steps:

  • First, we require to obtain all the data members private.
  • Then public getter and setter functions should be performed for each data member in such a way that, get function gets the value of data member and the set function sets the value of data member.

Encapsulation and data hiding can be performed in C++, by using user-defined types called Classes. The access specifiers in classes can be protected, private, or public. By default all the items in a class are private. According to the need, we can change the access levels. Three levels of access specifiers are as below:

  • Private: Members of a similar class can access the data.
  • Public: All the classes can access the data easily.
  • Protected: Access of data is allowed to members of the same class or derived classes.

The most helpful use of encapsulation is done only when we use either private or protected access specifiers. When using the public we have to make sure that, we know its proper need in the code.

To explain this we will take a look at the below class.

class Student
{
private:
double physics;
double chemistry;
double biology;
public:
double GetTotalMarks(void)
{
return physics + chemistry + biology;
}};

Explanation:

In the above sample, physics, chemistry, and biology are of the type double and are private variables. GetTotalMarks ( ) is a public process used to retrieve the total marks of all three subjects. We can’t access each subject in another class because of its security level. But we can access the process and can be used to retrieve the total marks by passing individual subject marks. We can place the marks of each subject by using the setter method, which we will look at in the next example.

#include<iostream> 
using namespace std; 
  
class Student 
{ 
    private: 
        // data hidden from outside world 
        int x;       
    public: 
        // function to set value of  
        void set(int a) 
        { 
            x =a; 
        } 
        int get()         // function to return value of 
        { 
          return x; 
        } }; 
int main()     // main function 
{ 
    Student obj; 
    obj.set(50); 
    cout<<obj.get(); 
    return 0; 
} 
Output
50

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top