Page 222 - DCAP404 _Object Oriented Programming
P. 222

Unit 10: Virtual Functions and Polymorphism




                 cout<<  “Three  \n”;  }                                                        Notes


          };
          void  main  (  )  {
                 //define  three  objects
                 baseA  obja;
                 derivedB  objb;

                 derivedC  objc;
                 base  A  *ptr  [3];  //define  an  array  of  pointers  to  baseA
                 ptr  [0]  =  &obja;
                 ptr  [1]  =  &objb;
                 ptr  [2]  =  &objc;
                 for  (int  i  =  0;  i  <=2;  i  ++  )
                 ptr  [i]->display  ();  //same  message  for  all  objects

                 getche  (  );
          }
          Output
          One
          Two
          Three

          The program listed below illustrates the static binding of the member functions of a class. In
          program there are two classes student and academic. The class academic is derived from class
          student. The two member function getdata and display are defined for both the classes. *obj is
          defined for class student, the address of which is stored in the object of the class academic.





             Notes  The functions getdata ( ) and display ( ) of student class are invoked by the pointer
             to the class.
          #  include  <iostream.h>
          #include  <conio.h>
          class  student  {
          private:

          int  rollno;
          char  name  [20];
          public:
          void  getdata  (  );
          void  display  (  );
          };



                                           LOVELY PROFESSIONAL UNIVERSITY                                   215
   217   218   219   220   221   222   223   224   225   226   227