Programming in C++
Exercise Sheet 7
- These exercises use toy examples to explore the ideas of multiple inheritance.
- Write a class A, classes B and C deriving from A, and a class D deriving from B
and C. Add some state to the A class and verify that a D object contains two
copies of the A data, by modifying the one accessible through B and printing the
one accessible through C.
- Repeat this demonstration, but using virtual inheritance, so that a D object contains a
single of the A data.
- Add default constructors to all classes, which merely print out the name of the class.
Use this to see the order in which constructors are executed, both with and without
virtual inheritance.
- Using virtual inheritance, define a virtual method in class A, and extend it in each of
the other classes without repeating code and without doing the same thing twice. (i.e.
like the draw method in the lecture.) Verify that this works by placing output
statements in each method.
- Define a class E derived from C, and a class F derived from both D and E, and repeat
the previous exercise.
- Suppose we have an Employee class including a name, National Insurance number, etc.
Suppose this class has two derived classes HourlyEmployee and CommissionEmployee, for
people paid in different ways, and later we’ll want a class for people paid both hourly and
by commission. What kind of inheritance is appropriate? Sketch these classes, their fields
and constructors.