Page 199 - DCAP404 _Object Oriented Programming
P. 199

Object-oriented Programming




                    Notes          inheritance by the child might cause some problems. All the public and protected members of
                                   ‘grandparent’ are inherited into ‘child’ twice, first via ‘parent1’ and again via ‘parent2’. So, there
                                   occurs a duplicacy which should be avoided.
                                   The duplication of the inherited members can be avoided by making common base class as the
                                   virtual base class: for e.g.

                                                        class  g_parent
                                                        {
                                   //Body
                                                        };
                                                        class  parent1:  virtual  public  g_parent
                                                        {

                                                               //  Body
                                                        };
                                                        class  parent2:  public  virtual  g_parent
                                                        {
                                                               //  Body
                                                        };
                                                        class  child  :  public  parent1,  public  parent2
                                                        {
                                                               //  body

                                                        };
                                   When a class is virtual base class, C++ takes necessary care to see that only one copy of that class
                                   is inherited, regardless of how many inheritance paths exists between virtual base class and
                                   derived class.




                                     Notes  Note that keywords ‘virtual’ and ‘public’ can be used in either order.

                                   //Program to show the virtual base class
                                                 #  include  <  iostream.h>

                                                 #  include  <  conio.h>
                                                 class  student               //  Base  class  declaration
                                                 {
                                                 protected:
                                                        int  r_no;
                                                 public:
                                                        void  get_n  (int  a)
                                                        {  r_no  =  a;}
                                                        void  put_n  (void)




          192                               LOVELY PROFESSIONAL UNIVERSITY
   194   195   196   197   198   199   200   201   202   203   204