What is encapsulation? | c++ Advanced | Part – 8

What is encapsulation

Imagine you have a water bottle which is filled with water upto the brim.

There are two marbles present inside the water bottle.

Can you pull these marbles out without spilling the water?

What if you are given a pair of tongs?

You can, right?

Encapsulation is a concept which describes the idea of bundling data member and member functions together.

It brings the idea of data hiding to the table.

The variables in the class can only be accessed through the functions declared inside the class.

The data members are declared as private.

And the member functions to access them as public so that they can be accessed from outside the class.

These functions are used to set or get the values of the variables.

Encapsulation provides a protected space for data members and restricts access outside the class.

Encapsulation is bundling data members and member functions together in order to restrict the access to the variables outside the class.

Let’s see how we can achieve encapsulation in C++.

class OhAbstraction{

private:

string name; //private member of the class

public:

void getName(string newName){

//public member to set the value

name = newName;

}

void showName(){

//public member to display the value

cout<<name;

}

};

In the above example, we can see that name is declared as private and thus it cannot be accessed outside the class.

But the functions getName() and showName() are declared as public.

Thus, these functions can be accessed from outside the class.

We can call these functions and pass the values of newName which is then assigned to the name data member.

Thus, we cannot change the values of the data members without accessing the member functions.

class OhAbstraction{

private:

string name;

public:

void getName(string newName){

name = newName;

}

};

int main() {

OhAbstraction oh;

oh.getName(“Rahul“); //Calling member function getName()

}

Here we are calling the function getName() and setting the value of the variable name.

The variable name is declared as private and hence cannot be accessed outside the class.

Thus, we have public member functions getName() to assign values to the data member.

Here, we have bundled data members and member functions together, which is nothing but encapsulation.

Let’s try to define another function which displays the value of name.

class OhAbstraction{

private:

string name;

public:

void getName(string newName){

name = newName;

}

void showName(){

cout<<name;

}

};

int main() {

OhAbstraction oh;

oh.getName(“Rahul“); //Calling member function getName()

oh.showName(); //Calling member function showName()

}

Can you guess the output of the above snippet?

Here we have a function showName() which displays the value of variable name.

After we call the getName() function with “Rahul” as the parameter, we are calling the showName() method.

Thus, the output of the snippet will be

Rahul

Encapsulation vs Abstraction

Encapsulation is about hiding data whereas abstraction focuses on hiding the implementation.

Abstraction is achieved by encapsulation whereas encapsulation is achieved by making the data of the system private.

Abstraction focuses on hiding the background details and focusing on important stuff whereas encapsulation is about bundling data members and member functions together.

To summarize

  • Encapsulation is a concept that puts forth the idea of bundling data members and member functions together.
  • The data members are declared as private.
  • And the member functions to access them as public so that they can be accessed from outside the class.
  • Encapsulation provides a protected space for data members and restricts access from outside the class.

Recommended Books For Java

Leave a Reply

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