OOPortal 





Building Classes in C++  «Prev  Next»
Lesson 1

Introduction to OOP and Encapsulation using C++

This module introduces you to the basic concept of encapsulation in C++.
The following will be discussed:
  1. Why encapsulation is central to object-oriented programming
  2. What classes and objects are
  3. How to write member functions as part of an abstract data type
  4. How to limit access to an ADT's member data and functions
  5. How a class differs from a struct
    At the end of the module, you'll be given the opportunity to take a quiz covering these topics.


All data access must occur through the public interface. Thus, the data fields of an object are effectively hidden from the programmer. The act of hiding data is called encapsulation. While it is theoretically possible in C++ to leave data fields unencapsulated (by placing them into the public section), this is very uncommon in practice.
Classes in C++ provide static encapsulation of objects by generating code which contains specific knowledge about the internals of encapsulated objects.
Static encapsulation occurs at compile time and therefore cannot directly support the evolution of objects since recompilation of source code is required if the internal layout changes. This also prohibits the use of distributed or persistent objects without first ensuring that the internal representations of the objects match the ones in the compiled code.

Python Hiding Information

This process of hiding the implementation, or functional details, of an object is suitably called information hiding. It is also sometimes referred to as encapsulation, but encapsulation is actually a more all-encompassing term. Encapsulated data is not necessarily hidden. Encapsulation is, literally, creating a capsule and so think of creating a time capsule. If you put a bunch of information into a time capsule, lock and bury it, it is both encapsulated and the information is hidden. On the other hand, if the time capsule has not been buried and is unlocked or made of clear plastic, the items inside it are still encapsulated, but there is no information hiding. The distinction between encapsulation and information hiding is largely irrelevant, especially at the design level. Many practical references use these terms interchangeably. As Python programmers, we do not actually have or need true information hiding, so the more encompassing definition for encapsulation is suitable.