OOP and functional programming
OOP
Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions Object-oriented programming has several advantages over procedural programming:
- OOP provides a clear structure for the programs
- OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
- OOP makes it possible to create full reusable applications with less code and shorter development time
The four principles of object-oriented programming
Encapsulation
- Encapsulation is used to hide the values or state of a structured data object, preventing unauthorized parties direct access to them.
- Each object keep its state private. Outside can call only a list of public methods. The object manages its own states via method
- Used to make sure that data inside our object is not modified unexpectedly by external code in a completely part of our program which cause us difficult time to trace bugs.
Abstraction
- Abstraction means that each object should only expose a high-level mechanism for using it.
- This mechanism should hide internal implementation details. It should only reveal operations relevant for the other objects
Inheritance
- It a mechanism that allows you to derive a class from another class that shares a set of attributes and methods
Polymorphism
- Different types can be accessed through the same interface
Functional programming
- Built upon the idea of lambda calculus
- Is a programming paradigm where programs are constructed by applying and composing functions.
- Functions are treated as first-class citizens, they can be bound to names, pass as arguments, returned from other functions