Page 196 - DCAP404 _Object Oriented Programming
P. 196

Unit 9: Inheritance




          9.3.2  Ambiguity in Multipath Inheritance                                             Notes

          Sometimes we need to apply two or more types of inheritance to design a program and this type
          of inheritance are called hybrid inheritance. But in hybrid inheritance, if multiple paths exit
          between a base class and derived class through different intermediate classes, then derived class
          inherits the members of the base class more than one time and it results in ambiguity. Here, base
          class is called indirect base class and intermediate base classes are called direct base classes.
          To avoid this ambiguity, the indirect base class is made virtual base class and to define base class
          as virtual, a keyword virtual is appended when extending the direct base  classes from  the
          indirect base class as given in the example. In this way, when we define the indirect base class
          virtual, compiler take care that only one copy of  that class is inherited, regardless of how many
          inherited paths exist between the virtual base class and the derived class.

                                    Figure  9.1: Hybrid  Inheritance




















                 Example:
          class  student

          {
              protected:
                     char  course[20];
                     char  name[20];
                     int  enroll;
          };



          class  exam:virtual  public  student
          {
                  protected:
                     char  etype[20];
                     int  msub1;
                     int  msub2;
                     int  msub3;
          };




                                           LOVELY PROFESSIONAL UNIVERSITY                                   189
   191   192   193   194   195   196   197   198   199   200   201