CPSC 427a: Object-Oriented Programming
Michael J. Fischer
Multiple Inheritance
What
is
multiple
inheritance
Multiple inheritance simply means deriving a class from two or more
base classes.
Example:
class Item : public Exam, public Ordered { ... };
Object structure
Suppose class A is multiply derived from both B and C.
We write this as class A : B, C { ... };.
Each instance of A has “embedded” within it an instance of B and an
instance of C.
All data members of both B and C are present in the instance, even if
they are not visible from within A.
Derivation from each base class can be separately controlled with privacy keywords, e.g.:
class A : public B, protected C { ... };.
Diamond pattern
One interesting case is the diamond pattern.
Each instance of A contains two instances of D—one in B and one in
C.
These can be distinguished using qualified names.
Suppose x is a public data member of D.
Within A, can write B::D::x to refer to the first copy, and C::D::x to refer to the second copy.
Handling Circularly Dependent Classes
Tightly
coupled
classes
Class B depends on class A if B refers to elements declared within class A
or to A itself.
The class B definition must be read by the compiler after reading
A.
This is often ensured by putting #include "A.hpp" at the top of file
B.hpp.
A pair of classes A and B are tightly coupled if each depends on the
other.
It is not possible to have both read after the other.
Whichever the compiler reads first will cause the compiler to complain about undefined symbols from the other class.
Example: List and Cell
Suppose we want to extend a cell to have a pointer to a sublist.
This won’t compile, because List is used (in class Cell) before it is defined. But putting the two class definitions in the opposite order also doesn’t work since then Cell would be used (in class List) before it is defined.
Circularity with #include
Circularity is less apparent when definitions are in separate files.
File list.hpp:
File cell.hpp:
File main.cpp:
What happens?
In this example, it appears that class List will get read before class
Cell since main.cpp includes list.hpp before cell.hpp.
Actually, the opposite occurs. The compiler starts reading list.hpp but
then jumps to cell.hpp when it sees the #include "cell.hpp"
line.
It jumps again to list.hpp when it sees the #include "list.hpp"
line in cell.hpp, but this is the second attempt to load list.hpp, so it
only gets as far as #pragma once. It then resumes reading cell.hpp
and processes class Cell.
When done with cell.hpp, it resumes reading list.hpp and processes class List.
Resolving circular dependencies
Several tricks can be used to allow tightly coupled classes to compile. Assume A.hpp is to be read first.
Template Example
16-Multiple-template
To illustrate templates, I converted 16-Multiple to use template
classes.
There is much to be learned from this example.
Today I point out only a few features.
Container class hierarchy
As before, we have PQueue->Linear->Container.
Now, each of these are template classes with parameter <T>.
T is the item type; the queue stores elements of type T*.
The main program creates a priority queue using
Item class hierarchy
As before, we have Item->Exam, Ordered.
Item is an adaptor class.
It bridges the requirements of PQueue<T> to the Exam class.
Ordered template class
Ordered describes an abstract interface for an abstract key type.
It becomes a template class with type parameter KeyType.
Item derives from Ordered<int>.
Alternative Ordered interfaces
The code presents two alternative interfaces:
A real application would choose one interface or the other and go with it.
Both are in the code for comparison.
The C++ Standard Library
A bit of history C++ standardization.
The standard library was derived from several different sources.
STL (Standard Template Library) portion of the C++ standard was derived from an earlier STL produced by Silicon Graphics (SGI).
Containers
A container stores a collection of objects of arbitrary type T.
The basic containers in STL are:
Common container operations
All containers share a large number of operations.
Operations include creating an empty container, inserting, deleting,
and copying elements, scanning through the container, and so
forth.
Liberal use is made of operator definitions to make containers behave as
much like other C++ objects as possible.
Containers implement value semantics, meaning type T objects are
copied freely within the containers.
If copying is a problem, store pointers instead.
vector<T>
A vector<T> is a growable array of elements of type T.
You must #include <vector>.
Elements can be accessed using standard subscript notion.
Inserting at the beginning or middle of a vector takes time O(n).
Example: