← Writing

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

  1. Encapsulation: Binding data and methods in one class and restrict direct acces.
  2. Abstraction: Show only necessary details and hiding complex implementation.
  3. Inheritance: One class can reuse properties and methods of other class.
  4. 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

  1. Abstract class
  2. 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

  1. Public: Members are accessible from anywhere in the program.
  2. Private: Members can only accessible within the class.
  3. Protected: within class and derived class.

Javascript doesn’t support protected access specifier.


Polymorphism

“Many Forms”

Two Types of Polymorphism are

  1. Compile time (Static)
  2. Run-time (Dynamic)

Compile Time

Compile time polymorphism can be achieve by

  1. Function overloading
  2. Operator overloading

Run Time

Run time polymorphism can be achieve by

  1. Virtual Functions
  2. Function overriding
  3. Base class Pointer

Links

  1. Coding implementation in CPP

    GitHub - sharma-dikshant/cp at oops