CSS 343: Notes from Lecture 15 (DRAFT)

Administrivia

Resource Management: RAII

Constructors, Destructors, & RAII

Inheritance & Polymorphism

Yo-Yo Problem

Problem: programmer bouncing up and down a long, complicated inheritance graph.

Best practices:

Liskov Substitution Principle

The basic concept of inheritance is that the subtype receives all the properties of the base type. Old-school inheritance model is that the subtype inherits the implementation. Modern use of inheritance is that the subtype is a kind of the base type ("is-a" or "isa").

Inheritance should never be used when the sublcass restricts the freedom implicit in the base class; it should only be used when the subclass adds extra details to the concept represented by the base class.

The Liskov substitution principle codifies this concept: if S is a subtype of T then objects of type T may be replaced by objects of type S. This is also known as "strong behavioral subtyping".

Classes should inherit the interface (abstract base classes) rather than the implementation.

Use composition (Has-A) and delegation to implement implementation inheritance. For example, a priority queue is not a kind of vector, even though the binary heap is implemented using a vector.

Circle-Ellipse Problem

There are a variety of potential solutions to the problem:

Principles of Object-Oriented Design: S.O.L.I.D

Object-Oriented Patterns

See the Design Patterns book (also known as the Gang of Four book: http://en.wikipedia.org/wiki/Design_Patterns. Not to be confused with this gang of four: http://en.wikipedia.org/wiki/Gang_of_Four