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.
  • Composition
  • 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

Virtual tables

A non-virtual class has a size of 1 because in C++ classes can’t have zero size.

A virtual class has a size of 8 on a 64 bit machine because there’s a hidden pointer inside it pointing to a vtable. vtables are static translation tables, created for each virtual-class.

#include <iostream>

int main() {

    struct A {
    } a;

    struct B {
        virtual void func(){;}
    } b;

    std::cout << (sizeof a) << "\n";
    std::cout << (sizeof b) << "\n";


Polymorphism means “many forms” and describes the ability of an operation to change behaviour according to its arguments.

Ad-hoc polymorphism

Function and operator overloading.

Writeln(Add(1, 2));                   (* Prints "3"             *)
Writeln(Add('Hello, ', 'World!'));    (* Prints "Hello, World!" *)

Parametric polymorphism

Function does the same thing for different types: templates in C++.

Sub-typing (inheritance)

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
  • [Polymorphism](<https://en.wikipedia.org/wiki/Polymorphism_(computer_science))
  • What’s the difference between malloc and new?
  • Compare functional and object-oriented programming