Object Oriented Programming | Quick Revision

Object oriented programming is a way where everything is treated as an object. Real world entities are defined as objects with
data -> variables
behaviour -> functions
Benefits of using object oriented programming over procedural programming
- Modularity
- Reusability
- Scalability
- Maintainability
Four Pillars of OOPs
- Encapsulation: Binding data and methods in one class and restrict direct acces.
- Abstraction: Show only necessary details and hiding complex implementation.
- Inheritance: One class can reuse properties and methods of other class.
- Polymorphism: Same functions behave differently in different context.
Getter and setter are used for controlled access (validation, filtering) to data.
Abstraction
Hiding implementation.
Abstraction is implemented using
- Abstract class
- Interface
Abstract Class:
- An abstract class can’t be instantiated and serve as blueprint for derived (concrete class).
- Abstract class can be created by declaring at least one pure virtual function.
- Abstract class may contain concrete members.
Interface
- Interface is represented using an abstract class that only contains pure virtual functions with no data members or concrete methods.
- This enforce a contract i.e. any class inheriting from their must implements all the listed functions.
Inheritance
Reusing methods of another class.
Types of inheritances
| type | remark | |
|---|---|---|
| Single | 1 P → 1 C | |
| Multi-level | 1 P → 1 SC → 1 C | |
| Multiple | 2 P → 1 C | • Cpp supports. |
| • Java doesn’t support. | ||
| • Javascript directly doesn’t support |
Access Specifiers
- Public: Members are accessible from anywhere in the program.
- Private: Members can only accessible within the class.
- Protected: within class and derived class.
Javascript doesn’t support protected access specifier.
Polymorphism
“Many Forms”
Two Types of Polymorphism are
- Compile time (Static)
- Run-time (Dynamic)
Compile Time
Compile time polymorphism can be achieve by
- Function overloading
- Operator overloading
Run Time
Run time polymorphism can be achieve by
- Virtual Functions
- Function overriding
- Base class Pointer
Links
Coding implementation in CPP