(Stefan Ram) writes:
> I was asked to teach »inheritance« and »polymorphism« in C++.
[...]
> Is anyone aware of examples where inheritance or
> polymorphism really shines in C++, that are taken from
> real-life applications of C++ or are something that can be
> obviously useful in C++ programming, but are also rather
> small and do not require too much knowledge of C++ (i.e.
> only require knowledge of classes and derivation of classes
> but not of, say, generics)? Some simple examples where
> inheritance or polymorphism really makes sense?
I like to use symbolic expressions, small ASTs that represent arithmetic
expressions: one purely virtual at the top, with sub-classes for
constants, variables, binary ops, etc. Typical operations are: printing,
evaluating (given a map from variables to values), and computing a
symbolic derivative. Then you can add classes for trigonometric
functions, add simplification, etc.
(A variant of this is representing boolean expressions, with operations
like computing normal forms, etc.)
-- Alain.