Programming in C++

Exercise Sheet 7
  1. These exercises use toy examples to explore the ideas of multiple inheritance.
    1. 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.

    2. Repeat this demonstration, but using virtual inheritance, so that a D object contains a single of the A data.

    3. 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.

    4. 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.

    5. Define a class E derived from C, and a class F derived from both D and E, and repeat the previous exercise.

  2. 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.