| Lesson 4 | Encapsulation |
| Objective | Mechanism behind packaging data |
Mechanism behind packaging data and ADT behavior
C++ provides the encapsulation mechanism to implement ADTs. Encapsulation packages both the internal implementation details of the type and the externally available operations and functions that can act on variables of that type. The implementation details can be made inaccessible to code that uses the type. Code that uses the ADT is called the client code for the ADT.
For example, a stack could be implemented as a fixed-length array, while the publicly available operations would include push and pop.
Changing the internal implementation to a linked list should not affect how push and pop are used externally. Encapsulating the data and behavior of the stack keeps the implementation of the stack hidden from its clients, which means the client code using the stack will not need to be rewritten if the stack implementation changes.
C++ provides the encapsulation mechanism to implement ADTs. Encapsulation packages both the internal implementation details of the type and the externally available operations and functions that can act on variables of that type. The implementation details can be made inaccessible to code that uses the type. Code that uses the ADT is called the client code for the ADT.
For example, a stack could be implemented as a fixed-length array, while the publicly available operations would include push and pop.
Changing the internal implementation to a linked list should not affect how push and pop are used externally. Encapsulating the data and behavior of the stack keeps the implementation of the stack hidden from its clients, which means the client code using the stack will not need to be rewritten if the stack implementation changes.