Bruce Eckel's Thinking in C++, 2nd Ed Contents | Prev | Next

Exercises

  1. These exercises will take you step-by-step through the traps of MI. Create a base class X with a single constructor that takes an int argument and a member function f( ), that takes no arguments and returns void. Now inherit X into Y and Z, creating constructors for each of them that takes a single int argument. Now multiply inherit Y and Z into A. Create an object of class A, and call f( ) for that object. Fix the problem with explicit disambiguation.
  2. Starting with the results of exercise 1, create a pointer to an X called px, and assign to it the address of the object of type A you created before. Fix the problem using a virtual base class. Now fix X so you no longer have to call the constructor for X inside A.
  3. Starting with the results of exercise 2, remove the explicit disambiguation for f( ), and see if you can call f( ) through px. Trace it to see which function gets called. Fix the problem so the correct function will be called in a class hierarchy.

Contents | Prev | Next


Contact: webmaster@codeguru.com
CodeGuru - the website for developers.
[an error occurred while processing this directive]