\documentstyle[12pt,html]{article}
\oddsidemargin 0.25in
\topmargin -0.5in
\textheight 8.5in
\textwidth 6.25in
\newcommand{\realspace}{
   \renewcommand{\baselinestretch}{1.5}
   \large\normalsize}
\newcommand{\singlespace}{
   \renewcommand{\baselinestretch}{1.0}
   \large\normalsize}
\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\newcommand{\bv}{\begin{verbatim}}
\newcommand{\edv}{\end{verbatim}}

\input epsf
\begin{document}
\setcounter{page}{115}

\centerline{\large{\bf LECTURE 11}}

\centerline{\bf Multiple Inheritance}
In the class hierarchies discussed so far, each class has
inherited from a single parent. However, it is possible for
a class to inherit from more than one base class. This is called
multiple inheritance. An example is the class sleeper sofa,
which is both a bed and a sofa: 
\begin{verbatim}
   class Bed
     {
       public:
	 Bed();                        //constructor
         void sleep();
         int weight;
     };
   class Sofa
     {
       public:
         Sofa();                       //constructor
         void sit();
         int weight;
     };
   class SleeperSofa:  public Bed, public Sofa
     {
       public:
         SleeperSofa();
         void foldOut();
     };
   int main()
     {
       SleeperSofa ss;
         ss.sit();                    //Sofa::sit()
         ss.foldOut();                //SleeperSofa::foldOut()
         ss.sleep();                  //Bed::sleep()
       return 0;
     }
\end{verbatim}
Notice the class {\tt SleeperSofa} inherits from both {\tt Bed}
and {\tt Sofa}. {\tt SleeperSofa} inherits all members of both
base classes. Thus {\tt ss.sit()} and {\tt ss.sleep();}
are legal. You can use a {\tt SleeperSofa} as a {\tt Bed} or 
as a {\tt Sofa}. Plus the class {\tt SleeperSofa} can have
members of its own such as {\tt foldOut()}.

\centerline{\bf Inheritance Ambiguities}
Although multiple inheritance is a powerful feature, it introduces
some ambiguities. Consider the preceding example where both 
{\tt Bed} and {\tt Sofa} have a data member {\tt weight}. Which
{\tt weight} does {\tt SleeperSofa} inherit? The answer is both.
{\tt SleeperSofa} has a member \verb2Bed::weight2 and another member
\verb2Sofa::weight2. Because they have the same name, unqualified
references to {\tt weight} are now ambiguous as in the following example:
\begin{verbatim}
#include <iostream.h>

void fn()
{
   SleeperSofa ss;
   cout << "weight = " 
        << ss.weight     //illegal: which weight?
        << "\n";
}
\end{verbatim}
The inheritance structure is shown in the following figure:

\epsfbox{lec11sofa1.eps}

We could fix this by explicitly specifying the desired base class:
\begin{verbatim}
#include <iostream.h>

void fn()
{
   SleeperSofa ss;
   cout << "weight = " 
        << ss.Sofa::weight  //ok but ugly
        << "\n";
}
\end{verbatim}
This is ok but undesirable because it forces class information to leak
outside the class into the application code.

\centerline{Virtual Inheritance}
So how do we fix name collisions? One way might be to make a more
fundamental base class {\tt Furniture} that has {\tt weight} as a member.
Then derive {\tt Bed} and {\tt Sofa} from {\tt Furniture}. So we want
the inheritance structure to look like:

\epsfbox{lec11sofa2.eps}

\begin{verbatim}
#include <iostream.h>

//Furniture - more fundamental concept; this class
//            has "weight" as a property
class Furniture
{
  public:
   Furniture()
   { }
   int weight;
};
class Bed : public Furniture
{
  public:
   Bed()
   { }
   sleep()
   { }
};
class Sofa : public Furniture
{
  public:
   Sofa()
   { }
   void watchTV()
   { }
};
class SleeperSofa : public Bed, public Sofa
{
  public:
   SleeperSofa()
   { }
   void foldOut()
   { }
};
void fn()
{
   SleeperSofa ss;
   cout << "weight = " 
        << ss.weight     //problem solved; right?
        << "\n";
}

