What is encapsulation

Thread Starter

anukalp

Joined Jul 28, 2018
158
I am not experienced in c++ . I read in text book that Encapsulation means data-hiding. I have seen some of discussions but I am not clear

For Example :
C:
#include <iostream>
using namespace std;

class person
{
  public:
    string name;
    int age;
};

int main()
{
  person a, b, c, d;
  a.name = "Rock";
  b.name = "Ceena";
  c.name = "Stroke";
  d.name = "Lacner";

  a.age = 30;
  b.age = 20;
  c.age = 33;
  d.age = 22;

  cout << a.name << ": " << a.age << endl;
  cout << b.name << ": " << b.age << endl;
  cout << c.name << ": " << c.age << endl;
  cout << d.name << ": " << d.age << endl;

  return 0;
}

If I want to show the name and age of person but I want to hide the salary then I think I have to use encapsulation.

Does it demonstrate the example of encapsulation ?
C:
class person
{
  public:
    string name;
    int age;
  private:
  float salary;
};
In program how to use the encapsulation ?
 
Last edited:

WBahn

Joined Mar 31, 2012
32,746
The term "data hiding" in the context of encapsulation with regard to computer programming practices is not what you are describing here.

Think about when you use a math library function such as sin() or log(). Do you know how those functions work? More to the point, do you NEED to know how those functions work in order to use them? No. That's encapsulation -- the inner workings (data and processes) are hidden from you. When you use library routines to open and interact with a file, do you know what data has to be kept track of in order to maintain track of where you currently are in the file? No. That's encapsulation.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
The term "data hiding" in the context of encapsulation with regard to computer programming practices is not what you are describing here.
I was trying to understand the encapsulation with simple self made example. I have been reading some information there is given long big details but I don't understand what's encapsulation is in programming. so I was trying to start with myself made example

What's the wrong in my example ? Is it possible to modify the example to understand encapsulation ?
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
The term "data hiding" in the context of encapsulation with regard to computer programming practices is not what you are describing here.
I found very hard to understand encapsulation concepts with out a real and simple example

Is there a simple way to understand with program rather than reading theoretical explanation ?
 

WBahn

Joined Mar 31, 2012
32,746
I found very hard to understand encapsulation concepts with out a real and simple example

Is there a simple way to understand with program rather than reading theoretical explanation ?
Consider a description of an object like a ball. It might have certain attributes, such as position, size, velocity, and density (and perhaps others, such as elasticity and color, depending on what you are doing with it).

So in my program I might create ten balls, each with random values for position, size, and density (let's ignore the others).

Now I run a simulation of how these balls interact within a room (probably another class of object) by setting up a loop that continuously asks if any ball has collided with another ball or the walls of the room and to have them rebound accordingly. I might do this will calls like

Code:
   if (ball[i].collidesWith(ball[j]))
      ball[i].bounceOff(ball[j]);
At this level of description I don't see any of the internal attributes of a ball, such as it's position and size. Nor do I know or care how a collision is detected or how one ball bounces off another.

In the implementation of the collidesWith() method, I would likely call a distanceFrom() method and I would not know or care how the distance between two balls is calculated, just that that function returns that information.

In the bounceOff() method I likely need to know the mass of the ball and so would have a mass() method. I would not know whether the mass of the ball is an attribute stored directly as part of each object, or whether it is calculated based on size and density. That information is hidden from me, but that's fine because I don't care. Furthermore, at some point in the future how that information is stored and/or calculated might change, but that's fine because I don't care. All I care about is that when I call the mass() method that it returns the mass of the ball.
 
Top