Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
Your Ad Here
Your Ad Here
Abstract  Classes  and  Pure  Virtual  Functions

Abstract  class  is  a  class  that  serves  only  as  a  base  class  from  which  classes  are derived. No objects  of  an  abstract  base  class  are  created.  A  base  class  that contains  pure  virtual functions is an  abstract  base  class.


Pure  virtual  function  can  be  defined  as


                                            virtual  void  show ( ) = 0 ;  //  pure  virtual  function


The  equal  sign  here  has  nothing  to  do  with  assignment;  the value  0  is  not  assigned  to  anything. The  =0  syntax  is  simply  how  we  tell  the  compiler  that  a  function  will  be  pure  virtual  function.

A  pure  virtual  function  is  a  function  declared  in  a  base  class  that  has  no  definition  relative  to  the base  class.  In  such  cases,  the  compiler  requires  each  derived  class  to  either  define  the  function or  redeclare  it  as  a  pure  virtual  function.  Remember  that  a  class  containing  pure  virtual  functions cannot  be  used  to  declare  any  objects  of  its  own.  Such  classes  are  called  abstract  base classes. The  main  objective  of  an  abstract  base  class  is  to  provide  some  traits  to  the  derived classes and to  create  a  base  pointer  required  for  achieving  runtime  polymorphism. .



                                  class  Base
                                  {
                                               public :
                                                          virtual  void  display ( )  =  0 ;
                                  } ;


                                  class  Derived1 : public  Base
                                  {
                                               public :
                                                           void  display ( )
                                                           {
                                                                    cout  <<  " derived1 " ;
                                                           }
                                  } ;



                                   class  Derived2 : public  Base
                                   {
                                             public :
                                                           void  display ( )
                                                           {
                                                                    cout  <<  " derived2 " ;
                                                           }
                                   } ;



                                  int  main ( )
                                  {
                                               Base  b;      //  error  can't  make  object  from  abstract  class

                                               Base  * ar [ 2 ] ;  //  array  of  pointers  to  base  class

                                               Derived1  d1;
                                               Derived2  d2;

                                               ar [ 0 ] = & d1 ;
                                               ar [ 1 ] = & d2 ;

                                               ar [ 0 ] ->display ( ) ;   //  execute  derived1's  display ( )
                                               ar [ 1 ] ->display ( ) ;   //  execute  derived2's  display ( )

                                               return  0 ;
                                  }



However,  programmer  can  declare  pointers  to  an  abstract  class  to  achieve  runtime polymorphism.