C++ language

adplus-dvertising
Encapsulation IN C++
Previous Home Next

Encapsulation is the technique of adjoining the data and functions within a class. That secretes the data from being execusion from without a class outright, only via the functions within the class is samarth to execution the details.

That is also Conversant as "Data Abstraction", as it endues a apparent severance between cretic of data type and the accessory execution information. There are 2 types, function abstraction and data abstraction. Functions it may be used outside knowing how its execution is function abstraction. Data abstraction is using data outside knowing how the data is stored.

"Encapsulation is an attribute of object design. That means that all of the object's data is implied and invisible in the object and penetration to it conditioned to members of that class."

Example:-

#include <iostream>
using namespace std;
class Adder{
public:
                  // constructor
Adder(int i = 0)
{
total = i;
}
                 // interface to outside world
void addNum(int number)
{
total += number;
}
              // interface to outside world
int getTotal()
{
return total;
};
private:
                  // hidden data from outside world
int total;
};
int main( )
{
Adder a;

a.addNum(5);
a.addNum(10);
a.addNum(50);

cout << "Total " << a.getTotal() <<endl;
return 0;
}

Making 1 class a friend of another strip the execution information and decrease encapsulation. The ideal is to lay as more of the information of every class invisible by all another classes as doable.

Previous Home Next