Instructor's Guide
Inheritance is the composition mechanism par excellence
for object-oriented programming.
As argued before, inheritance allows for virtually
unlimited factorization and hence reuse of code.
Inheritance, however, is increasingly recognized as a mechanism
that is difficult to control.
Moreover, the programmer employing inheritance,
when working with a statically typed language
like Eiffel or C++, is faced with the problem
of fitting the inheritance structure within the
type system by designing an adequate type structure.
In this section, we will look at the mechanisms
available to develop hierarchic type structures
and ways to check that derived classes meet
the criterion of behavioral conformance as expressed
in section contracts.
Brief examples will be presented to illustrate
the problems and pitfalls of which the programmer
must be aware.
In particular, we will discuss the mechanism of explicit
scoping, the difficulties involved in satisfying
the invariant properties for derived classes,
and the use of multiple inheritance with shared
base classes (which results in structures having
a diamond property).
Virtual functions and scoping
Scoping, when used in combination with recursive virtual functions,
may lead to unexpected problems.
In slide 7-recursion, an example is given illustrating a problem that
may occur when defining a derived class that
relies for its functionality on the original methods
defined for the base class.
Virtual functions and recursion
class P {
\fbox{P}
public:
virtual int m(int i) {
cout << i; return i==0 ? i : m(i-1);
}
};
class C : public P {
In the example, the derived class C is used
to provide a better interface to the functionality
P offers, by including an additional test in .
This test prevents from diverging.
However, the statement
P* p = new C(); p->m(k);
\ifsli{//}{}
for any will not result in printing
the sequence $0..kP::mC::mP::mP'operator()value()display()