Advanced-Programming

Polymorphism

Slides:

Polymorphism = “several forms”

Binding time

The binding of the function name with the code can be:

Classification of polymorphism

Polymorphism classification

Universal vs ad hoc polymorphism

Universal and ad-hoc polymorphis can coexist.

Overloading (ad hoc)

Coercion (universal)

The automatic conversion of an object to a different type. Opposed to casting, which is explicit.

Inclusion (universal)

Subtyping, inheritance.
Is ensured by the substitution principle: an object of a subtype (subclass) can be used in any context where an object of the supertype (superclass) is expected.
It is usually resolved a compile type (static binding).

Overriding (ad hoc in universal)

Overriding of a method in a subclass (thus it is possible only with inclusion).
Changes the signature: parameters and return value maintain the same type, but changes the type of the object on which it can be invoked on.
It is resolved at runtime (dynamic binding) with invokevirtual.

Parametric (universal)

Function Templates in C++

template <class T> // or <typename T>
T sqr(T x) { return x * x; }

Compiler/linker automatically generates one version for each parameter type used by a program:

int a = sqr(5);        // generates int sqr(int x) { return x * x; }
double b = sqr(3.14);  // generates double sqr(double x) { return x * x; }

Templates are executed by the compiler and differs from the macros that are executed by the preprocessor. Macros are just textually substituted, on templates are performed analysis checks.

Instantiation at compile time (static binding).

Generics in Java

Array<Type1> and Array<Type2> are not related by subtyping, but Type1[] and Type2[] are. Thus Java arrays are covariant.

Invariance, Covariance and contravariance

PECS Principle

Producer Extends, Consumer Super:

Example:

<T> void copy(List< ? super T > dst, List< ? extends T > src);

Standard Template Library (C++)

The goal of the STL is to represent algorithms in as general form as possible without compromising efficiency.

Main entities

STL entities

C++ namespaces

STL relies on C++ namespaces: containers exposes a type named iterator in the container’s namespace.
Each class implicity introduces a new namespace, the iterator type assumes its meaning depending on the context.

Complexity

Insert/delete complexity in containers:

Container Beginning Middle End
vector linear linear constant
list constant constant constant
deque constant linear constant

Iterator types

Iterators implements operations in constant time.
Containers may support different operators depending on their structure.

Container Iterator
vector random access
list bidirectional
deque random access

When a container is modified iterators to it can become invalid: the result of operations on them is not defined.

Memory model

STL abstract the memory model with allocators. Allocators are classes that encapsulate the information about the memory model. Each container is parametrized by such an allocator to let the implementation unchanged when switching memory models.

Limitations