20.1 A Brief Overview of Traditional OOP
While C is primarily procedural, C++ incorporates OOP principles extensively. Rooted in languages like Simula and Smalltalk, OOP structures programs around objects, which encapsulate data (fields or members) and the procedures that operate on that data (methods). The primary motivations behind OOP include:
- Managing Complexity: Decomposing large systems into smaller, self-contained objects that model conceptual entities.
- Code Reuse: Extending existing code, often through inheritance, where new classes (derived/subclasses) acquire properties and behaviors from existing ones (base/superclasses).
- Intuitive Modeling: Designing software based on object interactions.
The three pillars commonly associated with traditional OOP (especially in C++) are:
- Encapsulation: Bundling data and methods within an object and controlling access to the internal state. C++ uses
public
,protected
, andprivate
access specifiers. This prevents direct external manipulation of internal data, helping maintain invariants. - Inheritance: Allowing a new class to inherit members (data and methods) from an existing class, establishing an “is-a” relationship. This promotes code reuse but can create strong coupling.
- Polymorphism: Enabling objects of different derived classes to be treated uniformly through a common base class interface, typically via base class pointers or references and virtual function calls in C++. This allows for flexible and extensible systems.