Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
Your Ad Here
Your Ad Here
Virtual  Base  classes

Virtual  Base  classes  are  related  to  multiple  inheritance.  To  declare  a  virtual  base class,  add  the  C++  keyword  virtual  to  the  base  class  specifier.  The  keyword  virtual prevents  a  base  class  from  being  duplicated  within  a  derivation  hierarchy.  

Consider  the  situation  with  a  base  class,  Parent;  two  derived  classes,  Child1  and Child2;  and  a  fourth  class,  Grandchild,  derived  from  both  Child1  and  Child2.  In  this arrangement,  a  problem  can  arise  if  a  member  function  in  the  Grandchild  class  wants to  access  data  or  functions  in  the  Parent  class.



                                   class  Parent
                                   {
                                              protected :
                                                            int  data ;
                                   } ;


                                   class  Child1 : public  Parent
                                   {

                                   } ;

                                   class  Child2 : public  Parent
                                   {

                                   } ;


                                   class  Grandchild : public  Child1,  public  Child2
                                   {
                 
                                               public :
                                                          int  readdata ( )
                                                          {
                                                                        return  data ;
                                                          }
                                   } ;



A  compiler  error  occurs  when  the  readdata ( )  member  function  in   Grandchild attempts to  access  data  in  Parent.  When  the  Child1  and  Child2  classes  are  derived from Parent,  each  inherits  a  copy  of  Parent.  Each  of  the  two  child-classes  contains its  own copy  of  Parent's  data.  Now,  when  Grandchild  refers  to  data,  which  of  the two  copies will  it access?  This  situation  is  ambiguous, and  that's  what  the  compiler reports.

To  eliminate  the  ambiguity,  we  make  Child1  and  Child2  into  virtual  base  classes.



                                   class  Parent
                                   {
                                           protected :
                                                       int  data ;
                                   } ;


                                   class  Child1 : virtual  public  Parent
                                   {


                                   } ;


                                   class  Child2 : virtual  public  Parent
                                   {


                                   } ;


                                   class  Grandchild : public  Child1,  public  Child2
                                   {
                                              public :
                                                           int  readdata ( )
                                                           {
                                                                 return  data ;
                                                           }
                                   } ;