The three pillars of OOPS in C++ are encapsulation, inheritance, and polymorphism. A true object-oriented program includes all three pillars. Let us deep dive into each pillar.
Encapsulation refers to placing both data and operations within a class definition to realize an abstract data type (ADT). The term abstraction refers to the process of extracting the crux or main part of a real-world thing or concept and modeling it with the data (data abstraction) and operations (procedural abstraction) of the ADT. The data portion of this tuple is generally placed in the "private" part of the class, while the operations from the public interface to the ADT, and is therefore placed in the "public" part of the class definition. Information hiding refers to the fact that we prevent a user of the class from having access to the data in the class implementation for that case where the user does not need such information. We expose data on a need-to-know basis. We say that we are practicing information hiding by defining our classes in this way.
Inheritance means acquiring all possessions or properties. In C++, we have two forms of inheritance, i.e., single inheritance and multiple inheritances. Single inheritance happens when one class (called the derived class) acquires the properties (data and operations) of another class (called the base class), whereas multiple inheritances occurs when one class acquires the properties of two or more base classes.
Thus a base class is the parent class from which other child classes are derived. Derived class or child class is also called an extended class. Poly means many, and morph means form. So polymorphism in C++ uses the same name for different operations on objects of different data types. Polymorphism may be achieved (or at least approximated) in several ways:
-
Using function overloading and operator overloading
-
Using function templates
-
Using virtual functions with dynamic binding or run-time binding