Page 224 - DCAP404 _Object Oriented Programming
P. 224

Unit 10: Virtual Functions and Polymorphism




          output                                                                                Notes
          enter  rollno
          25
          enter name

          raghu
          the student’s roll number is 25 and name is raghu



             Did u know?  What is dynamic binding?
             You can specify the compiler match a function call with the correct function definition at
             run time; this is called dynamic binding.
          The program listed below illustrates the dynamic binding of member functions of a class. In this
          program there are two classes student and academic. The class academic is derived from student.
          Student function has two virtual functions getdata ( ) and display (). The pointer for student class
          is defined and object for academic class is created. The pointer is assigned the address of the
          object and function of derived class are invoked by pointer to student.
          #include  <iostream.h>
          #include  <conio.h>
          class  student  {
          private:

          introllno;
          char  name  [20];
          public:
          virtual  void  getdata  (  );
          virtual  void  display  (  );
          };
          class  academic:  public  student  {

          private  :
          char  stream[10];
          public:
          void  getdata  (  );
          void  display  (  );
          };
          void  s_dent::  getdata  (  )
          {
                 cout<<  “enter  rollno\n”;

                 cin  >>  rollno;
                 cout<<  “enter  name  \n”;
                 cin  >>name;



                                           LOVELY PROFESSIONAL UNIVERSITY                                   217
   219   220   221   222   223   224   225   226   227   228   229