C++ Programming Code Examples C++ > Beginners Lab Assignments Code Examples C++ Programming Encapsulation C++ Programming Encapsulation In Object Oriented Programming, encapsulation represents binding data and functions into one container. This container hides the details of the data and the way functions process data. In C++, Class is a container that binds data and functions. The mechanism of hiding details of a class is called abstraction and it is described in "C++ Abstraction". Class encapsulates all manipulations with the data. Take a look on the example: class myStack { //interface of class myStack //this is only accessible for user public: //allocate memory for stack myStack(int _size = 50) { size = _size; stack = new int[size]; //initially stack is empty top = -1; } //add value to stack bool push(int i) { if (isFull()) return false; else { top++; stack[top] = i; } } int pop() { if (isEmpty()) throw new exception("Stack is empty"); else { return stack[top--]; } } //hidden data members and member functions private: //return true if stack is full bool isFull() { return size == top - 1; } bool isEmpty() { return top == -1; } int size; int* stack; int top; };