Inheritance and class hierarchies
A class for straight lines
A first try on a class for parabolas
A class for parabolas using inheritance
Checking the class type
Attribute vs inheritance: has-a vs is-a relationship
Superclass for defining an interface
Class hierarchy for numerical differentiation
Classes for differentiation
Verification
A flexible main program
Extensions
Alternative implementation via functions
Alternative implementation via functional programming
Alternative implementation via a single class
Class hierarchy for numerical integration
Numerical integration methods
Classes for integration
Verification
Using the class hierarchy
About object-oriented programming
Class hierarchy for making drawings
A first glimpse of Pysketcher
Basic construction of sketches
Inner workings of the Pysketcher tool
Example of classes for geometric objects
Adding functionality via recursion
Scaling, translating, and rotating a figure
Classes for DNA analysis
Class for regions
Class for genes
Subclasses
Summary
Chapter topics
Example: Input data reader
Exercises
Exercise 1: Demonstrate the magic of inheritance
Exercise 2: Make polynomial subclasses of parabolas
Exercise 3: Implement a class for a function as a subclass
Exercise 4: Create an alternative class hierarchy for polynomials
Exercise 5: Make circle a subclass of an ellipse
Exercise 6: Make super- and subclass for a point
Exercise 7: Modify a function class by subclassing
Exercise 8: Explore the accuracy of difference formulas
Exercise 9: Implement a subclass
Exercise 10: Make classes for numerical differentiation
Exercise 11: Implement a new subclass for differentiation
Exercise 12: Understand if a class can be used recursively
Exercise 13: Represent people by a class hierarchy
Exercise 14: Add a new class in a class hierarchy
Exercise 15: Compute convergence rates of numerical integration methods
Exercise 16: Add common functionality in a class hierarchy
Exercise 17: Make a class hierarchy for root finding
Exercise 18: Make a calculus calculator class
Exercise 19: Compute inverse functions
Exercises
Exercise 20: Make line drawing of a person; program
Exercise 21: Make line drawing of a person; class
Exercise 22: Animate a person with waving hands
References
This document introduces the basic ideas of object-oriented
programming. Different people put different meanings into the term
object-oriented programming: some use the term for programming with
objects in general, while others use the term for programming with
class hierarchies. The author applies the second meaning, which is the
most widely accepted one in computer science. The first meaning is
better named object-based programming. Since everything in Python
is an object, we do object-based programming all the time, yet one
usually reserves this term for the case when classes different from
Python's basic types (int
, float
, str
, list
, tuple
, dict
)
are involved.
Necessary background for the present document includes basic knowledge
about classes in Python, at least concepts such as attributes
(method attributes, data attributes),
methods, constructors, the self
object, and the __call__
special method.
For the sections Class hierarchy for numerical differentiation
and Class hierarchy for numerical integration one must know the most basic methods for numerical
differentiation and
integration.
During an initial reading of the document, it can be beneficial to skip
the more advanced material in
the sections Extensions-Alternative implementation via a single class.
All the programs associated with this document are found in the folder src/oo.