


                                                              Chapter 9
                                                   MULTIPLE INHERITANCE
                                                  AND FUTURE DIRECTIONS

      C++ version 2.0 was released by AT&T during the summer of 1989, and
      the major addition to the language is multiple inheritance, the
      ability to inherit data and methods from more than one class into
      a subclass.  Multiple inheritance and a few of the other additions
      to the language will be discussed in this chapter along with a few
      of the expected future directions of the language.

      Several companies have announced their intention of marketing C++
      compilers early in 1990, but as of this writing, Borland
      International and Zortech are the only two major companies to
      actually deliver a product to the marketplace.  The examples in
      this chapter have all been compiled and executed using the Borland
      C++ compiler version 1.00 but the Zortech compiler was on back
      order at the time of this writing, so the example programs could
      not be tested with it.

      After completing this tutorial, you should have enough experience
      with the language to study additional new constructs on your own
      as they are implemented by the various compiler writers.  We will
      update the entire tutorial as soon as practical following
      procurement of any new compiler, but hopefully the language will
      not change rapidly enough now to warrant an update oftener than
      twice a year.  Please feel free to contact us for information on
      updates to the Coronado Enterprises C++ tutorial.


      MULTIPLE INHERITANCE
      _________________________________________________________________

      The major addition to the C++ language with the release of version
      2.0 is the ability to inherit methods and variables from two or
      more parent classes when building a new class.  This is called
      multiple inheritance, and is purported by many people to be a major
      requirement for an object oriented programming language.  Some
      writers have expressed doubts as to the utility of multiple
      inheritance, and we are inclined to agree with them.  To illustrate
      the validity of this, it was not easy to think up a good example
      of the use of multiple inheritance as an illustration for this
      chapter.  In fact, the resulting example is sort of a forced
      example that really does nothing useful.  It does however,
      illustrate the mechanics of the use of multiple inheritance with
      C++, and that is our primary concern at this time.

      The biggest problem with multiple inheritance involves the
      inheritance of variables or methods from two or more parent classes
      with the same name.  Which method should be chosen as the inherited
      variable or method if two or more have the same name?  This will
      be illustrated in the next few example programs.

                                                               Page 9-1
      
                                       Chapter 9 - Multiple Inheritance

      SIMPLE MULTIPLE INHERITANCE
      _________________________________________________________________

      An examination of the file named MULTINH1.CPP    ================
      will reveal the definition of two very simple      MULTINH1.CPP
      classes in lines 4 through 27 named moving_van   ================
      and driver.

      In order to keep the program as simple as possible, all of the
      member methods are defined as inline functions.  This puts the code
      for the methods where it is easy to find and study.  You will also
      notice that all variables in both classes are declared to be
      protected so they will be readily available for use in any class
      which inherits them.  The code for each class is kept very simple
      so that we can concentrate on studying the interface to the methods
      rather than spending time trying to understand complex methods.
      As mentioned previously, chapter 12 will illustrate the use of non-
      trivial methods.

      In line 30, we define another class named driven_truck which
      inherits all of the data and all of the methods from both of the
      previously defined classes.  In the last two chapters, we studied
      how to inherit a single class into another class, and to inherit
      two or more classes, the same technique is used except that we use
      a list of inherited classes separated by commas as illustrated in
      line 30.  The observant student will notice that we use the keyword
      public prior to the name of each inherited class in order to be
      able to freely use the methods within the subclass.  In this case,
      we didn't define any new variables, but we did introduce two new
      methods into the subclass in lines 32 through 39.

      We declared an object named chuck_ford which presumably refers to
      someone named Chuck who is driving a Ford moving van.  The object
      named chuck_ford is composed of four variables, three from the
      moving_van class, and one from the driver class.  Any of these four
      variables can be manipulated in any of the methods defined within
      the driven_truck class in the same way as in a singly inherited
      situation.  A few examples are given in lines 47 through 56 of the
      main program and the diligent student should be able to add
      additional output messages to this program if he understands the
      principles involved.

      All of the rules for private or protected variables and public or
      private method inheritance as used with single inheritance extends
      to multiple inheritance.



      DUPLICATED METHOD NAMES
      _________________________________________________________________

      You will notice that both of the parent classes have a method named
      initialize(), and both of these are inherited into the subclass

                                                               Page 9-2
      
                                       Chapter 9 - Multiple Inheritance

      with no difficulty.  However, if we attempt to send a message to
      one of these methods, we will have a problem, because the system
      does not know which we are referring to.  This problem will be
      solved and illustrated in the next example program.

      Before going on to the next example program, it should be noted
      that we have not declared any objects of the two parent classes in
      the main program.  Since the two parent classes are simply normal
      classes themselves, it should be apparent that there is nothing
      magic about them and they can be used to define and manipulate
      objects in the usual fashion.  You may wish to do this to review
      your knowledge of simple classes and objects of those classes.

      Be sure to compile and execute this program after you understand
      its operation completely.



      MORE DUPLICATE METHOD NAMES
      _________________________________________________________________

      The second example program in this chapter named ================
      MULTINH2.CPP, illustrates the use of classes       MULTINH2.CPP
      with duplicate method names being inherited into ================
      a subclass.

      If you study the code, you will find that a new method has been
      added to all three of the classes named cost_per_full_day().  This
      was done intentionally to illustrate how the same method name can
      be used in all three classes.  The class definitions are no problem
      at all, the methods are simply named and defined as shown.  The
      problem comes when we wish to use one of the methods since they are
      all the same name and they have the same numbers and types of
      parameters and identical return types.  This prevents some sort of
      an overloading rule to disambiguate the message sent to one or more
      of the methods.

      The method used to disambiguate the method calls are illustrated
      in lines 60, 64, and 68 of the main program.  The solution is to
      prepend the class name to the method name with the double colon as
      used in the method implementation definition.  This is referred to
      as qualifying the method name.  Actually, you could qualify all
      method calls, but if the names are unique, the compiler can do it
      for you and make your code easier to write and read.

      Be sure to compile and execute this program and study the results.
      The observant student will notice that there is a slight
      discrepancy in the results given in lines 79 through 81, since the
      first two values do not add up to the third value exactly.  This
      is due to the limited precision of the float variable but should
      cause no real problem.





                                                               Page 9-3
      
                                       Chapter 9 - Multiple Inheritance

      DUPLICATED VARIABLE NAMES
      _________________________________________________________________

      If you will examine the example program named    ================
      MULTINH3.CPP, you will notice that each subclass   MULTINH3.CPP
      has a variable with the same name.               ================

      According to the rules of inheritance, an object
      of the driven_truck class will have two variables with the same
      name, weight.  This would be a problem if it weren't for the fact
      that C++ has defined a method of accessing each one in a well
      defined way.  You have probably guessed that we will use
      qualification to access each variable.  Lines 38 and 45 illustrate
      the use of the variables.  It may be obvious, but it should be
      explicitly stated, that there is no reason that the subclass itself
      cannot have a variable of the same name as those inherited from the
      parent classes.  In order to access it, you simply use
      qualification.

      It should be apparent to you that once you understand single
      inheritance, multiple inheritance is nothing more than an extension
      of the same rules.  Of course, if you inherit two methods or
      variables of the same name, you must use qualification to allow the
      compiler to select the correct one.


      FUTURE DIRECTIONS OF C++
      _________________________________________________________________

      An ANSI committee has been formed to write an ANSI standard for
      C++.  They first met in the Spring of 1990 and are expected to
      complete the standard in about three years.  Until the new standard
      is released, the C++ language is expected to stay fairly stable.
      However, due to the nature of compiler writers and their desire to
      slightly improve their offerings over their competitors, you can
      bet that the language will not remain static during this three year
      period.

      Many small changes have been added during the past year that barely
      affect the casual programmer, or even the heavy user of the
      language.  You can be sure that the language will evolve slowly and
      surely into a very usable and reliable language.  There are two
      areas that should be discussed in a little detail because they will
      add so much to the language in future years, exception handling and
      parameterized types.


      FUTURE DIRECTIONS - EXCEPTION HANDLING
      _________________________________________________________________

      A future version of C++ will have some form of exception handling
      to allow the programmer to trap errors and prevent the system from
      completely shutting down when a fatal error occurs.  The Ada
      language allows the programmer to trap any error that occurs, even

                                                               Page 9-4
      
                                       Chapter 9 - Multiple Inheritance

      system errors, execute some recovery code, and continue on with the
      program execution in a very well defined way.  Bjarne Stroustrup
      has announced that some form of exception handling will be
      implemented but he has not stated what form it would take as of
      this writing.


      FUTURE DIRECTIONS - PARAMETERIZED TYPES
      _________________________________________________________________

      Many times, when developing a program, you wish to perform some
      operation on more than one data type.  For example you may wish to
      sort a list of integers, another list of floating point numbers,
      and a list of alphabetic strings.  It seems silly to have to write
      a separate sort function for each of the three types when all three
      are sorted in the same logical way.  With parameterized types, you
      will be able to write a single sort routine that is capable of
      sorting all three of the lists.

      This is already available in the Ada language as the generic
      package or procedure.  Because it is available in Ada, there is a
      software components industry that provides programmers with
      prewritten and thoroughly debugged software routines that work with
      many different types.  When this is available, there will be a
      components industry for C++ and precoded, debugged and efficient
      source code will be available off the shelf to perform many of the
      standard operations.  These operations will include such things as
      sorts, queues, stacks, lists, etc.

      Bjarne Stroustrup has announced that parameterized types will be
      available in a future version of C++ but he has not announced the
      details of how they would be implemented.  He has presented a paper
      with details of one way to implement them, but this is only a
      suggestion, not a specification.


      WHAT SHOULD BE YOUR NEXT STEP?
      _________________________________________________________________

      Once again, we have reached a major milestone in C++ programming.
      With the ability to use inheritance, you have nearly all of the
      tools you need to effectively use the object oriented programming
      techniques of C++ and you would do well to stop studying again and
      begin programming.  The only topic left with C++ is virtual methods
      which are used for dynamic binding or polymorphism.  This will be
      covered in the next two chapters.  The vast majority of all
      programming can be done without dynamic binding, and in attempting
      to force it into every program, you could wind up with an
      unreadable mess.







                                                               Page 9-5
