OOP is a programming paradigm based on the concept of “objects”, which may contain data, in the form of fields often known as attributes; and code in the form of procedures, often known as methods. We are aiming to group related data and functions.
- Encapsulation: tie the data and the functions that are acting together in a class.
- Delegation is calling a routine “in another context”, for instance calling a base class member function from an instance of a derived class.
- Open recursion
- Abstraction: providing only essential information to the outside world and hiding their background details, separation of interface and implementation.
- Decoupling: removing dependencies on external computations/data.
- Low coupling, high cohesion
- See virtual.
Polymorphism means “many forms” and describes the ability of an operation to change behaviour according to its arguments.
Function and operator overloading.
Writeln(Add(1, 2)); (* Prints "3" *) Writeln(Add('Hello, ', 'World!')); (* Prints "Hello, World!" *)
Function does the same thing for different types: templates in C++.
Class hierarchies, diamond inheritance.
Static and dynamic polymorphism
Polymorphism can be distinguished by when the implementation is selected: statically (at compile time) or dynamically (at run time, typically via a virtual function). This is known respectively as static dispatch and dynamic dispatch, and the corresponding forms of polymorphism are accordingly called static polymorphism and dynamic polymorphism.
- Stack overflow
- What’s the difference between malloc and new?
- Compare functional and object-oriented programming
OOP programming in C++ is not particularly elegant in some instances. And can be very nuanced and easy to introduce subtle bugs. Also, if you inherit from a class it might not be immediately clear what other stuff you have also inherited.