int main()
{
   fn();
   return 0;
}
\end{verbatim}
This doesn't work because now {\tt SleeperSofa} inherits 2 
{\tt Furniture} objects; one through {\tt Bed} and one through
{\tt Sofa}. So when {\tt weight} is called, it doesn't know which one to use.
The inheritance structure actually looks like:

\epsfbox{lec11sofa3.eps}

Obviously {\tt SleeperSofa} only needs one {\tt Furniture} object. To
accomplish this, we use {\it virtual inheritance}.
\begin{verbatim}
#include <iostream.h>
class Furniture
{
  public:
   Furniture()
   { }
   int weight;
};
class Bed : virtual public Furniture
{
  public:
   Bed()
   { }
   void sleep()
   { }
};
class Sofa : virtual public Furniture 
{
  public:
   Sofa()
   { }
   void watchTV()
   { }
};
class SleeperSofa : public Bed, public Sofa
{
  public:
   SleeperSofa() : Sofa(), Bed()
   { }
   void foldOut()
   { }
};
void fn()
{
   SleeperSofa ss;
   cout << "weight = "
        << ss.weight
        << "\n";
}

int main()
{
   fn();
   return 0;
}
\end{verbatim}
Notice the addition of the keyword {\it virtual} in the inheritance
of {\tt Furniture} in {\tt Bed} and {\tt Sofa}. This says, ``Give me
a copy of {\tt Furniture} unless you already have one somehow, in which
case I'll just use that one.'' So {\tt SleeperSofa} ends up inheriting
only one {\tt Furniture} object. Now the reference to {\tt weight} in
{\tt fn} is no longer ambiguous and we have solved the problem
of name collisions. Now the inheritance structure is the desired one and
looks like:

\epsfbox{lec11sofa2.eps}
 

If virtual inheritance solves this problem so nicely, why isn't it the
norm or default? There are 2 reasons. First virtually inherited base classes
are handled internally quite differently than normally inherited
base classes, and these differences involve extra overhead. Second,
you might want 2 copies of the base class, though this is unusual.
As an example of the latter, consider a {\tt TeachingAssistant} who is
both a {\tt Student} and a {\tt Teacher}, both of which are subclasses
of {\tt Academician}. If the university gives its TA's 2 ID's--a student
ID and a separate teacher ID--class {\tt TeachingAssistant} will need
2 copies of class {\tt Academician}.

\centerline{\it Order of Construction}
The rules for constructing objects need to be expanded to handle
multiple inheritance. The constructors are invoked in the following
order:
\begin{enumerate}
\item The constructors for any virtual base classes are called in the
order in which the classes are inherited.
\item Then the constructors for any nonvirtual base classes are called
in the order in which the classes are inherited.
\item Next the constructor for any member objects are called in the
order in which the member objects appear in the class.
\item Finally, the constructor for the class itself is called
\end{enumerate}
Notice that base classes are constructed in the order in which they are
inherited and not in the order in which they appear on the constructor 
line.

\centerline{\bf Cautionary Note}
I bring up the topic of virtual inheritance not because I want you to 
go out and use it right away, but because you may want to read someone
else's code which uses it. Here are some reasons to be cautious
about using multiple inheritance:
\begin{enumerate}
\item Multiple inheritance introduces more 
overhead and therefore is more inefficient. 
\item It can introduce ambiguities like the name collisions we saw 
earlier. Another ambiguity arises when casting a pointer from
a subclass to a base class; this often changes the value of the
pointer in strange and mysterious ways. For example:
\begin{verbatim}
#include <iostream.h>
class Base1 {int mem};
class Base2 {int mem};
class SubClass: public Base1, public Base2 {};

void fn(SubClass *pSC)
{
  Base1 *pB1 = (Base1*)pSC;
  Base2 *pB2 = (Base2*)pSC;
}
\end{verbatim}
{\tt pB1} and {\tt pB2} are not numerically equal even though  
they came from the same original value {\tt pSC}. (Actually,
if {\tt fn} is passed a zero, they are equal. See how strange it gets?)
\end{enumerate}
\end{document}




